Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Cant connect to local host or send messages to client

Discussion in 'Multiplayer' started by Epic-Username, May 29, 2017.

  1. Epic-Username

    Epic-Username

    Joined:
    May 24, 2015
    Posts:
    339
    I'm trying to set up a networked project and I've decided to use the llapi my code basically does the same as this: https://docs.unity3d.com/Manual/UNetUsingTransport.html except with very few changes.
    But with this setup the client doesn't receive network events and i cant connect to the local host when running 2 instances of the build on the same machine but when i port forward on another machine i can connect between 2 computers and only the server receives network events.

    I've been trying to fix this problem by researching for a couple weeks and have received no results and have not come any closer to the solution can someone please help before i go crazy, thanks.
     
  2. Epic-Username

    Epic-Username

    Joined:
    May 24, 2015
    Posts:
    339
    Hello? is this a bug or something, can someone please respond?
     
  3. TomPo

    TomPo

    Joined:
    Nov 30, 2013
    Posts:
    86
    For me is working

    Server side:
    Code (CSharp):
    1. NetworkTransport.Init();
    2.  ConnectionConfig ConnConfig = new ConnectionConfig();
    3.  channelID_ReliableSequenced = ConnConfig.AddChannel(QosType.ReliableSequenced);
    4.  HostTopology topology = new HostTopology(ConnConfig, maxConnections);
    5.  hostID = NetworkTransport.AddHost(topology, portNr, null);
    Client side: (other project)
    Code (CSharp):
    1. NetworkTransport.Init();
    2.  ConnectionConfig ConnConfig = new ConnectionConfig();
    3.  channelID_ReliableSequenced = ConnConfig.AddChannel(QosType.ReliableSequenced);
    4.  HostTopology topology = new HostTopology(ConnConfig, maxConnections);
    5.  hostID = NetworkTransport.AddHost(topology);
    6.  connectionID = NetworkTransport.Connect(hostID, MasterServerIP, portNr, 0,out error);
    To send msg:
    Code (CSharp):
    1. byte[] buffer = System.Text.Encoding.UTF8.GetBytes(msg);
    2. byte error;
    3. NetworkTransport.Send(hostID, connectionID, 0, buffer, msg.Length * sizeof(char), out error);
    To recieve msg: (inside update)
    Code (CSharp):
    1. int recHostID, recConnID, recChannelID, dataSize, bufferSize = 1024;
    2. byte[] recBuffer = new byte[1024];
    3. byte error;
    4.  
    5. NetworkEventType recEvent = NetworkTransport.Receive(out recHostID, out recConnID, out recChannelID, recBuffer, bufferSize, out dataSize, out error);
    6.  
    7. if (recEvent == NetworkEventType.Nothing) return;
    8.  
    9.         switch (recEvent) {
    10.             case NetworkEventType.ConnectEvent:
    11.                 //add what you want
    12.                 break;
    13.             case NetworkEventType.DisconnectEvent:
    14.                //add what yyou want
    15.                 break;
    16.             case NetworkEventType.DataEvent:
    17.                 string msg = System.Text.Encoding.UTF8.GetString(recBuffer);
    18.                 break;
    19.         }
     
  4. Epic-Username

    Epic-Username

    Joined:
    May 24, 2015
    Posts:
    339
    Finally a response after more then a week, though to be fair my question is kinda vague... anyway thank you for the response, I'll compare my code to this and see where i went wrong.
     
  5. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    It is very hard to help you without your code.
     
  6. Epic-Username

    Epic-Username

    Joined:
    May 24, 2015
    Posts:
    339
    From that code I've made a quick test script:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.Networking;
    5.  
    6. public class NetworkManager : MonoBehaviour
    7. {
    8.  
    9.     int channelID_ReliableSequenced;
    10.     int hostID;
    11.     int connectionID;
    12.  
    13.     HostTopology topology;
    14.  
    15.     void Start ()
    16.     {
    17.         NetworkTransport.Init ();
    18.         ConnectionConfig ConnConfig = new ConnectionConfig ();
    19.         channelID_ReliableSequenced = ConnConfig.AddChannel (QosType.ReliableSequenced);
    20.         topology = new HostTopology (ConnConfig, 20);
    21.     }
    22.  
    23.     void Update ()
    24.     {
    25.         int recHostID, recConnID, recChannelID, dataSize, bufferSize = 1024;
    26.         byte[] recBuffer = new byte[1024];
    27.         byte error;
    28.  
    29.         NetworkEventType recEvent = NetworkTransport.Receive (out recHostID, out recConnID, out recChannelID, recBuffer, bufferSize, out dataSize, out error);
    30.  
    31.         if (recEvent == NetworkEventType.Nothing)
    32.             return;
    33.  
    34.         switch (recEvent) {
    35.         case NetworkEventType.ConnectEvent:
    36.             //add what you want
    37.             break;
    38.         case NetworkEventType.DisconnectEvent:
    39.             //add what yyou want
    40.             break;
    41.         case NetworkEventType.DataEvent:
    42.             string msg = System.Text.Encoding.UTF8.GetString (recBuffer);
    43.             print ("recieved: " + msg);
    44.             break;
    45.         }
    46.     }
    47.  
    48.     bool clientRunning;
    49.     bool serverRunning;
    50.  
    51.     string serverIPGUI = "127.0.0.1";
    52.     string portGUI = "7777";
    53.     string msg = "";
    54.  
    55.     void OnGUI ()
    56.     {
    57.         if (serverRunning == false && clientRunning == false) {
    58.             serverIPGUI = GUI.TextField (new Rect (10, 15, 160, 20), serverIPGUI, 25);
    59.             portGUI = GUI.TextField (new Rect (10, 55, 160, 20), portGUI, 25);
    60.  
    61.             if (GUI.Button (new Rect (180, 10, 260, 30), "Run Client On: " + serverIPGUI + ":" + portGUI)) {
    62.                 print ("Running client");
    63.                 clientRunning = true;
    64.                 runClient (int.Parse (portGUI), serverIPGUI);
    65.             }
    66.  
    67.             if (GUI.Button (new Rect (180, 50, 160, 30), "Run Server")) {
    68.                 print ("Running server");
    69.                 serverRunning = true;
    70.                 runServer (int.Parse (portGUI));
    71.             }
    72.  
    73.         } else {
    74.            
    75.             msg = GUI.TextField (new Rect (10, 15, 160, 20), msg, 25);
    76.             if (GUI.Button (new Rect (170, 10, 160, 30), "Send: " + msg)) {
    77.                 print ("attempting to send message");
    78.                 SendMessage (msg);
    79.             }
    80.  
    81.         }
    82.  
    83.     }
    84.  
    85.  
    86.     public void runServer (int port)
    87.     {
    88.         hostID = NetworkTransport.AddHost (topology, port, null);
    89.     }
    90.  
    91.     public void runClient (int port, string serverIP)
    92.     {
    93.         byte error;
    94.  
    95.         hostID = NetworkTransport.AddHost (topology);
    96.         connectionID = NetworkTransport.Connect (hostID, serverIP, port, 0, out error);
    97.     }
    98.  
    99.     public void SendMessage (string msg)
    100.     {
    101.         byte[] buffer = System.Text.Encoding.UTF8.GetBytes (msg);
    102.         byte error;
    103.         NetworkTransport.Send (hostID, connectionID, 0, buffer, msg.Length * sizeof(char), out error);
    104.     }
    105.  
    106.  
    107. }
    108.  
    I run the server in a window and the client in unity and when i send a message from the server the client doesn't recieve the message, so what am i doing wrong here?
     
  7. Epic-Username

    Epic-Username

    Joined:
    May 24, 2015
    Posts:
    339
  8. morvi

    morvi

    Joined:
    Feb 8, 2014
    Posts:
    10
    In case, this is still an issue, it's most probably because the same port is used for server and client. On the same machine, this doesn't work, since two sockets (server-socket and client socket) cannot share the same port.

    So you should check if the client connects to 127.0.0.1 (or localhost, but localhost didn't work for me as server ip adress) and in this case choose an alternative port for the client (7778, e.g.) - or give the client the opportunity to choose his port manually