Detailed description of the send and Recv functions in Linux Kernel

Source: Internet
Author: User

Linux send and Recv Functions

1. Introduction

#include <sys/socket.h>ssize_t recv(int sockfd, void *buff, size_t nbytes, int flags);ssize_t send(int sockfd, const void *buff, size_t nbytes, int flags);

  • The first three parameters of send and Recv are equivalent to read and write;
  • The flags parameter value is 0 or:
Flags Description Recv Send
Msg_dontroute Bypassing route table search
Msg_dontwait This operation is not blocked only
MSG_OOB Send or receive out-of-band data
Msg_peek View External messages
Msg_waitall Wait for all data

2. Send resolution

  • Sockfd: Specifies the sender socket descriptor.
  • Buff: buffer for storing data to be sent
  • Nbytes: number of bytes of data to be improved
  • Flags: generally set to 0.

1) Send first compares the sending data length nbytes and the sending buffer length of socket sockfd. If nbytes> socket sockfd sends the buffer length, this function returns socket_error.

2) If nbtyes <= length of the sending buffer of socket sockfd, send first checks whether the protocol is sending data in the sending buffer of sockfd. If yes, wait until the Protocol finishes sending the data, if the Protocol has not started sending data in the sending buffer of sockfd or the sending buffer of sockfd has no data, send compares the remaining space and nbytes of the sending buffer of sockfd.

3) If nbytes> the length of the buffer space remaining for sending socket sockfd, send waits for the Protocol to finish sending data in the sending buffer zone of socket sockfd.

4) if nbytes <the size of the buffer space remaining for sending socket sockfd, send only copies the data in the Buf to the remaining space (Note:Instead of sending data from the sending buffer of socket sockfd to the other end of the connection, it is transmitted by protocol. Send only copies the data in the Buf to the remaining space in the sending buffer zone of socket sockfd ).

5) if the send function copies data, the actual number of bytes of copy is returned. If an error occurs during sending data copy, the send function returns socket_error. If the network is disconnected while waiting for the Protocol to send data, the send function also returns socket_error.

6) After the send function successfully copies the data in the buff to the free space of the sockfd sending buffer, it returns the data, but the data is not necessarily uploaded to the other end of the connection immediately. If a network error occurs during subsequent transmission, then the next socket function will return socket_error (each socket function except send must wait for the data in the sending buffer of the socket to be transmitted by the protocol before it can continue, if a network error occurs while waiting, the socket function returns socket_error ).

7) in Unix systems, if the network is disconnected when sending data while waiting for the Protocol to send data, the process that calls send will receive a sigpipe signal, and the process processes the signal by terminating the process.

2. Recv Parsing

  • Sockfd: Socket descriptor of the receiving end
  • Buff: the buffer that stores the data received by the Recv function.
  • Nbytes: Specifies the length of the buff
  • Flags: usually set to 0

1) Recv waits until the data in the sending buffer of sockfd is transmitted by the Protocol. If a network error occurs when the Protocol transmits data in the sending buffer of sock, The Recv function returns socket_error.

2) if no data exists in the sending buffer of socket sockfd or the data is successfully sent by the protocol, Recv first checks the receiving buffer of socket sockfd, if no data is in the receiving buffer of sockfd or the Protocol is receiving data, the Recv waits until the data is received. When the Protocol receives the data, the Recv function copies the data in the receiving buffer of sockfd to the Buff (Note:: The data received by the protocol may be longer than the buff length. In this case, you must call the Recv function several times to copy the data in the sockfd receiving buffer. The Recv function only copies data. The protocol is used to receive data ).

3) The Recv function returns the actual number of bytes for copy. If the Recv has an error during copy, it returns socket_error. If the network is interrupted when the Recv function is waiting for the Protocol to receive data, it returns 0.

4) in Unix systems, if the network is disconnected when the Recv function is waiting for the Protocol to receive data, the process that calls the Recv will receive a sigpipe signal, the process processes this signal by default.

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.