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

NetworkTransport - Timeout after Connect

Discussion in 'Multiplayer' started by Akfly, Mar 16, 2017.

  1. Akfly

    Akfly

    Joined:
    Jan 31, 2017
    Posts:
    12
    Hi,

    I'm trying to connect two different PCs remotely, but when I call "Connect", I get a Timeout message followed up by a DisconnectEvent after some time. The other machine doesn't receive any event (it should get a ConnectEvent). I've tried opening ports and deactivating Firewalls on both PCs, but it didn't work.

    This code works on a LAN, but they just don't work online (yes I'm using the external IP for online).

    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using System.IO;
    5. using System.Runtime.Serialization.Formatters.Binary;
    6. using UnityEngine;
    7. using UnityEngine.Networking;
    8.  
    9. public class TestConn : MonoBehaviour
    10. {
    11.     int myReliableChannelId;
    12.     int socketId;
    13.     int socketPort = 8558;
    14.  
    15.     // Use this for initialization
    16.     void Start()
    17.     {
    18.         NetworkTransport.Init();
    19.         ConnectionConfig config = new ConnectionConfig();
    20.  
    21.         myReliableChannelId = config.AddChannel(QosType.Reliable);
    22.  
    23.         int maxConnections = 10;
    24.         HostTopology topology = new HostTopology(config, maxConnections);
    25.         socketId = NetworkTransport.AddHost(topology, socketPort);
    26.         Debug.Log("Socket Open. SocketId is: " + socketId);
    27.     }
    28.  
    29.     int connectionId;
    30.     public void Connect()
    31.     {
    32.         byte error;
    33.         connectionId = NetworkTransport.Connect(socketId, "myExternalIP", socketPort, 0, out error);
    34.         Debug.Log("Connected to server. ConnectionId: " + connectionId);
    35.  
    36.         if (error != (byte)NetworkError.Ok)
    37.             Debug.LogWarning("Connection Error: " + error + ": " + (NetworkError)error);
    38.     }
    39.  
    40.     public void SendSocketMessage()
    41.     {
    42.         byte error;
    43.         byte[] buffer = new byte[1024];
    44.         Stream stream = new MemoryStream(buffer);
    45.         BinaryFormatter formatter = new BinaryFormatter();
    46.         formatter.Serialize(stream, "HelloServer");
    47.  
    48.         int bufferSize = 1024;
    49.  
    50.         NetworkTransport.Send(socketId, connectionId, myReliableChannelId, buffer, bufferSize, out error);
    51.     }
    52.  
    53.     void Update()
    54.     {
    55.         int recHostId;
    56.         int recConnectionId;
    57.         int recChannelId;
    58.         byte[] recBuffer = new byte[1024];
    59.         int bufferSize = 1024;
    60.         int dataSize;
    61.         byte error;
    62.         NetworkEventType recNetworkEvent = NetworkTransport.Receive(out recHostId, out recConnectionId, out recChannelId, recBuffer, bufferSize, out dataSize, out error);
    63.  
    64.         if (error != (byte)NetworkError.Ok)
    65.             Debug.LogWarning("Connection Error: " + error + ": " + (NetworkError)error);
    66.  
    67.         switch (recNetworkEvent)
    68.         {
    69.             case NetworkEventType.Nothing:
    70.                 Debug.Log("Nothing...");
    71.                 break;
    72.             case NetworkEventType.ConnectEvent:
    73.                 Debug.Log("incoming connection event received");
    74.                 break;
    75.             case NetworkEventType.DataEvent:
    76.                 Stream stream = new MemoryStream(recBuffer);
    77.                 BinaryFormatter formatter = new BinaryFormatter();
    78.                 string message = formatter.Deserialize(stream) as string;
    79.                 Debug.Log("incoming message event received: " + message);
    80.                 break;
    81.             case NetworkEventType.DisconnectEvent:
    82.                 Debug.Log("remote client event disconnected");
    83.                 break;
    84.         }
    85.     }
    86.  
    87. }
    88.  
    89.  
     
    IgorAherne likes this.
  2. donnysobonny

    donnysobonny

    Joined:
    Jan 24, 2013
    Posts:
    220
    I have a feeling it might be that you are specifying a port, although I can't be 100% sure.

    Generally, unet is designed for the client-server model, where one side of the connection is a client (creating the connection to the server) and the other side is a server (accepting connections from many clients). In this case, you wouldn't want both the client and the server to use the same port, as this will confuse things.

    In my implementation, my client-side calls AddHost without specifying a port. This causes the socket to be set up on a randomly available port number, based on the OS. Since the client will be the person that calls Connect (the server should never need to call Connect to connect to a client...), the client can then specify the port number of the server to connect to.

    Obviously when calling AddHost for the server-side, you would specify a port number which is known to clients.

    So to simplify things. from the client-side:
    • call AddHost without specifying a port/address. Just pass the HostTopology
    • when calling Connect, specify the address/port of the server that you are connecting to
    From the server-side:
    • call AddHost and specify a port along with the HostTopology
    • handle incoming connections from clients in the ConnectEvent
    It is possible to implement server-to-server networking in unet by having two sockets (hosts) on either side. One as your incoming socket and one as your outgoing. If that's what you're trying to do, and you need further help with it, let me know.

    If you are sure that you've done all of the above and it's still not working, please provide an example of the full code that you are using including the actual Connect call being made etc.
     
    Akfly likes this.
  3. Akfly

    Akfly

    Joined:
    Jan 31, 2017
    Posts:
    12
    Thanks for the answer, I managed to solved the issue ^^.

    Actually I had different ports for clients and server, the problem was really simple, I was using the same variable for the port on "AddHost" and "Connect", so if the server had a different port it couldn't stablish a connection :p.