c – sendmsg如何工作?

前端之家收集整理的这篇文章主要介绍了c – sendmsg如何工作?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
你知道sendmsg有这个声明:

int sendmsg(int s,const struct msghdr * msg,int flags);

并且msghdr结构具有以下形式:

  1. struct msghdr {
  2. void * msg_name; /* optional address */
  3. socklen_t msg_namelen; /* size of address */
  4. struct iovec * msg_iov; /* scatter/gather array */
  5. size_t msg_iovlen; /* # elements in msg_iov */
  6. void * msg_control; /* ancillary data,see below */
  7. socklen_t msg_controllen; /* ancillary data buffer len */
  8. int msg_flags; /* flags on received message */
  9. };

如你所见,msghdr有一个缓冲区数组,iovec和缓冲区数msg_iovlen.我不知道sendmsg如何发送这些缓冲区.它连接所有缓冲区并发送还是发送在for循环?

解决方法

联机帮助页面提供了一个消息(单数)和多个元素(复数):

For send() and sendto(),the message is found in buf and has length
len. For sendmsg(),the message is pointed to by the elements of the
array msg.msg_iov. The sendmsg() call also allows sending ancillary
data (also known as control information).

对于一个流套接字,无论如何.您发送的任何数据只会在另一端最后成为一长串数据.

对于数据报或消息套接字,我可以看到为什么更清晰一些将会有所帮助.但是,您似乎只发送一个数据报或消息与一个sndmsg调用;每个缓冲区元素不是一个.

我真的很好奇地挖掘了Linux源代码,并更好地了解了这个答案.看起来像send,sendto只是在Linux中的sendmsg的包装器,为你构建了struct msghdr.实际上,UDP sendmsg实现为每个sendmsg调用一个UDP头提供了空间.

如果性能是你担心的,那么如果你只传递一个iovec,那么看起来你不会受益于sendmsg.如果您在用户空间中连接缓冲区,那么这可能会赢得您的一些.

这与writev有点类似,附加的好处是您可以指定与无连接套接字(如UDP)一起使用的目标地址.你也可以添加辅助数据,如果你是这样的事情. (通常用于跨UNIX域套接字发送文件描述符.)

猜你在找的C&C++相关文章