简体   繁体   中英

UDP Echo client/server in C#/C++ not returning message

I have a server written in C++ and a client written in C#. The client sends its packet to the server, the server receives it, then sends it back. Unfortunately, everything works EXCEPT when the server sends the message back. Here is the simple code:

// SERVER

    if ((recv_len = recvfrom(s, buf, sizeof(buf), 0, (struct sockaddr *) &si_sender, &si_senderSize)) == -1)
    {
        die("recvfrom()", s);
    }

    //print details of the client/peer and the data received
    printf("Received packet from %s:%d\n", inet_ntoa(si_sender.sin_addr), ntohs(si_sender.sin_port));

    std::cout << "Data: " << buf << std::endl;

    //now reply the client with the same data information

    if (sendto(s, buf, sizeof(buf), 0, (struct sockaddr*) &si_sender, si_senderSize) == -1)
    {
        die("sendto()", s);
    }

 //cleanup here

//CLIENT

    private void btnSearch_Click(object sender, EventArgs e)
    {
        bool found = false;
        byte[] text_to_send = Encoding.ASCII.GetBytes("networkinfo");
        client.Send(text_to_send, text_to_send.Length);

        IPEndPoint serverResponse = new IPEndPoint(IPAddress.Any, 0);

        while (!found)
        {
            if (client.Available > 0)
            {
                byte[] responseBuffer = client.Receive(ref serverResponse);
                string response = Encoding.ASCII.GetString(responseBuffer);
                writeIPAddress(serverResponse.Address.ToString(), serverResponse.Port.ToString());
                found = true;
            }
        }

The server does not fail on the sendto() function, but the while loop in the client polls forever because it is not detecting information to receive on the network, but the sendto() function didn't return error?

Any ideas to where the network data is??

Ok, i figured it out finally and I want to post the resolution to the problem so other people won't have this headache.

The problem was broadcasting a UDP packet with UDPClient. You CANNOT receive with the same UDPClient object after you broadcast a packet (using ipaddress.broadcast or "255.255.255.255"). The solution I ran into was to immediately close this object, open another one on the same port, then use that new object to receive the response.

//setup broadcast client
broadcastClient = new UdpClient(5454);
broadcastAddress = new IPEndPoint(IPAddress.Broadcast, 8888);
broadcastClient.Connect(broadcastAddress);

//send and immediately close
byte[] text_to_send = Encoding.ASCII.GetBytes("networkinfo");
broadcastClient.Send(text_to_send, text_to_send.Length);
broadcastClient.Close();

//open up new client to receive
UdpClient receivingClient = new UdpClient(5454);
IPEndPoint serverResponse = new IPEndPoint(IPAddress.Any, 0);
byte[] message = receivingClient.Receive(ref serverResponse);
string messageReceived = Encoding.ASCII.GetString(message);

Debug.WriteLine(messageReceived);

I did not see anything about this quirk (problem?) on MSDN, so I'm not sure if this is a bug or it is as intended and just not documented.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM