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. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  5. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

Annoying networking problem with "Attempt to send to not connected connection"

Discussion in 'Connected Games' started by sblombliz, Jul 28, 2017.

  1. sblombliz

    sblombliz

    Joined:
    Feb 18, 2017
    Posts:
    1
    Hello everyone,
    this is my first post and i very tired trying solving this problem.

    When a client disconnect, server try to send message on disconnected client returning a warning and other two errors. I've tryed to close all possible connection but error still remain.

    I'm using NatPunchthrought script starting from this project:
    https://github.com/noblewhale/NATPunchthroughClient

    this is the code:

    Code (CSharp):
    1. public class NATServer : NetworkServerSimple
    2. {
    3.     public override void Initialize()
    4.     {
    5.         base.Initialize();
    6.  
    7.         // Use the custom connection class to prevent connectionId conflicts
    8.         SetNetworkConnectionClass<NATServerNetworkConnection>();
    9.     }
    10.  
    11.     public override void OnConnected(NetworkConnection conn)
    12.     {
    13.         base.OnConnected(conn);
    14.         NetworkServer.AddExternalConnection(conn);
    15.     }
    16. }
    17.  
    18. public class NATServerNetworkConnection : NetworkConnection
    19. {
    20.     private int offset;
    21.  
    22.     /*
    23.     * Add an offset to the connectionId. We first offset by the maxinum number of connections that the server expects
    24.     * and then further offset by the hostId since each additional host will only have one connection
    25.     *
    26.     */
    27.     public override void Initialize(string networkAddress, int networkHostId, int networkConnectionId, HostTopology hostTopology)
    28.     {
    29.         offset = NetworkServer.hostTopology.MaxDefaultConnections + networkHostId;
    30.         base.Initialize(networkAddress, networkHostId, networkConnectionId + offset, hostTopology);
    31.     }
    32.  
    33.  
    34.     /*
    35.      * Instead of sending out on the NetworkServer we need to send out on
    36.      * the appropriate NATServer that the client connected to.
    37.      * We also un-offset the connectionId to get the id of the connection on the NATServer
    38.      */  
    39.     public override bool TransportSend(byte[] bytes, int numBytes, int channelId, out byte error)
    40.     {
    41.         // i can use "isReady" for track if client disconnected
    42.         return NetworkTransport.Send(hostId, connectionId - offset, channelId, bytes, numBytes, out error);
    43.  
    44.     }
    45. }
    This is the custom classes used on project, now, my netManager custom class:
    Code (CSharp):
    1.  
    2. public class NetManager : NetworkManager
    3. {
    4.     //-- SOME CODE --//
    5.  
    6.     void Start()
    7.     {
    8.         //Check if instance already exists
    9.         if (instance == null)
    10.  
    11.             //if not, set instance to this
    12.             instance = this;
    13.  
    14.         //If instance already exists and it's not this:
    15.         else if (instance != this)
    16.  
    17.             //Then destroy this. This enforces our singleton pattern, meaning there can only ever be one instance of a GameManager.
    18.             Destroy(gameObject);
    19.  
    20.         //Sets this to not be destroyed when reloading scene
    21.         DontDestroyOnLoad(gameObject);
    22.  
    23.         clientIDField = typeof(NetworkClient).GetField("m_ClientId", BindingFlags.NonPublic | BindingFlags.Instance);
    24.         natHelper = GetComponent<NATHelper>();
    25.  
    26.         //settings for default connectionConfig to use
    27.         foreach (QosType type in base.channels)
    28.             connectionConfig.AddChannel(type);
    29.      
    30.     }
    31.    
    32.     //-- SOME CODE --//
    33.  
    34.    // When the server gets the onHolePunchedServer callback it will be passed the port that you need to listen on.
    35.     void OnHolePunchedServer(int natListenPort)
    36.     {
    37.         NATServer newServer = new NATServer();
    38.      
    39.         bool isListening = newServer.Listen(natListenPort, NetworkServer.hostTopology);
    40.         if (isListening)
    41.             natServers.Add(newServer);
    42.     }
    43.  
    44.     //-- SOME CODE --//
    45.  
    46.     /*
    47.      * When the client receives the onHolePunchedClient callback it will be passed two ports.
    48.      * You need to use the first port as the port the client is connecting from and the second port as the port the client is connecting to on the server.
    49.      */
    50.     void OnHolePunchedClient(int natListenPort, int natConnectPort)
    51.     {
    52.         Debug.Log("OnHolePunchedClient");
    53.         // The port on the server that we are connecting to
    54.         networkPort = natConnectPort;
    55.  
    56.         // Make sure to connect to the correct IP or things won't work
    57.         if (hostGameMM.external_ip == natHelper.externalIP)
    58.         {
    59.             if (hostGameMM.internal_ip == Network.player.ipAddress)
    60.             {
    61.                 // Host is running on the same computer as client, two separate builds
    62.                 networkAddress = "127.0.0.1";
    63.             }
    64.             else
    65.             {
    66.                 // Host is on the same local network as client
    67.                 networkAddress = hostGameMM.internal_ip;
    68.             }
    69.         }
    70.         else
    71.         {
    72.             // Host is somewhere out on the internet
    73.             networkAddress = hostGameMM.external_ip;
    74.         }
    75.  
    76.         // Standard client setup stuff than the NetworkMager would normally take care of for us
    77.         NetworkTransport.Init(globalConfig);
    78.  
    79.         /**
    80.         if (customConfig)
    81.         {
    82.             foreach (QosType type in base.channels)
    83.                 connectionConfig.AddChannel(type);
    84.         }
    85.         else
    86.         {
    87.             connectionConfig..AddChannel(QosType.ReliableSequenced);
    88.             connectionConfig.AddChannel(QosType.Unreliable);
    89.         }*/
    90.  
    91.         // If we try to use maxConnection when the Advanced Configuration checkbox is not checked
    92.         // we will get a crc mismatch because the host will have been started with the default
    93.         // max players of 8 rather than the value in maxConnections
    94.         //int maxPlayers = 8;
    95.         //if (customConfig)
    96.         //    maxPlayers = maxConnections;
    97.         int maxPlayers = maxConnections;
    98.  
    99.         HostTopology topo = new HostTopology(connectionConfig, maxPlayers);
    100.  
    101.         // Start up a transport level host on the port that the hole was punched from
    102.         // This doesn’t actually add a “Host” like the HLAPI does. This command actually just starts up a socket, I’m pretty sure they should change the name of the method to AddSocket
    103.         int natListenSocketID = NetworkTransport.AddHost(topo, natListenPort);
    104.      
    105.         // Create and configure a new NetworkClient
    106.         client = new NetworkClient();
    107.         client.Configure(topo);
    108.  
    109.         // Connect to the port on the server that we punched through to
    110.         client.Connect(networkAddress, networkPort);
    111.  
    112.         //NetworkTransport.RemoveHost((int)clientIDField.GetValue(client));
    113.  
    114.         // Magic! Set the client's transport level host ID so that the client will use
    115.         // the host we just started above instead of the one it creates internally when we call Connect.
    116.         // This has to be done so that the connection will be made from the correct port, otherwise
    117.         // Unity will use a random port to connect from and NAT Punchthrough will fail.
    118.         // This is the S*** that keeps me up at night.
    119.         clientIDField.SetValue(client, natListenSocketID);
    120.  
    121.         // Tell Unity to use the client we just created as _the_ client so that OnClientConnect will be called
    122.         // and all the other HLAPI stuff just works. Oh god, so nice.
    123.         UseExternalClient(client);
    124.      
    125.         GameManager.instance.isHost = false;
    126.     }
    127.  
    128.     //-- SOME CODE --//
    129.  
    130.     private void Update()
    131.     {
    132.         natServers.ForEach(server => server.Update());
    133.     }
    134.  
    135.  
    WARNING: Attempt to send to not connected connection {1}
    ERROR: Failed to send internal buffer channel:1 bytesToSend:27
    ERROR: Send Error: WrongConnection channel:1 bytesToSend:27

    Maybe i'm using wrong method to disconnect a client?

    I've try to use conn.Disconnect(); conn.Dispose(); when a client disconnect but nothing. How i can stop transport.send when Update() pump Network?
     
  2. Kajo

    Kajo

    Joined:
    Apr 15, 2013
    Posts:
    12
    Hello, im having the exact problem, found a solution to this?
     
  3. diego-vieira

    diego-vieira

    Joined:
    Mar 27, 2015
    Posts:
    23
    hello, I'm having the same problem either, did you guys found a solution ?
     
  4. crydrk

    crydrk

    Joined:
    Feb 10, 2012
    Posts:
    72
    I also have this problem. Watching this thread, and will report back if I find a solution as well.