Simple UDP example to send and receive data from same socket

前端 未结 3 521
太阳男子
太阳男子 2020-12-12 22:10

For some reason I am having a hard time sending and receiving data from the same socket. Anyways here is my client code:

var client = new UdpClient();
IPEndP         


        
相关标签:
3条回答
  • 2020-12-12 22:26

    (I presume you are aware that using UDP(User Datagram Protocol) does not guarantee delivery, checks for duplicates and congestion control and will just answer your question).

    In your server this line:

    var data = udpServer.Receive(ref groupEP);
    

    re-assigns groupEP from what you had to a the address you receive something on.

    This line:

    udpServer.Send(new byte[] { 1 }, 1); 
    

    Will not work since you have not specified who to send the data to. (It works on your client because you called connect which means send will always be sent to the end point you connected to, of course we don't want that on the server as we could have many clients). I would:

    UdpClient udpServer = new UdpClient(UDP_LISTEN_PORT);
    
    while (true)
    {
        var remoteEP = new IPEndPoint(IPAddress.Any, 11000);
        var data = udpServer.Receive(ref remoteEP);
        udpServer.Send(new byte[] { 1 }, 1, remoteEP); // if data is received reply letting the client know that we got his data          
    }
    

    Also if you have server and client on the same machine you should have them on different ports.

    0 讨论(0)
  • 2020-12-12 22:33

    here is my soln to define the remote and local port and then write out to a file the received data, put this all in a class of your choice with the correct imports

        static UdpClient sendClient = new UdpClient();
        static int localPort = 49999;
        static int remotePort = 49000;
        static IPEndPoint localEP = new IPEndPoint(IPAddress.Any, localPort);
        static IPEndPoint remoteEP = new IPEndPoint(IPAddress.Parse("127.0.0.1"), remotePort);
        static string logPath = System.AppDomain.CurrentDomain.BaseDirectory + "/recvd.txt";
        static System.IO.StreamWriter fw = new System.IO.StreamWriter(logPath, true);
    
    
        private static void initStuff()
        {
          
            fw.AutoFlush = true;
            sendClient.ExclusiveAddressUse = false;
            sendClient.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
            sendClient.Client.Bind(localEP);
            sendClient.BeginReceive(DataReceived, sendClient);
        }
    
        private static void DataReceived(IAsyncResult ar)
        {
            UdpClient c = (UdpClient)ar.AsyncState;
            IPEndPoint receivedIpEndPoint = new IPEndPoint(IPAddress.Any, 0);
            Byte[] receivedBytes = c.EndReceive(ar, ref receivedIpEndPoint);
            fw.WriteLine(DateTime.Now.ToString("HH:mm:ss.ff tt") +  " (" + receivedBytes.Length + " bytes)");
    
            c.BeginReceive(DataReceived, ar.AsyncState);
        }
    
    
        static void Main(string[] args)
        {
            initStuff();
            byte[] emptyByte = {};
            sendClient.Send(emptyByte, emptyByte.Length, remoteEP);
        }
    
    0 讨论(0)
  • 2020-12-12 22:45

    I'll try to keep this short, I've done this a few months ago for a game I was trying to build, it does a UDP "Client-Server" connection that acts like TCP, you can send (message) (message + object) using this. I've done some testing with it and it works just fine, feel free to modify it if needed.

    0 讨论(0)
提交回复
热议问题