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

Client unable to send message.

Discussion in 'Multiplayer' started by Epic-Username, Jul 18, 2017.

  1. Epic-Username

    Epic-Username

    Joined:
    May 24, 2015
    Posts:
    339
    When i send a network message through the LLAPI either the server does not receive the message or the client does not send it, i get the following warning message on the clients console:
    The server receives the connection event and the client creates a connection with the server, so i have no idea whats going on I've been researching for a while now but i cant find anyone else having this issue.
    Here is my code:

    ---Server---
    Code (CSharp):
    1.  
    2.     int connectionID;
    3.     int maxConnections = 20;
    4.     int reliableChannelID;
    5.     int hostID;
    6.     int socketPort = 8888;
    7.     byte error;
    8.  
    9.     void Start ()
    10.     {
    11.         NetworkTransport.Init ();
    12.         ConnectionConfig config = new ConnectionConfig ();
    13.         reliableChannelID = config.AddChannel (QosType.Reliable);
    14.         HostTopology topology = new HostTopology (config, maxConnections);
    15.         hostID = NetworkTransport.AddHost (topology, socketPort);
    16.     }
    17.  
    18.     void Update ()
    19.     {
    20.         int recHostID;
    21.         int recConnectionID;
    22.         int recChannelID;
    23.         byte[] recBuffer = new byte[1024];
    24.         int bufferSize = 1024;
    25.         int dataSize;
    26.         NetworkEventType recNetworkEvent = NetworkTransport.Receive (out recHostID, out recConnectionID, out recChannelID, recBuffer, bufferSize, out dataSize, out error);
    27.  
    28.         switch (recNetworkEvent) {
    29.  
    30.         case NetworkEventType.ConnectEvent:
    31.             print ("connectionRecieved");
    32.             break;
    33.  
    34.         case NetworkEventType.DataEvent:
    35.             string message = Encoding.Unicode.GetString (recBuffer, 0, dataSize);
    36.             print ("Recieved: " + message);
    37.             break;
    38.  
    39.         case NetworkEventType.DisconnectEvent:
    40.             break;
    41.  
    42.         case NetworkEventType.Nothing:
    43.             break;
    44.  
    45.         }
    46.  
    47.     }
    48.  
    49.  
    ---Client---

    Code (CSharp):
    1. int connectionID;
    2.     int maxConnections = 20;
    3.     int reliableChannelID;
    4.     int hostID;
    5.     int socketPort = 8889;
    6.     byte error;
    7.  
    8.     void Start ()
    9.     {
    10.         NetworkTransport.Init ();
    11.     }
    12.  
    13.     public void Connect ()
    14.     {
    15.         ConnectionConfig config = new ConnectionConfig ();
    16.         reliableChannelID = config.AddChannel (QosType.Reliable);
    17.         HostTopology topology = new HostTopology (config, maxConnections);
    18.         hostID = NetworkTransport.AddHost (topology, socketPort);
    19.  
    20.         connectionID = NetworkTransport.Connect (hostID, "127.0.0.1", 8888, 0, out error);
    21.         print ("connected, connectionID: " + connectionID);
    22.     }
    23.  
    24.     public void Disconnect ()
    25.     {
    26.         NetworkTransport.Disconnect (hostID, connectionID, out error);
    27.     }
    28.  
    29.     public void sendMsg (string message)
    30.     {
    31.         byte[] buffer = Encoding.Unicode.GetBytes (message);
    32.  
    33.         NetworkTransport.Send (hostID, connectionID, reliableChannelID, buffer, message.Length * sizeof(char), out error);
    34.     }
     
  2. Epic-Username

    Epic-Username

    Joined:
    May 24, 2015
    Posts:
    339
    I found out what the problem is: the client doesn't know its connected because it has to have this code as well otherwise it doesn't know it's connected.
    Code (CSharp):
    1. int recHostID;
    2.         int recConnectionID;
    3.         int recChannelID;
    4.         byte[] recBuffer = new byte[1024];
    5.         int bufferSize = 1024;
    6.         int dataSize;
    7.         NetworkEventType recNetworkEvent = NetworkTransport.Receive (out recHostID, out recConnectionID, out recChannelID, recBuffer, bufferSize, out dataSize, out error);
    8.         switch (recNetworkEvent) {
    9.         case NetworkEventType.ConnectEvent:
    10.             print ("connectionRecieved");
    11.             break;
    12.         case NetworkEventType.DataEvent:
    13.             string message = Encoding.Unicode.GetString (recBuffer, 0, dataSize);
    14.             print ("Recieved: " + message);
    15.             break;
    16.         case NetworkEventType.DisconnectEvent:
    17.             break;
    18.             break;
     
    aabramychev likes this.
  3. Epic-Username

    Epic-Username

    Joined:
    May 24, 2015
    Posts:
    339
    I have a similar problem to above but with the server: i'm unable to send data from the server to the client maybe it's because in the server it's not setting a connection ID? How can i send data from server to clients if the server doesn't have an connection ID.
     
  4. aabramychev

    aabramychev

    Unity Technologies

    Joined:
    Jul 17, 2012
    Posts:
    574
    Connect request is just polite request for connection establishing. You don't know will this request successful or not.
    So,
    client:
    connectionId = ...Connect();
    event = Receive(... receivedConnectionId...);
    if event == ConnectEvent && connectionId == receivedConnectionId
    Only AFTER THAT I CAN SEND DATA.

    Server:
    event = Receive(... receivedConnectionId...);
    if event == ConnectEvent
    WE RECEIVE NEW CONNECTION, new connectionid = receivedConnectionId
    NOW WE CAN SEND DATA TO receivedConnectionId

    was i clean?
     
  5. Epic-Username

    Epic-Username

    Joined:
    May 24, 2015
    Posts:
    339

    So the server uses the clients id to send a message to them?
    Code (CSharp):
    1.  
    2.         NetworkTransport.Send (hostID, targetID, reliableChannelID, buffer, message.Length * sizeof(char), out error);
    3.  
    Where targetID is a clients connection ID.
     
  6. aabramychev

    aabramychev

    Unity Technologies

    Joined:
    Jul 17, 2012
    Posts:
    574
    @Epic-Username, hm, no :)
    I guess the best approach will be, PM me with your project and I will take a look, ok?