Socket send function and recv Function

Source: Internet
Author: User

Socket send function and recv Function

Socket send function and recv Function

1. send Function

Int send (SOCKET s, const char FAR * buf, int len, int flags ); 

Both the client and server applications use the send function to send data to the other end of the TCP connection. The client program generally uses the send function to send requests to the server, while the server uses the send function to send responses to the client program.

The first parameter of this function specifies the sender socket descriptor;

The second parameter specifies a buffer for storing the data to be sent by the application;

The third parameter specifies the number of bytes of data to be sent;

The fourth parameter is usually set to 0.

Description onlyExecute the synchronous Socket send Function. When this function is called,

(1) send first compares the length of the data to be sent len and the length of the sending buffer of socket s. If len is greater than the length of the sending buffer of s, this function returns SOCKET_ERROR;

(2) If len is smaller than or equal to the length of the sending buffer of s, send first checks whether the protocol is sending data in the sending buffer of s. If yes, wait until the Protocol finishes sending the data, if the Protocol has not started sending data in the sending buffer of s or there is no data in the sending buffer of s, send will compare the remaining space in the sending buffer of s and len

(3) If len is larger than the remaining space, send will wait until the Protocol finishes sending data in the sending buffer of s.

(4) If len is smaller than the remaining space, send only copies the data in the buf to the remaining space (Note that not sending transmits the data in the sending buffer of s to the other end of the connection, but the Protocol. Sending only copies the data in the buf to the remaining space of the sending buffer of s.).

If the send function successfully copies the 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 when sending data while waiting for the Protocol to send data, the send function also returns SOCKET_ERROR.

Note that the send function successfully copies the data in the buf to the remaining space of the s sending buffer, and then returns the data. However, the data is not necessarily uploaded to the other end of the connection immediately.. If a network error occurs during subsequent transmission, the next Socket function will return SOCKET_ERROR. (Each Socket function except send must wait until the data in the sending buffer of the Socket is transmitted by the protocol before it can continue. If a network error occurs while waiting, then the Socket function will return SOCKET_ERROR)

Note: In Unix systems, if the network is disconnected when sending data while waiting for the send protocol, the process that calls send receives a SIGPIPE signal. The process processes the signal by default and terminates the process.

Tests show that the send function of asynchronous socket can return the corresponding number of bytes when the network is just disconnected, and the select Check is also writable. However, after a few seconds, an error occurs when sending the message again.-1 is returned. Select cannot detect writable data.


 

2. recv Function

Int recv (SOCKET s, char FAR * buf, int len, int flags ); 

Both the client and server applications use the recv function to receive data from the other end of the TCP connection. The first parameter of this function specifies the receiver socket descriptor;

The second parameter specifies a buffer that is used to store the data received by the recv function;

The third parameter specifies the length of the buf;

The fourth parameter is usually set to 0.

Description onlyExecute the recv function of synchronous Socket. When the application calls the recv function,

(1) recv waits for data in the sending buffer of s to be transmitted by the Protocol. If a network error occurs when the Protocol sends data in the sending buffer of s, the recv function returns SOCKET_ERROR,

(2) If there is no data in the sending buffer of s or the data is successfully sent by the protocol, recv first checks the receiving buffer of socket s, if there is no data in the s receiving buffer or the Protocol is receiving data, the recv waits until the Protocol receives the data. When the Protocol receives the data, the recv function copies the data in the s receiving buffer to the buf (Note that the data received by the protocol may be longer than the buf length. In this case, you must call the recv function several times to copy the data in the s receiving buffer. The recv function only copies data, and the protocol is used to actually receive data.),

The recv function returns the number of bytes of the actual copy. If a recv error occurs during copy, SOCKET_ERROR is returned. If the recv function is interrupted while waiting for the Protocol to receive data, 0 is returned.

Note: 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.