(1) It is recommended to use the Recvmsg ()/sendmsg () function, which is the most general I/O function and can actually replace the other functions above.
(2) Each function declaration is as follows:
ssize_t sendmsg (int sockfd, const struct MSGHDR *msg, int flags);
ssize_t recvmsg (int sockfd, struct MSGHDR *msg, int flags);
-------------------------------------------
(3) Read ()/write ():
1) Read function:
The read function is responsible for reading the content from FD. When read succeeds, read returns the actual number of bytes read, if the returned value is 0 to indicate that the end of the file has been read, and less than 0 indicates an error occurred. If the error is eintr, the read is caused by an interrupt, if econnrest indicates a problem with the network connection.
2) Wirte function:
The Write function writes the nbytes byte content in buf to the file descriptor FD. Returns the number of bytes written when successful. Returns 1 on failure and sets the errno variable.
Two return results:
1) The return value of write is greater than 0, indicating that some or all of the data is written.
2) The returned value is less than 0, and an error occurs. We are going to deal with the error type. If the error is EINTR, an interrupt error occurred while writing. If the epipe indicates a problem with the network connection (the other party has closed the connection).
-----------------------------------------
(4) recv ()/send ():
1) The Send function:
The parameters of send have the following meanings:
SOCKFD: The socket descriptor that represents your connection to the remote program.
Msg: A pointer to the address of the message you want to send.
Len: Is the length of the message you want to send.
Flags: Send tokens. Usually set to 0 (you can view the man pages of send for additional parameter values and understand what each parameter means).
2) Send return value:
The Send () function returns the length of the data it actually sends after it is called, and the Send () function returns –1 if an error occurs.
Attention:
Send may have less data than the length you specified for the parameter you gave it. Because if you give send () the length of the data contained in the parameter is much larger than the data sent by send (), the Send () function sends only the maximum data length that it can send, and then it believes that you will call it again for the second time to send the rest of the data. So, remember that if the return value of the Send () function is less than Len, then you need to send the rest of the data again. Fortunately, if the package is small enough (less than 1K), send () usually sends the light one at a time.
3) recv function:
SOCKFD: Is the socket descriptor for which you want to read data.
BUF: is a pointer to the memory cache area where you can store data.
Len: is the maximum size of the buffer.
Flags: A flag for the recv () function, typically 0 (see recv () for other values and meanings
4) recv return value:
Recv () returns the length of the data it actually receives. (That is, the length of the data stored in the BUF). If the return –1 represents an error (such as an outage outside the network, the other side closes the socket connection, and so on), the global variable errno stores the error code.
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.