我想学习用c/C++编写socket编程。我写了两个小的udp发送者。一个是绑定(),另一个没有绑定()。我在远程IP上构建了两个程序。同时,我在本地系统上构建了一个小型udp接收器。我试图从两个发件人程序发送udp消息到我的本地接收器。但是接收者只能通过bind()方法接收发件人的消息。它必须绑定在与目的地相同的端口号上。否则,即使使用bind(),它仍然不起作用。 但是,当我移动发件人程序没有绑定()到我的本地系统,并发送消息到“127.0.0.1”它的工作。做udp源端口和目标端口必须匹配吗?
所以对我来说,好像在本地发送udp数据包的src端口和dest端口可以是不同的数字。但是,当从不同的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;
}
不,我认为你对几个关键点感到困惑。这是一个很好的教程,可能会有所帮助:[用UDP套接字编程](https://www.cs.rutgers.edu/~pxk/417/notes/sockets/udp.html)。此链接进一步讨论[bind](http://linux.die.net/man/2/bind)的作用:http://stackoverflow.com/questions/3057029/do-i-have-to-bind- a-udp-socket-in-my-client-program-to-receive-data-i-always -g – paulsm4
每个UDP(和TCP)端点都由一个(地址,端口)*对*表征。这非常适合(机器,程序)。端口是单独一个端点的特性,而不是整个通信信道的特性,所以不需要在两端使用相同的端口。通常情况下,它不是。 –