Send, Recv, sendto, recvfrom

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 one end of the TCP connection.

The client program generally uses the send function to send requests to the server, while the server often uses the send function to send responses to the client program.

The first delimiter number of this function specifies the sender socket descriptor;

The second shard number indicates a buffer that stores the data to be sent by the application;

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

The fourth vertex is generally set to 0.

Here, we only describe the running process of the send function for Synchronous socket. When this function is called, send is equal to the length of the data to be sent by Len and the sending buffer length of socket s. If Len is greater than the sending buffer length of S, socket_error is returned; if Len is smaller than or equal to the length of the sending buffer of S, send checks whether the protocol is sending data in the sending buffer of S. If it is waiting for the Protocol to finish sending the data, if the Protocol does not enable sending data in the sending buffer of the sending s or the sending buffer of the sending s has no data, the sending is more than the remaining space and Len of the sending buffer of the sending S, 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 not sending transmits data in the sending buffer of S to another end of the connection, instead, the data in the Buf is copied to the free 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 immediately transmitted to another end of the connection. If a network error occurs during the transmission process of the Protocol, the next socket function will return socket_error. (Each socket function except send always waits for the data in the socket sending buffer to be transmitted by the protocol before it starts to execute. 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. By default, the process processes the signal 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 one end of the TCP connection.

The first delimiter of this function specifies the socket descriptor of the receiving end;

The second digit indicates a buffer, which is used to store the data received by the Recv function;

The third vertex number indicates the length of the Buf;

The fourth vertex is generally set to 0.

Here we only describe the running process of the Recv function for Synchronous socket. 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 sends data in the s sending buffer, a network error occurs, then, the Recv function returns socket_error. If there is no data 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, 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 real data reception is completed by the Protocol). The Recv function returns the actual number of bytes of the copy. If the Recv has an error in copy, socket_error is returned. If the network is interrupted when the Recv function is 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.

 

Sendto and recvfrom are generally used in UDP, but they can be used after the connect function is called in TCP.

 

Sendto () and recvfrom () -- transmit data using Datagram
In the connectionless datagram socket mode, because the local socket does not establish a connection with the remote machine, the destination address should be specified when sending data. The prototype of the sendto () function is:
Int sendto (INT sockfd, const void * MSG, int Len unsigned int flags, const struct sockaddr * To, int tolen );
This function has two more records than the send () function. To indicates the IP address and port number of the host, while tolen is often assigned sizeof (struct sockaddr ). The sendto function also returns the actual length of data bytes or-1 in case of a sending error.
The original recvfrom () function is:
Int recvfrom (INT sockfd, void * Buf, int Len, unsigned int lags, struct sockaddr * From, int * fromlen );
From is a variable of the struct sockaddr type, which saves the IP address and port number of the source machine. Fromlen is often set to sizeof (struct sockaddr ). When recvfrom () is returned, fromlen includes the number of data bytes actually stored in from. The recvfrom () function returns the number of bytes received or-1 if an error occurs, and the corresponding errno is set.
It should be noted that when you call the connect () function for the datagram socket, you can also transmit data using send () and Recv, however, this socket is still a datagram socket and uses the UDP Service at the transport layer. However, when sending or receiving data reports, the kernel automatically adds the source and destination information to the messages.

 

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.