Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

UNET LLAPI. How send messages from Server to Clients and receive them in Client Side?

Discussion in 'UNet' started by kami36, Aug 21, 2016.

  1. kami36

    kami36

    Joined:
    Mar 16, 2015
    Posts:
    2
    To send messages from client to server use NetworkTransport.Send() and use NetworkTransport.Receive() for receive them.
    But how I can do this conversely?
     
  2. buronix

    buronix

    Joined:
    Dec 11, 2013
    Posts:
    111
    First of all, you can do the same as the server but in the client, but then you need to have a direct connection (Port forwarding) and know the IP /Port of the client in the server side.

    Or if the client connected previously to the server, in the server side you listen with :

    Code (CSharp):
    1. NetworkTransport.Receive(out int hostId, out int connectionId, out int channelId, byte[] buffer, intbufferSize, out int receivedSize, out byte error);
    so you need to keep the connectionId of the client you want to send data and use it from the server with:

    Code (CSharp):
    1. NetworkTransport.Send(int hostId, int connectionId, int channelId, byte[] buffer, int size, out byte error);
    the hostId is the same that you created previously with :

    Code (CSharp):
    1.  
    2. ConnectionConfig Config = new ConnectionConfig();
    3. int ReliableChannelId = Config.AddChannel(QosType.ReliableSequenced);
    4. HostTopology Topology = new HostTopology(Config, 10);
    5. int hostId = NetworkTransport.AddHost(Topology, Port);
    then you need to receive in the client also the messages, for example with :

    Code (CSharp):
    1. NetworkTransport.ReceiveFromHost(int hostId, out int connectionId, out int channelId, byte[]buffer, int bufferSize, out int receivedSize, out byte error);
    You can use also Receive, but if you want to have a client/server in the same instance I recommend you to use ReceiveFromHost (or you will need to filter manually lot of messages and logic).

    Also you need the same channel in both of them, (if you connected from client to server I Imagine that this is working for you).
     
    Last edited: Aug 21, 2016
    kami36 likes this.
  3. kami36

    kami36

    Joined:
    Mar 16, 2015
    Posts:
    2
    Oh, thank you very much! Everything is works! Your post was very useful for me!
     
  4. buronix

    buronix

    Joined:
    Dec 11, 2013
    Posts:
    111
    I´m Glad to Help!