2016-02-09 132 views
1

我試圖從筆記本電腦發送數據到在端口30718上通信的UDP服務器。數據發送是正確的,但是當我想要接收響應時,程序在UdpClient .receive()方法。我試圖用wireshark嗅探網絡,似乎響應正確傳遞,但c#不會收到它。 這裏是我的代碼:C#UdpClient.receive()沒有收到對廣播請求的響應

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 
    } 

這裏是從Wireshark的輸出(第二張圖片是響應廣播請求): wireshark1

wireshark2

有誰有解決這個問題的解決方案? 謝謝。

+0

,如果您首先檢查搜索引擎搜索,它可能是這個職位的副本: http://stackoverflow.com/questions/24363729/c-sharp-application-not-receiving-packets-on -udpclient-receive – icbytes

+0

你不需要調用'Connect',這裏是一個如何通過UDP進行廣播的簡單示例,https://github.com/lextm/sharpsnmplib/blob/master/SharpSnmpLib/Messaging/Discoverer。 CS –

回答