3

I've set a udp socket and call sendto() with a different recipient at each call.

I would like to use writev() in order to benefit scater/gather io but writev() does not allows me to specify the recipient addr/port as in sendto(). Any suggestions?

yves Baumes
  • 8,836
  • 7
  • 45
  • 74

2 Answers2

3

On Linux, there is sendmmsg(2)

The sendmmsg() system call is an extension of sendmsg(2) that allows the caller to transmit multiple messages on a socket using a single system call. (This has performance benefits for some applications.)

The prototype is:

int sendmmsg(int sockfd, struct mmsghdr *msgvec, unsigned int vlen,
             unsigned int flags);

struct mmsghdr {
    struct msghdr msg_hdr;  /* Message header */
    unsigned int  msg_len;  /* Number of bytes transmitted */
};

Since both the address and the i/o vector is specified in struct msghdr, you can both send to multiple destinations and make use of scatter/gather.

wkz
  • 2,203
  • 1
  • 15
  • 21
2

You can use writev to send a coalesced set of buffers to a single end point if you use connect to specify the end point beforehand. From the (OSX) manpage for connect(2):

datagram sockets may use connect() multiple times to change their association

You cannot use writev to send each buffer to a different endpoint.

A potential downside of using connect / writev instead of sendto*n is that it is yet another system call per writev.

If the set of recipients is limited (and known in advance) it may be preferable to use a separate socket per recipient and just connect each socket once.

Alnitak
  • 334,560
  • 70
  • 407
  • 495
  • "If the set of recipients is limited (and known in advance) it may be preferable to use a separate socket per recipient and just connect each socket once." -> oh yeah, right, that's what we do now. Thnx! – yves Baumes Dec 03 '13 at 17:22