udp 源端口和目标端口必须匹配吗?

do udp source port and destination port have to match?

我正在尝试使用 c/c++ 学习套接字编程。我写了两个小的 udp 发件人。一个是有 bind()ing 的,另一个是没有 bind()ing 的。我在远程 IP 上构建了这两个程序。同时,我在本地系统上构建了一个小型 udp 接收器。我试图将 udp 消息从两个发件人程序发送到我的本地接收器。但是接收方只能通过 bind()ing 接收来自发送方的消息。并且它必须与目的地绑定在相同的端口号上。否则,即使使用 bind()ing,它仍然不起作用。 但是当我在没有绑定()的情况下将发件人程序移动到我的本地系统,并将消息发送到“127.0.0.1”时它起作用了。

所以对我来说,似乎在本地发送 udp 数据包时,源端口和目标端口可以是不同的数字。但是当从不同的IP地址发送udp数据包时,发送端口和接收端口必须有匹配的端口号。是吗?

这是我的udp发送程序:

#include <iostream>
#include <cstring>
#include <cerrno>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

using namespace std;

int main(int argc, char *argv[]){
    if(argc!=2){
        cout<<"need arg"<<endl;
        return 1;
    }
    int sfd = socket(AF_INET, SOCK_DGRAM, 0);
    struct sockaddr_in des;
    des.sin_family = AF_INET;
    des.sin_port = 9999;
    des.sin_addr.s_addr = inet_addr("my ip address"); //I used "127.0.0.1" when I tried it locally.

    /* this is all the difference between the sender with and without bind()
    struct sockaddr_in sai;
    sai.sin_family = AF_INET;
    sai.sin_port = 5001;
    sai.sin_addr.s_addr = INADDR_ANY;
    if(bind(sfd, (struct sockaddr *)&sai, sizeof sai)==-1){
        cout<<"bind:"<<strerror(errno)<<endl;
        _exit(1);
    }
    cout<<"binded successfully"<<endl;
    */

    int byt = sendto(sfd, argv[1], strlen(argv[1])+1, 0, (struct sockaddr *)&des, sizeof des);
    cout<<byt<<endl;
    if(byt<0){
        cout<<"sendto"<<strerror(errno)<<endl;
    }

    return 0;
}

So to me, it seems like sending udp packet locally the src port and dest port can be different numbers.

正确。

But when sending udp packet from a different IP address, the sending and receiving port have to have matching port number. Is that right?

没有。它没有。回复时您必须做的是确保使用通过 recvfrom() 返回的已接收数据报的源端口作为 sendto().

中回复数据报的目标端口