以下是我到目前为止(剥离错误检查):
struct sockaddr_in addr, ss, dest;
int port, s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
fcntl(s, F_SETFL, fcntl(s, F_GETFL, 0) | O_NONBLOCK);
memset((char*) &addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
inet_aton("127.0.0.1", &addr.sin_addr);
bind(s, (struct sockaddr*) &addr, sizeof(addr));
unsigned int len = sizeof(ss);
getsockname(s, (struct sockaddr*) &ss, &len);
port = ss.sin_port;
memset((char*) &dest, 0, sizeof(dest));
dest.sin_family = AF_INET;
dest.sin_port = htons(port);
inet_aton("127.0.0.1", &dest.sin_addr);
sendto(s, "test", 5, 0, (struct sockaddr*) &dest, sizeof(dest));
char buf[5];
recv(s, buf, 5, 0);
最后一句话以消息
Resource temporarily unavailable
失败(因为O_NONBLOCK
标志)。在代码片段中,我让操作系统绑定一个随机端口,然后使用
getsockname
获取它。如果我改为使用固定端口并删除对getsockname
的调用,则它可以工作。PS:我在linux机器上。
最佳答案
当您捕获操作系统分配的端口时,忘记使用ntohs
。以下是我的结论(我做了一些小事情,使代码更简洁):
#include <netinet/in.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
int main()
{
struct sockaddr_in addr = {}, ss, dest = {};
int port, s = socket(AF_INET, SOCK_DGRAM | SOCK_NONBLOCK, 0);
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
bind(s, (struct sockaddr*) &addr, sizeof(addr));
unsigned int len = sizeof(ss);
getsockname(s, (struct sockaddr*) &ss, &len);
port = ntohs(ss.sin_port);
dest.sin_family = AF_INET;
dest.sin_port = htons(port);
dest.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
sendto(s, "test", 5, 0, (struct sockaddr*) &dest, sizeof(dest));
char buf[5];
int got = recv(s, buf, 5, 0);
printf("got: %d, errno: %s\n", got, strerror(errno));
return 0;
}