UdpClient.() vs UdpClient(port)
本文关键字:UdpClient port vs | 更新日期: 2023-09-27 18:08:48
我有一个小问题和一个解决方案,但我想了解到底发生了什么。根据UdpClient的构造函数,我使用EndReceive()抛出一个套接字异常或不(10054 -一个现有的连接被远程主机关闭)
谢谢你的提示!文森特
private UdpClient udpClient;
void recv(IAsyncResult res)
{
IPEndPoint remote = new IPEndPoint(IPAddress.Any, 0);
byte[] data = udpClient.EndReceive(res, ref remote); // code stops here in debug
// do something with data received from remote
udpClient.BeginReceive(new AsyncCallback(recv), null);
}
private void Form1_Load(object sender, EventArgs e)
{
//udpClient = new UdpClient(); // will throw exception
udpClient = new UdpClient(8000); // will not throw exception
udpClient.Connect("127.0.0.1", 8000);
udpClient.BeginReceive(new AsyncCallback(recv), null);
}
private void button1_Click(object sender, EventArgs e)
{
udpClient.Send(Encoding.ASCII.GetBytes("Hallo"), 5);
}
case 1: udpClient = new udpClient() -您将套接字绑定到随机本地端口
案例2:udpClient = new udpClient(8000) -你绑定套接字到本地端口8000
也许在情况下1远程主机尝试发送到随机端口和防火墙拒绝?