Linux Network Programming-advanced socket Functions

Source: Internet
Author: User
Tags sendmsg
Linux Network Programming-advanced socket functions-general Linux technology-Linux programming and kernel information. The following is a detailed description. 6.1 recv and send
The recv and send functions provide similar functions as read and write, but they provide the fourth parameter to control read/write operations.
Int recv (int sockfd, void * buf, int len, int flags)
Int send (int sockfd, void * buf, int len, int flags)
The preceding three parameters are the same as read and write parameters. The fourth parameter can be a combination of 0 or below.
_______________________________________________________________
| MSG_DONTROUTE | no route table |
| MSG_OOB | accept or send out-of-band data |
| MSG_PEEK | views data and does not remove data from the System Buffer. |
| MSG_WAITALL | wait for all data |
| -------------------------------------------------------------- |
MSG_DONTROUTE: a sign used by the send function. This sign tells the IP protocol. The destination host is on the local network and there is no need to find the route table. This sign is generally used in network diagnostics and routing programs.
MSG_OOB: You can receive and send out-of-band data. We will explain this later.
MSG_PEEK: indicates the use of the recv function, indicating that the content is only read from the System Buffer, but the content of the system buffer is not clear. in this way, the content will remain the same for the next read. this flag can be used when multiple processes read and write data.
MSG_WAITALL is the sign used by the recv function, indicating that all information will not be returned until it arrives. when this flag is used, the recv is blocked until the specified condition is met or an error occurs. 1) when the specified byte is read, the function returns normally. return value is equal to len 2) when the end of the file is read, the function returns normally. return value less than len 3) When an operation error occurs,-1 is returned, and the error is set to the corresponding error code (errno)
If the flags value is 0, the operation is the same as the read and write operations. There are several other options, but we actually use very few. You can view the Linux Programmer's Manual for detailed explanation.
6.2 recvfrom and sendto
These two functions are generally used in non-Socket network programs (UDP) and we have learned before.
6.3 recvmsg and sendmsg
Recvmsg and sendmsg can implement the functions of all the previous read and write functions.
Int recvmsg (int sockfd, struct msghdr * msg, int flags)
Int sendmsg (int sockfd, struct msghdr * msg, int flags)
Struct msghdr
{
Void * msg_name;
Int msg_namelen;
Struct iovec * msg_iov;
Int msg_iovlen;
Void * msg_control;
Int msg_controllen;
Int msg_flags;
}
Struct iovec
{
Void * iov_base;/* address at which the buffer starts */
Size_t iov_len;/* buffer length */
}
Msg_name and msg_namelen when the socket is not a connection-oriented (UDP), they store the address information of the receiver and the sender. msg_name is actually a pointer to struct sockaddr, and msg_name is the length of the structure. when the socket is connection-oriented, the two values should be NULL. msg_iov and msg_iovlen indicate the buffer content for receiving and sending. msg_iov is a structure pointer. msg_iovlen indicates the size of the structure array. msg_control and msg_controllen are the two variables used to receive and send control data. msg_flags specifies the operation options for receiving and sending control data. the options are the same as those of recv and send.
6.4 socket disabling
There are two functions for closing the socket: close and shutdown. close is the same as closing the file.
6.5 shutdown
Int shutdown (int sockfd, int howto)
TCP connections are bidirectional (read/write). When we use close, all read/write channels are closed. Sometimes we only want to close one direction, in this case, we can use shutdown. for different howto, different methods are used to disable the system.
When howto is set to 0, the system will close the read channel. However, you can continue writing to the connected descriptor.
Howto = 1 close the write channel. In contrast to the above, you can only read it.
Howto = 2 close the read/write channel. Like close, if several sub-processes share a socket, if we use shutdown, all sub-processes cannot be operated. In this case, we can only use close to close the socket descriptor of the sub-process.
Related Article

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.