Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Opening socket using NetworkConnection.address

Discussion in 'Multiplayer' started by fredsom, Dec 16, 2015.

  1. fredsom

    fredsom

    Joined:
    Feb 15, 2015
    Posts:
    19
    Hi,

    I'm trying to connect my clients with the llapi after they found each other with the matchmaking. I tested my sockets with a local connection and could send datas to myself (yay !).
    Now I am using the ip address given in NetworkConnection.address (obtained in the callbacks in the NetworkManager class), is it a good thing ? If not, is there another way ?

    As a result, I obtain no errors and no other message, I only receive a disconnected event after a while (which probably means the connection request timed out). In case this is not related with using the ip address given by the NetworkManager, here is the code explained :

    After both clients are connected and ready, I call the iniNetwork function :
    Code (CSharp):
    1. public void initNetwork()
    2.     {
    3.         client = ClientManager.Instance.getLocalClient();
    4.         NetworkTransport.Init();
    5.         ConnectionConfig config = new ConnectionConfig();
    6.         channelId = config.AddChannel(QosType.UnreliableFragmented);
    7.         HostTopology topology = new HostTopology(config, 10);
    8.         recieverId = NetworkTransport.AddHost(topology, portNum);
    9.         senderId = NetworkTransport.AddHost(topology);
    10.         initialized = true;
    11.     }
    At the same time, a button is displayed to start llapi connection. This button is only available when both clients have their Transport Layer initialized.

    This button calls the following function :
    Code (CSharp):
    1. public void connect()
    2.     {
    3.         byte error;
    4.         EFClient other = ClientManager.Instance.getDistantClient();
    5.         Debug.Log(other.connection.address);
    6.         connectionId = NetworkTransport.Connect(senderId, address, portNum, 0, out error);
    7.         if (error != (byte)NetworkError.Ok)
    8.         {
    9.             NetworkError nerror = (NetworkError)error;
    10.             Debug.Log("Error: " + nerror.ToString());
    11.         }
    12.     }
    I listen to event in the Update() with :

    Code (CSharp):
    1. if (initialized)
    2.         {
    3.             int recHostId;
    4.             int recConnectionId;
    5.             int recChannelId;
    6.             byte[] recBuffer = new byte[1024];
    7.             int bufferSize = 1024;
    8.             int dataSize;
    9.             byte error;
    10.             NetworkEventType recNetworkEvent = NetworkTransport.Receive(out recHostId, out recConnectionId, out recChannelId, recBuffer, bufferSize, out dataSize, out error);
    11.             switch (recNetworkEvent)
    12.             {
    13.                 case NetworkEventType.ConnectEvent:
    14.                     // Server received disconnect event
    15.                     if (recHostId == recieverId)
    16.                     {
    17.                         Debug.Log("Server: Player " + connectionId.ToString() + " connected!");
    18.                     }
    19.                     if (recHostId == senderId)
    20.                     {
    21.                         Debug.Log("Client: Client connected to " + connectionId.ToString() + "!");
    22.                     }
    23.  
    24.                     break;
    25.                 case NetworkEventType.Nothing:
    26.                     break;
    27.                 case NetworkEventType.DataEvent:
    28.                     // Server received data
    29.                     if (recHostId == recieverId)
    30.                     {
    31.  
    32.                         // Let's decode data
    33.                         Stream stream = new MemoryStream(recBuffer);
    34.                         BinaryFormatter f = new BinaryFormatter();
    35.                         string msg = f.Deserialize(stream).ToString();
    36.  
    37.                         Debug.Log("Server: Received Data from " + connectionId.ToString() + "! Message: " + msg);
    38.                     }
    39.                     if (recHostId == senderId)
    40.                     {
    41.                         Debug.Log("Client: Received Data from " + connectionId.ToString() + "!");
    42.                     }
    43.                     break;
    44.  
    45.                 case NetworkEventType.DisconnectEvent:
    46.                     // Client received disconnect event
    47.                     if (recHostId == senderId && connectionId == recConnectionId)
    48.                     {
    49.                         Debug.Log("Client: Disconnected from server!");
    50.                     }
    51.  
    52.                     // Server received disconnect event
    53.                     if (recHostId == recieverId)
    54.                     {
    55.                         Debug.Log("Server: Received disconnect from " + connectionId.ToString());
    56.                     }
    57.                     break;
    58.             }
    59.         }
    Thank you for your help, sorry for the long post.
     
  2. fredsom

    fredsom

    Joined:
    Feb 15, 2015
    Posts:
    19
    Still did not find a solution to this problem. Having to use RPCs to send a stream of bytes isn't really convenient...