Search Unity

  1. Unity 2019.1 beta is now available.
    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. We're looking for insight from anyone who has experience with game testing to help us better Unity. Take our survey here. If chosen to participate you'll be entered into a sweepstake to win an Amazon gift card.
    Dismiss Notice
  4. On February 28th the Feedback website will shut down and be redirected to the Unity forums. See the full post for more information.
    Dismiss Notice
  5. Want to provide direct feedback to the Unity team? Join the Unity Advisory Panel.
    Dismiss Notice
  6. Unity 2018.3 is now released.
    Dismiss Notice
  7. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice

Unity Multiplayer Attempt to send to not connected connection

Discussion in 'Connected Games' started by Ezro, Jul 13, 2018.

  1. Ezro


    Jun 7, 2013
    Hey everyone,

    My current code flow is the following:
    WorldButton.OnClick() -> int connId = NetworkSystem.MigrateServer() -> NetworkSystem.SendConnect()

    Code (CSharp):
    1. void Start()
    2. {
    3.         NetworkTransport.Init();
    4.         ConnectionConfig cc = new ConnectionConfig();
    6.         ReliableChannel = cc.AddChannel(QosType.Reliable);
    7.         ReliableFragmentedChannel = cc.AddChannel(QosType.ReliableFragmented);
    8.         UnreliableChannel = cc.AddChannel(QosType.Unreliable);
    9. }
    Code (CSharp):
    1.     public int MigrateServer(int connectionId, string address, int port)
    2.     {
    3.         NetworkTransport.Disconnect(hostId, connectionId, out error);
    4.         if (error == 0)
    5.         {
    6.             Connection connection = OpenConnections.First(a => a.ConnectionId == connectionId);
    7.             OpenConnections.Remove(connection);
    8.         }
    9.         int connId = NetworkTransport.Connect(hostId, address, port, 0, out error);
    10.         if (error == 0)
    11.         {
    12.             Connection connection = new Connection("WorldServer", connId);
    13.             Debug.Log("Connected to " + connection.ConnectionType + " as " + connection.ConnectionId);
    14.             OpenConnections.Add(connection);
    15.             byte[] connect = new byte[1];
    16.             connect[0] = 1;
    17.             NetworkTransport.Send(hostId, connId, ReliableChannelId, b, b.Length, out error);
    18.             return connId;
    19.         }
    20.         return -1;
    21.     }
    In my MigrateServer, I see that I'm connecting properly with Debug.Log() (connId returns as != -1), but when I try to Send the connect[], I receive the message "Attempt to send to not connected connection".

    Does anyone know why this may be the case? I thought that since the connId was != -1 that it had already established the handshake.
  2. Ezro


    Jun 7, 2013
    Over the weekend I was able to resolve this issue. I didn't read (understand) the documentation well enough, but have come to understand that when you connect it creates a connectionId from a connectionId pool within the requesting client itself.

    As a result, you can receive connectionId >= 0 with error == 0, but you're not actually connected to the other endpoint at that point. As the documentation states, I was able to remedy this by leveraging the ConnectEvent and NetworkTransport Receive.