Search Unity

  1. Check out the Unite LA keynote for updates on the Visual Effect Editor, the FPS Sample, ECS, Unity for Film and more! Watch it now!
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. Improved Prefab workflow (includes Nested Prefabs!), 2D isometric Tilemap and more! Get the 2018.3 Beta now.
    Dismiss Notice
  4. Want more efficiency in your development work? Sign up to receive weekly tech and creative know-how from Unity experts.
    Dismiss Notice
  5. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  6. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

Trouble with NetworkTransport

Discussion in 'Connected Games' started by Aeonvirtual, Jul 7, 2018.

  1. Aeonvirtual

    Aeonvirtual

    Joined:
    Oct 14, 2015
    Posts:
    22
    I'm trying to use NetworkTransport to establish a connection between two separate machines. But any connection attempt from the client to the server is not seen by the server. I tried different socketPorts, but nothing works and I'm stumped. Can anyone see what is wrong?

    On both client and server I run this to initialize:
    Code (CSharp):
    1. private void initNetwork() {
    2.     NetworkTransport.Init();
    3.     ConnectionConfig config = new ConnectionConfig();
    4.     c1 = config.AddChannel(QosType.Reliable);
    5.     topology = new HostTopology(config, 10);
    6.     hostId = NetworkTransport.AddHost(topology, socketPort);
    7. }
    Then, on the client, I run this to request a connection. I get no errors (NetworkError.OK )
    Code (CSharp):
    1. connectionId = NetworkTransport.Connect(hostId, serverIp, socketPort, 0, out err);
    2.  
    The server is listening for incoming calls like this (called from Update) but it never gets anything other than NetworkEventType.Nothing
    Code (CSharp):
    1. private void handleServer() {
    2.     int recHostId;
    3.     int connectionId;
    4.     int channelId;
    5.     int dataSize;
    6.     byte error;
    7.  
    8.     byte[] workingBuffer = new byte[1024];
    9.     byte[] recBuffer;
    10.  
    11.     NetworkEventType recData = NetworkTransport.Receive(out recHostId, out connectionId, out channelId, workingBuffer, workingBuffer.Length, out dataSize, out error);
    12.  
    13.     if (recData == NetworkEventType.Nothing)
    14.         return;
    15.  
    16.     // it never gets here  
    17.     Debug.Log("Event: " + recData.ToString());
    18.  
    19.     recBuffer = new byte[dataSize];
    20.  
    21.     if (dataSize > 0) {
    22.         Buffer.BlockCopy(workingBuffer, 0, recBuffer, 0, dataSize);
    23.     }
    24.  
    25.     switch (recData) {
    26.         case NetworkEventType.ConnectEvent: Debug.Log("Connected!"); break;
    27.         case NetworkEventType.DisconnectEvent: Debug.Log("Disconnected!"); break;
    28.         case NetworkEventType.DataEvent: Debug.Log("Data!"); break;
    29.     }
    30. }
     
  2. Aeonvirtual

    Aeonvirtual

    Joined:
    Oct 14, 2015
    Posts:
    22
    Anyone?
     
  3. TwoTen

    TwoTen

    Joined:
    May 25, 2016
    Posts:
    1,010
    No errors from frame polling on either server or client?

    That would mean something is preventing the socket from connecting. Software (commonly firewall. Windows firewall for example is really S*** and you might have to go in and remove the block if you pressed cancel when the firewall prompted you about Unity for the first time) and NAT. Those are the most common problems.
     
  4. Aeonvirtual

    Aeonvirtual

    Joined:
    Oct 14, 2015
    Posts:
    22
    I'm not sure what you mean by frame polling. But I don't seem to be getting any errors from NetworkTransport.Connect
    I used a packet sniffer to verify that I am getting messages from the client on the server and I do. Its just that in unity, it does not register the connect event.
     
  5. TwoTen

    TwoTen

    Joined:
    May 25, 2016
    Posts:
    1,010
    Poll the Receive method every frame.
     
  6. Aeonvirtual

    Aeonvirtual

    Joined:
    Oct 14, 2015
    Posts:
    22
    The client does not give an error on Connect and the server does not give any errors on Receive, but the NetworkEventType is always Nothing.
     
  7. Aeonvirtual

    Aeonvirtual

    Joined:
    Oct 14, 2015
    Posts:
    22
    Alright, so I was using Receive instead of ReceiveFromHost. Now it is working. Took me a while to figure out!