[英]Sending and receiving UDP packet from same process
Here is what I have so far (stripped error checking): 这是我到目前为止(剥离的错误检查):
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);
The last sentence fails with a message of Resource temporarily unavailable
(because of the O_NONBLOCK
flag). 最后一句失败,并显示一条消息“
Resource temporarily unavailable
(由于O_NONBLOCK
标志)。
In the snippet I let the OS to bind a random port, and then I obtain it with getsockname
. 在代码段中,我让操作系统绑定了一个随机端口,然后使用
getsockname
获取它。 If I use a fixed port instead and remove the call to getsockname
then it works. 如果我改用固定端口并删除对
getsockname
的调用,那么它将起作用。
PS: I'm on a linux machine. PS:我在Linux机器上。
port = ss.sin_port
should give network ordered port number. port = ss.sin_port
应该提供网络订购的端口号。 When you assign port with dest.sin_port = htons(port)
you are applying htons()
to a short which is already in network byte order. 当您使用
dest.sin_port = htons(port)
分配端口时,您会将htons()
应用于已经按网络字节顺序排列的short。 Use dest.sin_port = port
instead and everything should be fine. 使用
dest.sin_port = port
代替,一切应该没问题。
Alternatively, if you want to get a host-ordered port number from getsockname()
result, you should use ntohs()
: 另外,如果要从
getsockname()
结果中获取主机排序的端口号,则应使用ntohs()
:
getsockname(s, (struct sockaddr*) &ss, &len);
port = ntohs(ss.sin_port);
/*...*/
dest.sin_port = htons(port);
You forgot to use ntohs
when you captured the OS-assigned port. 捕获操作系统分配的端口时,您忘记使用
ntohs
。 Here's what I ended up with (I did a couple small things to make the code more concise too): 这就是我最终得到的结果(我做了几件事,使代码也更加简洁):
#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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.