简体   繁体   中英

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).

In the snippet I let the OS to bind a random port, and then I obtain it with getsockname . If I use a fixed port instead and remove the call to getsockname then it works.

PS: I'm on a linux machine.

port = ss.sin_port should give network ordered port number. When you assign port with dest.sin_port = htons(port) you are applying htons() to a short which is already in network byte order. Use dest.sin_port = port instead and everything should be fine.

Alternatively, if you want to get a host-ordered port number from getsockname() result, you should use 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. 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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM