Socket send and Recv Functions

Source: Internet
Author: User
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.

Only the execution process of the send function for Synchronous socket is described here. When this function is called, 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, the function returns socket_error; 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 for the Protocol to finish 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, if Len is larger than the size of the remaining space, send will wait for the Protocol to finish sending data in the s sending buffer, if Len is smaller than the size of the remaining space, send only copies the data in the Buf to the remaining space (note that it is not sending the data in the sending buffer of S to the other end of the connection, it is transmitted through the Protocol,Send only copies the data in the Buf to the remaining space in the sending buffer zone 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 for the data in the socket sending buffer to be transmitted by the protocol before execution. 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.
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.

Only the execution process of the Recv function for Synchronous socket is described here. When the application calls the Recv function, the Recv waits for the data in the s sending buffer to be transmitted by the Protocol. If the Protocol encounters a network error when sending data in the s sending buffer, then the Recv function returns socket_error. If no data is in the sending buffer of s or the data is successfully sent by the protocol, the 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 larger than the length of the Buf, so
In this case, you need to 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 actual number of bytes of 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.

/*************************************** ****************************************/

Int send (
Socket s,
Const char far * Buf,
Int Len,
Int flags
);

How much data can the send function send at most each time? Is it the maximum value of Int?

[Sms88 ]:
Not the maximum value of int

[Sms88 ]:
Theoretically, it depends on the size of a window minus the size of the protocol header.

[Xxyyboy ]:
No, send has a length that is not specified, but a length that is specified.
Int send (
Socket S, // connection
Const char far * Buf, // buffer content
Int Len, // buffer Len
Int flags // unknown
);

The Int function only indicates the return value of the failed message sending, not the length of the returned value.

If you have a buffer, you can send the message.
The data streams from Recv may be intermittent. You need to put them together and decode them.

[Dodoboy ]:
That is to say, if the data in the buffer is too large, I only need to call the send function once, but does the lower layer have a successful transmission or will it be ignored?

[Mudunke775 ]:
The data streams from Recv may be intermittent. You need to put them together and decode them.

//////////////////////////////////////// //////////////////////////

Decoding and merging packages are implemented at the lower layer. Do not decode them by yourself?

[Unsigned ]:
Sending is divided into blocking and non-blocking. In blocking mode, if it is normal, it will not return until the data you want to send is sent, the data in your buffer zone will be copied based on the size of the buffer zone available at the underlying layer. If the buffer zone is full, the system will return immediately, at this time, the returned value only indicates how much data is copied to the buffer zone, but does not indicate how much data is sent. At the same time, you need to call send again to copy the data to the underlying buffer zone again.

[Iambic ]:
Similar to write.

[Arthur _]:
You can use * setsockopt * to enlarge the buffer.

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.