none
How To Receive Data From UDP Client? RRS feed

  • Question

  • Hello everyone, 

    I am working on a simple multiplayer game. I wrote a C++ server and I'm now trying to write the client in C# in the Unity game engine. I'm pretty new to networking. I'm a bit confuse on how this works in C#. I've got this so far, but it doesn't seem to work and throws an error: 

    IPEndPoint ep = new IPEndPoint(IPAddress.Parse(serverIPAddress), port); UdpClient udp = new UdpClient();        byte[] data = udp.Receive(ref ep);        string s = Encoding.ASCII.GetString(data, 0, data.Length);       

    print(s);

    The error:

    SocketException: An invalid argument was supplied.
    
    System.Net.Sockets.Socket.ReceiveFrom (System.Byte[] buffer, System.Int32 offset, System.Int32 size, System.Net.Sockets.SocketFlags socketFlags, System.Net.EndPoint& remoteEP) (at <735c83e43c004ee998f77c68cf8035da>:0)
    System.Net.Sockets.Socket.ReceiveFrom (System.Byte[] buffer, System.Int32 size, System.Net.Sockets.SocketFlags socketFlags, System.Net.EndPoint& remoteEP) (at <735c83e43c004ee998f77c68cf8035da>:0)
    System.Net.Sockets.UdpClient.Receive (System.Net.IPEndPoint& remoteEP) (at <735c83e43c004ee998f77c68cf8035da>:0)
    client.Update () (at Assets/client.cs:35)
    
    Thanks in advance!

    Tuesday, November 12, 2019 9:44 PM

Answers

  • Hi Yexx, 

    Thank you for posting here.

    For your question, you want to how to receive data from UDP client.

    I make a simple test on my side, and you can refer to the following code.

    Client:

                var client = new UdpClient();
                IPEndPoint ep = new IPEndPoint(IPAddress.Parse("ip address"), 11000); // endpoint where server is listening
                client.Connect(ep);
                byte[] bytes = Encoding.ASCII.GetBytes("Hello!");
                client.Send(bytes, bytes.Length);
    
                var receivedData = client.Receive(ref ep);
    
                Console.Write("Client receive data: " + Encoding.ASCII.GetString(receivedData, 0, receivedData.Length));
    
                Console.Read();
                Console.ReadLine();
    

    Server:

                UdpClient udpServer = new UdpClient(11000);
    
                while (true)
                {
                    var remoteEP = new IPEndPoint(IPAddress.Any, 11000);
                    var data = udpServer.Receive(ref remoteEP); // listen on port 11000
                    Console.Write("Server receive data :"+ Encoding.ASCII.GetString(data, 0, data.Length));
                    byte[] bt = Encoding.ASCII.GetBytes("hi");
                    udpServer.Send(bt, bt.Length, remoteEP); // reply back
                }
    

    Result of my test:

    Hope it can help you.

    Best Regards,

    Xingyu Zhao




    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, November 13, 2019 3:02 AM
    Moderator
  • UDP is probably not what you want to use.  The philosophy is somewhat unusual.  You can't listen to a specific IP address.  Instead, the IP address you provide tells the socket which of YOUR network interfaces it should listen to.  You can only have one app at a time listen on a specific UDP port, and that app will receive all traffic sent to that port.  The sending end is really the "client", and it has to send to a specific IP address.  That's usually not what you want for a client/server thing.

    You almost certainly want to use TCP, where the server can listen for arbitrary connections, and the client ends connect to the server's IP.


    Tim Roberts | Driver MVP Emeritus | Providenza &amp; Boekelheide, Inc.

    Wednesday, November 13, 2019 7:49 AM

All replies

  • Hi Yexx, 

    Thank you for posting here.

    For your question, you want to how to receive data from UDP client.

    I make a simple test on my side, and you can refer to the following code.

    Client:

                var client = new UdpClient();
                IPEndPoint ep = new IPEndPoint(IPAddress.Parse("ip address"), 11000); // endpoint where server is listening
                client.Connect(ep);
                byte[] bytes = Encoding.ASCII.GetBytes("Hello!");
                client.Send(bytes, bytes.Length);
    
                var receivedData = client.Receive(ref ep);
    
                Console.Write("Client receive data: " + Encoding.ASCII.GetString(receivedData, 0, receivedData.Length));
    
                Console.Read();
                Console.ReadLine();
    

    Server:

                UdpClient udpServer = new UdpClient(11000);
    
                while (true)
                {
                    var remoteEP = new IPEndPoint(IPAddress.Any, 11000);
                    var data = udpServer.Receive(ref remoteEP); // listen on port 11000
                    Console.Write("Server receive data :"+ Encoding.ASCII.GetString(data, 0, data.Length));
                    byte[] bt = Encoding.ASCII.GetBytes("hi");
                    udpServer.Send(bt, bt.Length, remoteEP); // reply back
                }
    

    Result of my test:

    Hope it can help you.

    Best Regards,

    Xingyu Zhao




    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, November 13, 2019 3:02 AM
    Moderator
  • UDP is probably not what you want to use.  The philosophy is somewhat unusual.  You can't listen to a specific IP address.  Instead, the IP address you provide tells the socket which of YOUR network interfaces it should listen to.  You can only have one app at a time listen on a specific UDP port, and that app will receive all traffic sent to that port.  The sending end is really the "client", and it has to send to a specific IP address.  That's usually not what you want for a client/server thing.

    You almost certainly want to use TCP, where the server can listen for arbitrary connections, and the client ends connect to the server's IP.


    Tim Roberts | Driver MVP Emeritus | Providenza &amp; Boekelheide, Inc.

    Wednesday, November 13, 2019 7:49 AM