Sendmsg recvmsg Function

Source: Internet
Author: User

Sendmsg (2) and recvmsg (2) Functions
These functions provide some advanced features that are not available for other set of interface I/O interfaces. Next we will take a look at sendmsg to introduce these topics. Then the recvmsg function will be fully introduced, because their function interfaces are similar. Next, we will describe the complete structure of msghdr.
Sendmsg (2) Function
Now it is time to enter this big league. In terms of concept, the sendmsg function is the basis for all write functions, and it belongs to a set of interfaces. The following list lists all and Input Functions in the order of complex additions. The added features are listed at each level.
Features added by functions
Write the simplest set of interface writing functions
The flags are added to send.
Sendto adds the set interface address and set interface length parameters.
Writev does not have a flag or a set of interface addresses, but it has the ability to distribute data.
Sendmsg adds a flag, sets the interface address and length, and disperses the ability to write and attach data.
The sendmsg (2) function prototype is as follows:
# Include
# Include
Int sendmsg (int s, const struct msghdr * MSG, unsigned int flags );
Function parameters are described as follows:
Set of interface s on which messages are to be sent
The information header structure pointer MSG, which controls the function call function.
The optional flag parameter flags. This is the same as the flag parameter of the send or sendto function call.
The Return Value of the function is the number of bytes actually sent. Otherwise, "-1" indicates that an error has occurred, while "errno" indicates the cause of the error.
Recvmsg (2) Function
Recvmsg is a function opposite to the sendmsg function. The function is prototype as follows:
# Include
# Include
Int recvmsg (int s, struct msghdr * MSG, unsigned int flags );
The function parameters are as follows:
Interface s on which information is to be received
The information header structure pointer MSG, which controls the function call operations.
Optional flag parameter flags. This is the same as the flag parameter of the Recv or recvfrom function call.
The return value of this function is the actual number of bytes received. Otherwise, "-1" indicates that an error has occurred, while "errno" indicates the cause of the error.
Understanding struct msghdr
When I first saw him, it seemed like a huge structure to be created. But don't be afraid. Its structure is defined as follows:
Struct msghdr {
Void * msg_name;
Socklen_t msg_namelen;
Struct iovec * msg_iov;
Size_t msg_iovlen;
Void * msg_control;
Size_t msg_controllen;
Int msg_flags;
};
The structure members can be divided into four groups. They are:
Set the interface address members msg_name and msg_namelen.
The I/O vector references msg_iov and msg_iovlen.
Msg_control and msg_controllen of the attached data buffer.
The receiving information flag msg_flags.
After we divide this structure into the above categories, the structure will not look so huge.
Msg_name and msg_namelen
These members are only required when our set interface is a datagram set interface. The msg_name Member points to the set interface address for sending or receiving information. Msg_namelen indicates the length of the interface address.
When recvmsg is called, msg_name points to the receiving region of the address to be received. When sendmsg is called, this directs to the destination address to which the datagram will be sent.
Note that msg_name is defined as a (void *) data type. We do not need to convert our set of interface addresses to (struct sockaddr *).
Msg_iov and msg_iovlen
These members specify the position of our I/O Vector Array and the number of items it contains. The msg_iov Member points to a struct iovec array. We will recall that the I/O vector points to our buffer zone. Msg_iov indicates the number of elements in our I/O Vector Array.
Msg_control and msg_controllen members
These members point to our affiliated data buffer and indicate the buffer size. Msg_control points to the secondary data buffer, while msg_controllen indicates the buffer size.
Msg_flags
When recvmsg is used, this member is used to receive a specific tag bit (which is not used for sendmsg ). The following table lists the markup bits that can be received at this position:
Tag description
Msg_eor is set when the end of the received record is reached. This is usually useful for the sock_seqpacket interface type.
Msg_trunc indicates that the end of the data is truncated because the receiving buffer is too small to receive all the data.
Msg_ctrunc indicates that some control data (ancillary data) is truncated because the buffer zone is too small.
MSG_OOB indicates that out-of-band data is received.
Msg_errqueue indicates that no data is received, but an extension error is returned.
You can view more information on the man manual page of recvmsg (2) and sendmsg (2.
Subsidiary data structure and macro
The recvmsg and sendmsg functions allow the program to send or receive ancillary data. However, these additional information is limited by certain format rules. This section describes the macros used to control information headers and programs to manage such information.

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.