套接字不接收任何数据包

时间:2017-08-30 00:52:09

标签: c# networking

我正在建立一个连接到服务器的客户端,然后等待服务器发送UDP数据包。每当服务器发送udp数据包时,客户端(I)需要发送回我收到的相同数据包。我启动了我的服务器,使用wpe捕获数据包,这就是它的样子:

49  0.0.0.0:10052  :0  9  SendTo   0000  A9 84 09 0A 00 AC 48 C4 D3    ......H..

50  :0  0.0.0.0:10052  9  RecvFrom   0000  A9 84 09 0A 00 AC 48 C4 D3  ......H..

服务器托管在端口10052上。所以我尝试的是:

 Socket jj2ClientUDP = new Socket(AddressFamily.InterNetwork,
 SocketType.Dgram, ProtocolType.Udp)

private void button1_Click(object sender, EventArgs e)
        {
     ep = new IPEndPoint(IPAddress.Parse(ServerIP), 10052);
     jj2ClientUDP.Connect(ep);
     jj2ClientUDP.BeginReceive(buffer1, 0, buffer1.Length, SocketFlags.None, new AsyncCallback(received), jj2ClientUDP);
}

private void received(IAsyncResult ar)
        {
            byte[] receivedData = new byte[1500];
            receivedData = (byte[])ar.AsyncState;
            jj2ClientUDP.SendTo(receivedData, ep);
            byte[] buffer2 = new byte[1500];
            jj2ClientUDP.BeginReceiveFrom(buffer2, 0, buffer2.Length, 0, ref ep, new AsyncCallback(received), jj2ClientUDP);
            MessageBox.Show("Received and sent back.");
            }

我做错了什么,因为它没有发回数据包。消息框没有出现。请帮忙。 先感谢您。
如果我将MessageBox放在收到的开头,那么也没有任何事情发生。

0 个答案:

没有答案