将两个UDP客户端连接到一个端口(发送和接收)

我尝试了这个问题的建议但收效甚微。

请…任何帮助将不胜感激!

这是我的代码:

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 } 

您必须在绑定之前设置套接字选项。

  static void Main(string[] args) { IPEndPoint localpt = new IPEndPoint(IPAddress.Any, 6000); UdpClient udpServer = new UdpClient(); udpServer.Client.SetSocketOption( SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true); udpServer.Client.Bind(localpt); UdpClient udpServer2 = new UdpClient(); udpServer2.Client.SetSocketOption( SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true); udpServer2.Client.Bind(localpt); // <<---------- No Exception here Console.WriteLine("Finished."); Console.ReadLine(); } 

或者更具说明性的例子:

  static void Main(string[] args) { IPEndPoint localpt = new IPEndPoint(IPAddress.Loopback, 6000); ThreadPool.QueueUserWorkItem(delegate { UdpClient udpServer = new UdpClient(); udpServer.ExclusiveAddressUse = false; udpServer.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true); udpServer.Client.Bind(localpt); IPEndPoint inEndPoint = new IPEndPoint(IPAddress.Any, 0); Console.WriteLine("Listening on " + localpt + "."); byte[] buffer = udpServer.Receive(ref inEndPoint); Console.WriteLine("Receive from " + inEndPoint + " " + Encoding.ASCII.GetString(buffer) + "."); }); Thread.Sleep(1000); UdpClient udpServer2 = new UdpClient(); udpServer2.ExclusiveAddressUse = false; udpServer2.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true); udpServer2.Client.Bind(localpt); udpServer2.Send(new byte[] { 0x41 }, 1, localpt); Console.Read(); } 

我查找了您的错误消息,这解释了错误是什么以及它为什么会发生。

以下是确切的错误消息和原因WSAEACCES 10013( MSDN )

没有权限。

尝试以其访问权限禁止的方式访问套接字。 一个例子是使用sendto的广播地址而没有使用setsockopt(SO_BROADCAST)设置广播权限。

WSAEACCES错误的另一个可能原因是,当调用绑定函数时(在带有SP4及更高版本的Windows NT 4.0上),另一个应用程序,服务或内核模式驱动程序绑定到具有独占访问权限的同一地址。 这种独占访问是带有SP4及更高版本的Windows NT 4.0的新function,并通过使用SO_EXCLUSIVEADDRUSE选项实现。

即使更改你的代码,以便我可以传入一个IP地址我得到相同的错误消息,看起来你不能绑定到同一个端口,这里只能使用一个端口是我用你的例子和改变它的示例代码从我的本地机器捕获我的IP ..

  IPAddress ipAddress = Dns.Resolve(Dns.GetHostName()).AddressList[0]; IPEndPoint ipLocalEndPoint = new IPEndPoint(ipAddress, 11000); //IPEndPoint localpt = new IPEndPoint(ipLocalEndPoint); UdpClient udpServer = new UdpClient(ipLocalEndPoint); udpServer.Client.SetSocketOption( SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true); udpServer.Connect(ipLocalEndPoint); UdpClient udpServer2 = new UdpClient(); udpServer2.Client.SetSocketOption( SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true); udpServer2.Client.Bind(ipLocalEndPoint); // <<---------- Exception here 

这会在Bind()方法上产生exception..抱歉。

要在UDP应用程序中解决WSAEACCESS 10013( MSDN )exception,您可以尝试

 udpServer.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, true);