1

I am trying to send data from my laptop to UDP server which communicates on port 30718. Data sending is correct, but when I want to receive response, program stucks on UdpClient.receive() method. i tried to sniff network with wireshark and it seem that response is delivered correctly, but c# will not receive it. Here is my code:

private void button1_Click(object sender, EventArgs e)
    {
        byte[] data = new byte[4];
        data[0] = 0x00;
        data[1] = 0x01;
        data[2] = 0x00;
        data[3] = 0xF6;

        byte[] rcvPacket = new Byte[1024];
        UdpClient client = new UdpClient();
        IPAddress address = IPAddress.Parse("255.255.255.255");
        client.EnableBroadcast = true;
        client.Connect(address, 30718);
        IPEndPoint remoteIPEndPoint = new IPEndPoint(IPAddress.Any, 0);


        client.Send(data, 4);
        rcvPacket = client.Receive(ref remoteIPEndPoint);

        string rcvData = Encoding.ASCII.GetString(rcvPacket);
        client.Close();  //close connection
    }

And here is output from wireshark (second picture is response to broadcast request): wireshark1

wireshark2

Does anybody have solution for fixing this problem? Thank you.

Matěj Řehák
  • 79
  • 3
  • 10
  • If You would check the searching engine first, it might be a duplicate of this post: http://stackoverflow.com/questions/24363729/c-sharp-application-not-receiving-packets-on-udpclient-receive – icbytes Feb 09 '16 at 14:36
  • You don't need to call `Connect` and here is a quick sample in how to broadcast via UDP, https://github.com/lextm/sharpsnmplib/blob/master/SharpSnmpLib/Messaging/Discoverer.cs – Lex Li Feb 09 '16 at 15:54

1 Answers1

0

Finally I found solution. This is good example of sending broadcasts and receiving responses. C# How to do Network discovery using UDP Broadcast

Community
  • 1
  • 1
Matěj Řehák
  • 79
  • 3
  • 10