English 中文(简体)
同一过程中发放和接收UDP包
原标题:Sending and receiving UDP packet from same process
  • 时间:2012-01-13 03:10:20
  •  标签:
  • c
  • sockets

这里是我迄今为止所做的工作(追踪错误):

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

在座标一中,让本组织对一个随机港口进行约束,然后我用<条码>获取。 如果我使用固定港口,则取消对<条码>植被名的呼吁。 之后,它运作。

PS:一毫升机。

最佳回答

页: 1 当你抓获了代办的港口时。 在我结束的这段话中(我做了几件小事,使《守则》更加简洁):

#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
", got, strerror(errno));

  return 0;
}
问题回答

<代码>port = sss.sin_port 应给网络定购港号。 当你指定港口<代码>dest.sin_port = htons(port)时,你将<代码>htons(<>htons(>)应用于一个已经按网络顺序排列的短程。 使用<代码>dest.sin_port = Port,一切均应罚款。

或者,如果您希望从<条码>中获取一个东道国的港口号码,则请使用<条码>。

getsockname(s, (struct sockaddr*) &ss, &len);
port = ntohs(ss.sin_port);
/*...*/
dest.sin_port = htons(port);




相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签