连接两个UDP客户到一个端口

连接两个UDP客户到一个端口

本文介绍了连接两个UDP客户到一个端口(发送和接收)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想建议从很少成功。



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



下面是我的代码:

 静态无效的主要(字串[] args)
{

IPEndPoint localpt =新IPEndPoint(IPAddress.Any,6000);

UdpClient udpServer =新的UdpClient(localpt);
udpServer.Client.SetSocketOption(
SocketOptionLevel.Socket,SocketOptionName.ReuseAddress,真正的);

UdpClient udpServer2 =新UdpClient();
udpServer2.Client.SetSocketOption(
SocketOptionLevel.Socket,SocketOptionName.ReuseAddress,真正的);

udpServer2.Client.Bind(localpt); //<<异常----------这里
}


解决方案

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

 静态无效的主要(字串[] args)
{
IPEndPoint localpt =新IPEndPoint(IPAddress.Any,6000);

UdpClient udpServer =新UdpClient();
udpServer.Client.SetSocketOption(
SocketOptionLevel.Socket,SocketOptionName.ReuseAddress,真正的);
udpServer.Client.Bind(localpt);

UdpClient udpServer2 =新UdpClient();
udpServer2.Client.SetSocketOption(
SocketOptionLevel.Socket,SocketOptionName.ReuseAddress,真正的);

udpServer2.Client.Bind(localpt); //<< ----------也不例外这里

Console.WriteLine(完了。);
到Console.ReadLine();
}

或更能说明问题的例子:

 静态无效的主要(字串[] args)
{
IPEndPoint localpt =新IPEndPoint(IPAddress.Loopback,6000);

ThreadPool.QueueUserWorkItem(代表
{
UdpClient udpServer =新UdpClient();
udpServer.ExclusiveAddressUse = FALSE;
udpServer.Client.SetSocketOption( SocketOptionLevel.Socket,SocketOptionName.ReuseAddress,真);
udpServer.Client.Bind(localpt);

IPEndPoint inEndPoint =新IPEndPoint(IPAddress.Any,0);
控制台.WriteLine(听的+ localpt +。);
字节[]缓冲= udpServer.Receive(REF inEndPoint);
Console.WriteLine(从接收+ inEndPoint ++ Encoding.ASCII.GetString(缓冲液)+。);
});

Thread.sleep代码(1000);

UdpClient udpServer2 =新UdpClient();
udpServer2.ExclusiveAddressUse = FALSE;
udpServer2.Client.SetSocketOption(SocketOptionLevel.Socket,SocketOptionName.ReuseAddress,真正的);
udpServer2.Client.Bind(localpt);

udpServer2.Send(新的byte [] {}×41,1,localpt);

Console.Read();
}


I tried the suggestion from this question with very little success.

Please... any help will be greatly appreciated!

Here is my code:

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
}
解决方案

You have to set the socket option before binding.

    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();
    }

Or a more illustrative example:

    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();
    }

这篇关于连接两个UDP客户到一个端口(发送和接收)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-14 01:13