用什么在recvfrom的的addrlen中字段()呢? [英] Whats the addrlen field in recvfrom() used for?

查看:315
本文介绍了用什么在recvfrom的的addrlen中字段()呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用我的程序,从我SRC_ADDR指定服务器获取数据DGRAM recvfrom的。但是,我不知道为什么我需要初始化并传递addrlen中。

I'm using recvfrom in my program to get DGRAM data from a server I specify in src_addr. However, I'm not sure why I need to initialize and pass in addrlen.

我读了手册页和我真的不明白它变得在

I read the man page and I didn't really understand what it's getting at.

如果SRC_ADDR不是NULL,和底层协议提供了源地址,源地址被填充。当
  SRC_ADDR为NULL,没有被填充;在这种情况下,addrlen中是不
  使用,也应为NULL。参数addrlen中是一个值结果参数,该方应前的初始化
  调用与SRC_ADDR相关联的缓冲器的大小,和
  改性在返回,以指示源地址的实际大小。返回的地址被截断如果所提供的缓冲区太小;
  在这种情况下,addrlen中会返回比供给到该呼叫的值。

If src_addr is not NULL, and the underlying protocol provides the source address, this source address is filled in. When src_addr is NULL, nothing is filled in; in this case, addrlen is not used, and should also be NULL. The argument addrlen is a value-result argument, which the caller should initialize before the call to the size of the buffer associated with src_addr, and modified on return to indicate the actual size of the source address. The returned address is truncated if the buffer provided is too small; in this case, addrlen will return a value greater than was supplied to the call.

我猜测,它有事情做与SRC_ADDR是IPv4或IPv6。这是正确的?

I'm guessing that it's got something to do with src_addr being ipv4 or ipv6. Is this correct?

谢谢!

推荐答案

也许有从你身边missinter pretation。谈到:

Maybe there is a missinterpretation from your side. Talking about:

ssize_t recvfrom(int sockfd, void *buf, size_t len, int flags, 
                 struct sockaddr *src_addr, socklen_t *addrlen);

SRC_ADDR 不可以用于手的的的ADRESS,你会喜欢听,而是你所提供的存储位置,以获得实际的源地址流传的退出

src_addr is not used to hand in the adress that you would like to listen to, but rather a storage location provided by you to get the actual source address handed out.

因此​​,如果您设置 SRC_ADDR 为NULL,因为您选择在地址都不感兴趣,你不必在意 addrlen中因为它不会反正使用。

Thus if you set src_addr to NULL because youre not interested in the address at all, you don't have to care about addrlen as it won't get used anyway.

如果你想了解的源地址被告知另一方面,你不仅必须提供一个存储位置,而且还告诉你所提供的存储位置有多大。
这就是为什么你应该初始化 * addr_len 来所分配的缓冲区大小。

If on the other hand you want to be informed about the source address, you not only have to provide a storage location, but also tell how big the storage location you provided is. Thats why you should initialize *addr_len to the buffer size you allocated.

您调用的值由 addrlen中指出,会告诉你关于你分配多少(如果有的话)的空间来存储源地址后得到了实际数据填充

After your call the value pointed to by addrlen will inform you about how much (if any) of the space you allocated to store the source address got actually filled with data.

与结构sockaddr和传球尺寸来回整个麻烦已经与该甚至thoug他们是最严重的网络中使用的插座的事实做是为了更加笼统的概​​念。

The whole hassle with struct sockaddr and passing sizes back and forth has to do with the fact that even thoug they're most heavily used in networking sockets were intended to be much more general concept.

想想UNIX域套接字作为一个例子,因为他们是通过他们需要一个adressing方案从来自基于IP的网络称为完全以不同的文件系统来实现。的sockaddr的这里使用的类型是:

Think about unix domain sockets as an an example as they are implemented via the filesystem they require an adressing scheme totaly different from that known from IP based networking. The type of sockaddr used here is:

struct sockaddr_un {
  sa_family_t sun_family;               /* AF_UNIX */
  sun_path[UNIX_PATH_MAX];  /* pathname */
};

比较这对基于IP网络使用的结构:

Compare this to the struct used in IP based networking:

struct sockaddr_in {
  sa_family_t    sin_family; /* address family: AF_INET */
  in_port_t      sin_port;   /* port in network byte order */
  struct in_addr sin_addr;   /* internet address */
};

应当清楚既没有太多的共同之处。

it should be clear both don't have too much in common.

插座被设计成能够适应这两种情况。

sockets were designed to be able to fit both cases.

这篇关于用什么在recvfrom的的addrlen中字段()呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆