我有交流程序。它首先尝试将 UDP 消息发送到环回地址,然后从环回中读取。但首先 sendto() 函数失败并显示消息“ sendto failed: Invalid argument ”。代码如下所示:
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdio.h>
int main(void)
{
struct sockaddr_in servaddr;
int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
bzero(&servaddr, sizeof(servaddr));
struct in_addr addr;
char dottedaddr[20];
inet_aton("127.0.0.1", &addr);
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = addr.s_addr;
servaddr.sin_port = htons(0);
struct sockaddr_in cliaddr;
inet_aton("192.168.2.12", &addr);
cliaddr.sin_family = AF_INET;
cliaddr.sin_addr.s_addr = addr.s_addr;
cliaddr.sin_port = htons(5000);
if(bind(sockfd, (const struct sockaddr *)&cliaddr, sizeof(cliaddr)) == -1)
{
perror("bind failed");
exit(1);
}
char buf[] = {'h', 'i', ' ', 'i', ' ', 'l', 'o', 'v', 'e', ' ', 'y', 'o', 'u', 0};
if( sendto(sockfd, buf, sizeof(buf), 0, (const struct sockaddr *)&servaddr, sizeof(servaddr)) == -1)
{
perror("sendto fails");
exit(2);
}
fd_set readFd;
FD_ZERO(&readFd);
FD_SET(sockfd, &readFd);
struct timeval timeout;
timeout.tv_sec = 5;
timeout.tv_usec = 0;
int ret = select(sockfd + 1, &readFd, NULL, NULL, &timeout);
if(ret > 0)
{
if(FD_ISSET(sockfd, &readFd))
{
char buf2[21];
struct sockaddr_in from;
int len = sizeof(from);
if(recvfrom(sockfd, buf2, sizeof(buf2), 0, (struct sockaddr *)&from, &len) == -1)
{
perror("recvfrom fails");
}
}
}
else if (ret == 0)
{
printf("select time out \n");
}
else
{
printf("select fails");
}
}
如果我将服务器端口从 0 更改为 5000,则 sendto() 可以成功。是什么原因 ?
第二个问题是,服务器端口改为5000后,select()无法检测到socket是否可读。它只是超时。我认为 sockfd 应该是可读的,因为我只是向环回地址发送一条消息。代码有什么问题吗?谢谢你!