UdpClient.() 与 UdpClient(端口)
我有一个小问题和一个解决方案,但我想了解到底发生了什么。 根据 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);
}
I have a little problem and a Solution, but i would like to understand whats happening exactly.
Depending on the constructor of UdpClient I use EndReceive() throws a Socket Exeption or not( 10054 - An existing connection got closed by the remote host (translated from german))
thanks for any hint!
Vincent
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);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
情况 1:udpClient = new UdpClient() - 将套接字绑定到随机本地端口
情况 2:udpClient = new UdpClient(8000) - 您将套接字绑定到本地端口 8000
也许在情况 1 远程主机尝试发送到随机端口并被防火墙拒绝?
case 1: udpClient = new UdpClient() - you binding socket to random local port
case 2: udpClient = new UdpClient(8000) - you binding socket to local port 8000
Maybe in case 1 remote host try send to random port and rejected by firewall?