Go A detailed description of the Socket send function and recv function

Source: Internet
Author: User
Tags terminates

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 typically sends a request to the server with the Send function, and the server usually sends a reply to the client using the Send function.

The first parameter of the function specifies the send-side socket descriptor;

The second parameter indicates a buffer where the application will send data;

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

The fourth parameter is generally 0.

This describes only the execution flow of the Send function that synchronizes the socket. When the function is called, the

(1) Send compares the length of the data to be sent, Len and the length of the transmit buffer of the socket s, and if Len is greater than the length of the send buffer of s, the function returns SOCKET_ERROR;

(2) If Len is less than or equal to the length of the send buffer of S, then send first checks if the protocol is sending the data in the send buffer of s, and if it is waiting for the protocol to send the data out, if the protocol has not started sending the data in the send buffer of s or S, there is no data in the send buffer. Then send compares the remaining space of the transmit buffer of S and Len

(3) If Len is larger than the remaining space, send waits for the protocol to send out the data in the transmit buffer of s

(4) If Len is less than the amount of space left, send simply copy the data in the BUF to the remaining space (note that it is not the send buffer that sends the data to the other end of the connection, but the protocol is transmitted, Send simply copy the data in the BUF to the remaining space in the send buffer of s).

If the Send function copy data succeeds, it returns the number of bytes actually copied, and if send has an error in copy data, then send returns SOCKET_ERROR, if the network is disconnected while the send waits for the protocol to transmit the data. Then the Send function also returns SOCKET_ERROR.

Note that the Send function returns the data in the BUF after it has been successfully copied to the remaining space of S's send buffer, but at this point the data is not necessarily immediately passed to the other end of the connection. If the protocol has a network error during subsequent transfers, the next socket function returns SOCKET_ERROR. (Each of the socket functions except for send will have to wait for the data in the socket's send buffer to continue before execution, and if a network error occurs while waiting, the socket function returns SOCKET_ERROR)

Note: Under UNIX systems, if send is disconnected when the network is waiting for the protocol to transmit data, the process that calls send receives a sigpipe signal, and the process terminates the default processing for that signal.

The test found that the Send function of the asynchronous socket can also send back the corresponding number of bytes when the network is just disconnected, while the use of select detection is also writable, but after a few seconds, send will be error, return-1. Select also cannot detect writable.


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 the function specifies a receive-side socket descriptor;

The second parameter indicates a buffer, which is used to hold the data received by the RECV function;

The third parameter indicates the length of the BUF;

The fourth parameter is generally 0.

This describes only the execution flow of the recv function that synchronizes the socket. When the application calls the Recv function,

(1) The recv waits for the data in the transmit buffer of S to be transmitted by the Protocol, if the Protocol has a network error while transmitting the data in the transmit buffer of S, then the RECV function returns SOCKET_ERROR,

(2) If there is no data in the transmit buffer of s or the data is successfully sent by the protocol, recv first checks the socket s receive buffer, if there is no data in the s receive buffer or the protocol is receiving data, then recv waits until the protocol receives the data. When the protocol takes over the data, the RECV function will copy the data from the receive buffer of S to BUF (note that the data received by the Protocol may be greater than the length of the buf, so in this case a few recv functions are called to copy the data from the receive buffer of S. The recv function is just copy data, and the actual receive data is the protocol to complete),

The RECV function returns the number of bytes of its actual copy. If recv errors in copy, it returns SOCKET_ERROR, and if the recv function waits for the protocol to receive data, the network is interrupted, it returns 0.

Note: Under UNIX systems, if the RECV function waits for the protocol to receive data when the network is disconnected, the process that calls Recv receives a sigpipe signal, and the process terminates the default processing for that signal.

http://www.cnblogs.com/jianqiang2010/archive/2010/08/20/1804598.html#undefined

Go A detailed description of the Socket send function and recv function

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.