UNIX Network Programming Reading Notes: recvmsg and sendmsg Functions

Source: Internet
Author: User
Tags sendmsg truncated

These two functions are the most common I/O functions. In fact, we can replace all read, readv, recv, and recvfrom calls with recvmsg calls. Similarly, various output function calls can be replaced with sendmsg calls.

# Include <sys/socket. h> sockfd, msghdr * msg, sockfd, msghdr * msg, --- Error

These two functions encapsulate most of the parameters in a msghdr structure:

            *msg_name;                iovec    *msg_iov;                              msg_iovlen;                         *msg_control;                          msg_flags;          

The msghdr structure shown here complies with POSIX specifications. Some systems still use this structure from older versions of 4.2BSD. This older structure does not have msg_flags members, and msg_control and msg_controllen members are called msg_accrights and msg_accrightslen respectively. This older structure only supports the form of auxiliary data used to pass the file description (called access permission ).

Msg_name and msg_namelenThese two members are used when the set interface is not connected (for example, the UDP set interface is not connected ). They are similar to the 5th and 6th parameters of reacvfrom and sendto: msg_name points to a set of interface address structures, where the caller stores the receiver (called for sendmsg) or the sender (called for recvmsg). Msg_name should be a null pointer if you do not need to specify the Protocol address (for example, for TCP or UDP interfaces connected. Msg_namelen is a value parameter for sendmsg and a value-result parameter for recvmsg.

Msg_iov and msg_iovlenThe two members specify the input or output buffer array (the iovec structure array), similar to the 2nd and 3rd parameters of readv and writev.

Msg_control and msg_controllenThe two members specify the location and size of the optional auxiliary data.

For recvmsg and sendmsg, we must distinguish the two variables: one is the flags parameter for passing values, and the other is the msg_flags Member of the passed msghdr structure, which transmits references, because the address passed to the function is the address of the structure.

Flag Kernel check:
Send flags
Sendto flags
Sendmsg flags
Kernel check:
Recv flags
Recvfrom flags
Recvmsg flags
Kernel return:
Recvmsg msg_flags
MSG_DONTROUTE
MSG_DONTWAIT
MSG_PEEK
MSG_WAITALL MSG_EOR
MSG_OOB MSG_MCAST
MSG_TRUNC
MSG_CTRUNC
MSG_NOTIFICATION    



Among these flags, the kernel only checks without returning the first four flags; both checks and returns the next two flags; without checking, only returns the last five flags. The Seven Flags returned by recvmsg are described as follows:

MSG_BCAST this flag is introduced with BSD/OS and is relatively new. Its return condition is: this datagram is collected as a link layer broadcast or its IP address is a broadcast address.

MSG_MCAST this flag is introduced with BSD/OS and is relatively new. Its return condition is: this datagram is collected as the link layer multicast.

MSG_TRUNC the return condition of this flag is that the current datagram is truncated; that is, the data returned by the kernel preparation exceeds the space allocated by the process in advance (the sum of all iov_len members ).

MSG_CTRUNC this flag returns the condition that the auxiliary data of this datagram is truncated; that is, the auxiliary data returned by the kernel preparation exceeds the space allocated by the process (msg_controllen ).

MSG_EOR if the returned data is not the end of a logical record, this flag is cleared; otherwise, this flag is set. TCP does not use this flag because it is a byte stream protocol.

MSG_OOB this flag is never returned for TCP out-of-band data. It is used by other protocol families (such as the OSI protocol family ).

MSG_NOTIFICATION: the message read by the SCTP receiver is an event notification rather than a data message.

Shows a msghdr structure and various information it points. In the figure, we assume that the process is about to call recvmsg for a UDP interface.

In the figure, the Protocol address is allocated 16 bytes and the secondary data is allocated 20 bytes. An array consisting of three iovec structures initialized for the buffer data: the first one specifies a 100-byte buffer, the second one specifies a 60-byte buffer, and the third one specifies an 80-byte buffer. We also assume that the IP_RECVDSTADDR interface option has been set for this interface to receive the IP address of the UDP packets read.

Let's assume that port 2000 has reached a 170-byte UDP datagram from Port, and its destination is our UDP interface, with the IP address 206.168.112.96. Shows all information in the msghdr structure returned by recvmsg. (The modified fields in the figure are marked with shadows)

Shows the differences between five groups of I/O functions:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.