【发布时间】:2012-02-25 13:31:03
【问题描述】:
我尝试了this question 的建议,但收效甚微。
请...任何帮助将不胜感激!
这是我的代码:
static void Main(string[] args)
{
IPEndPoint localpt = new IPEndPoint(IPAddress.Any, 6000);
UdpClient udpServer = new UdpClient(localpt);
udpServer.Client.SetSocketOption(
SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
UdpClient udpServer2 = new UdpClient();
udpServer2.Client.SetSocketOption(
SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
udpServer2.Client.Bind(localpt); // <<---------- Exception here
}
【问题讨论】:
-
你遇到了什么异常?
-
@M.Babcock 我收到的异常消息是:“试图以访问权限禁止的方式访问套接字”
-
您的
udpServer实例在尝试绑定时是否会抛出相同的异常? -
似乎您没有遵循问题链接中提供的相同代码示例。我看到 IPAddress.Any, 6000 但是 UdpClient udpServer2 = new UdpClient(5000) ;你为什么不在 Try{} catch{} 中包装这样的东西
-
@M.Babcock 如果我没记错的话,它在使用端点构造时已经绑定。无论如何,我尝试这样做: udpServer.Client.Bind(localpt);在设置套接字选项后立即收到不同的异常消息:“提供了无效参数”
标签: c# port reusability udpclient