Search Unity

LLAPI issue: "Attempt to send to not connected connection"

Discussion in 'Multiplayer' started by ZoidbergForPresident, May 1, 2018.

  1. ZoidbergForPresident

    ZoidbergForPresident

    Joined:
    Dec 15, 2015
    Posts:
    157
    Hi,

    I'm in the making of a small game playable through lan and I have a wee bit issue.

    The connection (and disconnection) seems to go through between server and client but when the server tried to ask some data to the client (like just a name) that just connected, the message seems lost in the network and the client never gets it.

    I have a GameManager GO that manages the local game and two children GO of it: LANServer and LANClient, both have two scripts components - a LANDiscovery (used for LANDiscovery, which appears to work) and a Server or Client scripts that manage comms between... server and client.

    Here's the server:
    Code (CSharp):
    1. using System;
    2. using System.Collections.Generic;
    3. using System.Text;
    4. using UnityEngine;
    5. using UnityEngine.Networking;
    6.  
    7. public class Server : MonoBehaviour
    8. {
    9.     public LANDiscovery LanDiscovery { get; private set; }
    10.     public Action<OnClientNameReceivedEvent> OnCoacheeNameReceived;
    11.     public Action<OnClientDisconnectedEvent> OnCoacheeDisconnected;
    12.  
    13.     private const int MAX_CONNECTIONS = 100;
    14.  
    15.     private int port = 5701;
    16.  
    17.     private int hostId;
    18.     private int webHostId;
    19.  
    20.     private int reliableChannel;
    21.     private int unreliableChannel;
    22.  
    23.     private bool isStarted;
    24.     private byte error;
    25.  
    26.     private Dictionary<int,Coachee> clients = new Dictionary<int, Coachee>(); // Key is connection id / Data is Coachee but only using name (id is connection id)
    27.  
    28.     private void Awake()
    29.     {
    30.         LanDiscovery = GetComponent<LANDiscovery>();
    31.     }
    32.  
    33.     private void Start()
    34.     {
    35.         NetworkTransport.Init();
    36.  
    37.         ConnectionConfig cc = new ConnectionConfig();
    38.         reliableChannel = cc.AddChannel(QosType.Reliable);
    39.         unreliableChannel = cc.AddChannel(QosType.Unreliable);
    40.  
    41.         HostTopology topology = new HostTopology(cc, MAX_CONNECTIONS);
    42.  
    43.         hostId = NetworkTransport.AddHost(topology, port, null);
    44.         webHostId = NetworkTransport.AddWebsocketHost(topology, port, null);
    45.  
    46.         isStarted = true;
    47.     }
    48.  
    49.     private void Update()
    50.     {
    51.         if (!isStarted) return;
    52.  
    53.         int recHostId;
    54.         int connectionId;
    55.         int channelId;
    56.         byte[] recBuffer = new byte[1024];
    57.         int bufferSize = 1024;
    58.         int dataSize;
    59.         byte error;
    60.         NetworkEventType recData = NetworkTransport.Receive(out recHostId, out connectionId, out channelId, recBuffer, bufferSize, out dataSize, out error);
    61.  
    62.         switch (recData)
    63.         {
    64.             case NetworkEventType.Nothing:         //1
    65.                 break;
    66.             case NetworkEventType.ConnectEvent:    //2
    67.                 Debug.Log("Player " + connectionId + " has connected!");
    68.                 OnConnection(connectionId);
    69.                 break;
    70.             case NetworkEventType.DataEvent:       //3
    71.                 string msg = Encoding.Unicode.GetString(recBuffer, 0, dataSize);
    72.                 Debug.Log("Player " + connectionId + " has sent : " + msg);
    73.                 string[] splitData = msg.Split('|');
    74.  
    75.                 switch (splitData[0])
    76.                 {
    77.                     case Helper.Client.Message.COACHEEDATA:
    78.                         OnCoacheeNameReceivedHandler(connectionId, splitData[1]);
    79.                         break;
    80.                     case Helper.Client.Message.COACHEEACTION:
    81.                         OnCoacheeActionReceivedHandler(connectionId, splitData);
    82.                         break;
    83.                     default:
    84.                         Debug.Log("Invalid message received: " + msg);
    85.                         break;
    86.                 }
    87.                 break;
    88.             case NetworkEventType.DisconnectEvent: //4
    89.                 Debug.Log("Player " + connectionId + " has disconnected!");
    90.                 OnDisconnection(connectionId);
    91.                 break;
    92.         }
    93.     }
    94.  
    95.     private void OnConnection(int connectionId)
    96.     {
    97.         clients.Add(connectionId, new Coachee("Unknown"));
    98.  
    99.         string msg = /* msg is built to be sent */
    100.  
    101.         Send(msg, reliableChannel, connectionId);
    102.     }
    103.  
    104.     private void OnDisconnection(int connectionId)
    105.     {
    106.         clients.Remove(connectionId);
    107.  
    108.         if (OnCoacheeDisconnected != null)
    109.         {
    110.             OnCoacheeDisconnected(new OnClientDisconnectedEvent(connectionId));
    111.         }
    112.     }
    113.  
    114.     private void OnCoacheeNameReceivedHandler(int connectionId, string playerName)
    115.     {
    116.         clients[connectionId].Name = playerName;
    117.  
    118.         if(OnCoacheeNameReceived != null)
    119.         {
    120.             OnCoacheeNameReceived(new OnClientNameReceivedEvent(connectionId, playerName));
    121.         }
    122.     }
    123.  
    124.     /// <summary>
    125.     /// Send to one specific connectionId.
    126.     /// </summary>
    127.     /// <param name="message"></param>
    128.     /// <param name="channelId"></param>
    129.     /// <param name="connectionId"></param>
    130.     private void Send(string message, int channelId, int connectionId)
    131.     {
    132.         Dictionary<int, Coachee> ccl = new Dictionary<int, Coachee>();
    133.         ccl.Add(connectionId, clients[connectionId]);
    134.         Send(message, channelId, ccl);
    135.     }
    136.  
    137.     /// <summary>
    138.     /// Send to all curently connected clients.
    139.     /// </summary>
    140.     /// <param name="message"></param>
    141.     /// <param name="channelId"></param>
    142.     /// <param name="serverClients"></param>
    143.     private void Send(string message, int channelId, Dictionary<int, Coachee> coachees)
    144.     {
    145.         Debug.Log("Sending : " + message);
    146.         byte[] bmsg = Encoding.Unicode.GetBytes(message);
    147.         foreach (var key in coachees.Keys)
    148.         {
    149.             NetworkTransport.Send(hostId, key, channelId, bmsg, message.Length * sizeof(char), out error);
    150.         }
    151.     }
    152. }
    And here's the client:
    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using System.Text;
    5. using UnityEngine;
    6. using UnityEngine.Networking;
    7. using UnityEngine.UI;
    8.  
    9. public class Client : MonoBehaviour
    10. {
    11.     public LANDiscovery LanDiscovery { get; private set; }
    12.     public Action<OnServerMessageReceivedEvent> OnServerMessageReceived;
    13.  
    14.     private const int MAX_CONNECTIONS = 100;
    15.  
    16.     private int port = 5701;
    17.  
    18.     private int hostId;
    19.     private int webHostId;
    20.  
    21.     private int reliableChannel;
    22.     private int unreliableChannel;
    23.  
    24.     private int clientId;
    25.     private int connectionId;
    26.  
    27.     private float connectionTime;
    28.     private bool isConnected = false;
    29.     private bool isStarted = false;
    30.     private byte error;
    31.  
    32.     public Coachee CoacheeData { get; set; }
    33.  
    34.     private void Awake()
    35.     {
    36.         LanDiscovery = GetComponent<LANDiscovery>();
    37.     }
    38.    
    39.     private void Update()
    40.     {
    41.         if (!isConnected) return;
    42.  
    43.         int recHostId;
    44.         int connectionId;
    45.         int channelId;
    46.         byte[] recBuffer = new byte[1024];
    47.         int bufferSize = 1024;
    48.         int dataSize;
    49.         byte error;
    50.         NetworkEventType recData = NetworkTransport.Receive(out recHostId, out connectionId, out channelId, recBuffer, bufferSize, out dataSize, out error);
    51.  
    52.         switch (recData)
    53.         {
    54.             case NetworkEventType.Nothing:         //1
    55.                 break;
    56.             case NetworkEventType.ConnectEvent:    //2
    57.                 break;
    58.             case NetworkEventType.DataEvent:       //3
    59.                 string msg = Encoding.Unicode.GetString(recBuffer, 0, dataSize);
    60.                 Debug.Log("Receiving: " + msg);
    61.                 string[] splitData = msg.Split('|');
    62.  
    63.                 switch (splitData[0])
    64.                 {
    65.                     case Helper.Server.Message.ASKCOACHEEDATA:
    66.                         OnAskData(splitData);
    67.                         break;
    68.  
    69.                     case Helper.Server.Message.SENDMESSAGE:
    70.                         OnReceiveServerMessage(splitData[1]);
    71.                         break;
    72.  
    73.                     default:
    74.                         Debug.Log("Invalid message received: " + msg);
    75.                         break;
    76.                 }
    77.                 break;
    78.             case NetworkEventType.DisconnectEvent: //4
    79.                 break;
    80.         }
    81.     }
    82.  
    83.     public void ConnectTo(string ipAddress, Coachee coacheeData)
    84.     {
    85.         if (coacheeData == null || coacheeData.Name == string.Empty)
    86.         {
    87.             Debug.Log("No CoacheeData/name was provided, aborting connection...");
    88.             return;
    89.         }
    90.  
    91.         // Starting connection
    92.         NetworkTransport.Init();
    93.  
    94.         // Same config than on server
    95.         ConnectionConfig cc = new ConnectionConfig();
    96.         reliableChannel = cc.AddChannel(QosType.Reliable);
    97.         unreliableChannel = cc.AddChannel(QosType.Unreliable);
    98.  
    99.         HostTopology topology = new HostTopology(cc, MAX_CONNECTIONS);
    100.  
    101.         // Different signature?
    102.         hostId = NetworkTransport.AddHost(topology, 0);
    103.  
    104.         connectionId = NetworkTransport.Connect(hostId, ipAddress, port, 0, out error);
    105.  
    106.         connectionTime = Time.time;
    107.         isConnected = true;
    108.     }
    109.    
    110.     private void OnAskData(string[] data)
    111.     {
    112.         clientId = int.Parse(data[1]);
    113.  
    114.         string message = String.Format("{0}|{1}", Helper.Client.Message.COACHEEDATA, CoacheeData.Name);
    115.         Send(message, reliableChannel);
    116.     }
    117.  
    118.     private void OnReceiveServerMessage(string message)
    119.     {
    120.         if(OnServerMessageReceived != null)
    121.         {
    122.             OnServerMessageReceived(new OnServerMessageReceivedEvent(message));
    123.         }
    124.     }
    125.  
    126.     private void Send(string message, int channelId)
    127.     {
    128.         Debug.Log("Sending : " + message);
    129.         byte[] bmsg = Encoding.Unicode.GetBytes(message);
    130.         NetworkTransport.Send(hostId, connectionId, channelId, bmsg, message.Length * sizeof(char), out error);
    131.     }
    132.  
    133.     #endregion
    134.  
    135. }
    136.  
    Thing is, in the client - after connecting - it never seems to hit a NetworkEventType.DataEvent and I don't know why.

    Would one of you kind people enlighten the poor noob I am? I really need to get this done quick. :(

    Thanks in advance,
     
  2. ZoidbergForPresident

    ZoidbergForPresident

    Joined:
    Dec 15, 2015
    Posts:
    157
    OK more info.

    When debugging the server and the client joins, I don't get the debug.log about the server sending the message to the client... but it does once I kill the client.

    And the other way round when the client is supposed to send messages I get warnings in the console:
    "Attempt to send to not connected connection {1}"

    What gives? Where have I messed the configuration?
     
    Last edited: May 1, 2018
  3. ZoidbergForPresident

    ZoidbergForPresident

    Joined:
    Dec 15, 2015
    Posts:
    157
    Please, anyone?

    I've read in unity answers that we have to check for the connect event on the client too but what do I have to do there and what conditions do I have to check where to make this work?

    I'd be really grateful for anyone helping me out of this. :p
     
  4. ZoidbergForPresident

    ZoidbergForPresident

    Joined:
    Dec 15, 2015
    Posts:
    157
    fuiheqsuifhgsquufvs

    Never mind, I had some network related configuration "issues".

    It works.
     
  5. wechat_os_Qy0x8kV7OeiqTUgihI8CzJaUo

    wechat_os_Qy0x8kV7OeiqTUgihI8CzJaUo

    Joined:
    Dec 22, 2018
    Posts:
    2
    ???can you tell me how you fixed it...?
     
  6. ZoidbergForPresident

    ZoidbergForPresident

    Joined:
    Dec 15, 2015
    Posts:
    157
    Oh I think it was a simple matter of firewall. :p

    Try deactivationg firewalls on both ends.

    I guess you have to let the adequate ports opened.

    Good luck.