Sending and receiving UDP packets between two programs on the same computer

后端 未结 8 1764
慢半拍i
慢半拍i 2020-12-02 12:37

Is it possible to get two separate programs to communicate on the same computer (one-way only) over UDP through localhost/127... by sharing the same port #?

We\'re

8条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-02 12:49

    Even changing your code so that I can pass in an IP address I gets the same error message it appears that you can't bind to the same port and only one port can be used here is the sample code I used your example and Altered it to capture my ip from my local machine.. 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
    

    this will produce the exception on the Bind () method.. sorry.

提交回复
热议问题