Sockets 特定端口中两个程序之间的UDP发送和接收

Sockets 特定端口中两个程序之间的UDP发送和接收,sockets,udp,port,Sockets,Udp,Port,我有一个完整的程序,通过UDP协议进行通信。程序在ip为192.168.1.9的PC上运行。当我发送特定数据时,该程序会响应 发送代码: var client = new UdpClient(); IPEndPoint destination = new IPEndPoint(IPAddress.Parse("192.168.1.9"), 1531); IPAddress localIp = IPAddress.Parse("192.168.1.3"); IPEndPoint source =

我有一个完整的程序,通过UDP协议进行通信。程序在ip为192.168.1.9的PC上运行。当我发送特定数据时,该程序会响应

发送代码:

var client = new UdpClient();
IPEndPoint destination = new IPEndPoint(IPAddress.Parse("192.168.1.9"), 1531);
IPAddress localIp = IPAddress.Parse("192.168.1.3");
IPEndPoint source = new IPEndPoint(localIp, 1530);
client.Client.Bind(source);
client.Connect(destination);
byte[] send_buffer = { 170, 170, 0, 0, 1, 1, 86 };
client.Send(send_buffer, send_buffer.Length);
Wireshark捕获:

但我的应用程序没有检测到任何东西:

    UdpClient listener = new UdpClient(1530);

    IPAddress ip = IPAddress.Parse("192.168.1.3");
    IPEndPoint groupEP = new IPEndPoint(IPAddress.Any, 1530);

    byte[] receive_byte_array;

        while (!done)
        {
            Console.WriteLine("Waiting for broadcast");
            receive_byte_array = listener.Receive(ref groupEP);
        }

我需要捕获端口1530上从192.168.9到192.168.1.3的通信。

您的发送方绑定到端口1530上的本地IP
192.168.1.3
作为其源,然后将数据发送到端口1531上的远程IP
192.168.1.9
作为目标

您的接收器正在绑定到端口1530上的本地IP
0.0.0
,以接收数据,然后过滤掉未从远程端口1530发送的任何入站数据(实际上是)

数据未发送到接收器正在读取的端口。

要解决此问题,您需要:

  • 将接收器更改为绑定到端口
    1531
    ,而不是端口
    1530

    UdpClient listener = new UdpClient(1531);
    
  • 将发件人更改为将数据发送到端口
    1530
    ,而不是端口
    1531

    IPEndPoint destination = new IPEndPoint(IPAddress.Parse("192.168.1.9"), 1530);
    

  • 为什么要使用UDP进行进程间通信?那么,为什么要使用特定IP而不是使用在192.168.1.9上运行的
    127.0.0.1
    loopback IP?程序已经完成,无法更改。UDP通信已经实现。如果我使用127.0.0.1,应用程序也不会捕获任何内容。我必须通过端口1531发送,但应用程序在此端口侦听。如果我使用端口1530,应用程序将不响应。请再次阅读我的答案。发送方正在从端口1530发送到端口1531,但接收方没有监听端口1531。我告诉过你怎么解决的。