The Send,sendto,recv,recvfrom function of TCP communication

Source: Internet
Author: User
Tags function prototype terminates

Send function

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

Both the client and the server application use the Send function to send data to the other end of the TCP connection.

The client typically sends a request to the server using the Send function, and the server usually sends a response to the client using the Send function.

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

The second parameter indicates a buffer that holds the application to send data;

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

The fourth parameter is generally set to 0.

This only describes the execution flow of the Send function for synchronous sockets. When this function is called, send first compares the length of Len and socket s of the data to be sent, and if Len is greater than the length of the send buffer of s, the function returns SOCKET_ERROR; If Len is less than or equal to the length of the send buffer of s, send first checks the protocol is sending the data in the send buffer of s, if it is waiting for the protocol to send the data, if the protocol has not started to send the data in the send buffer of s, or the send buffer of S has no data, then send to compare s of the remaining space of the sending buffer and Len, If Len is larger than the remaining space, send waits until the protocol sends the data from the send buffer of s, and if Len is less than the remaining space, send only the data in the BUF to the rest of the space (note that not send sends the data from the send buffer of s to the other end of the connection) Instead, the protocol sends only the data in the BUF to the rest of the send buffer of S. If the Send function copy data succeeds, it returns the actual copy of the number of bytes, if the send in the copy data error, then send back to Socket_error, if the send is waiting for the protocol to transfer data when the network disconnected, Then the Send function also returns SOCKET_ERROR.

Note that the Send function returns when the data in buf succeeds in the remaining space of the send buffer of s, but the data is not necessarily immediately uploaded to the other end of the connection. The next socket function returns SOCKET_ERROR if the protocol has a network error during subsequent transmissions. (each except send the SOCKET function at the beginning of the first always wait for the socket to send the buffer data sent by the Protocol to continue, if there is a network error waiting, then the socket function will return to SOCKET_ERROR)

Note: On UNIX systems, if send is disconnected while waiting for the protocol to transfer data, the process calling send receives a sigpipe signal, and the process terminates the process by default.

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

Both the client and the server application receive data from the other end of the TCP connection using the Recv function.

The first parameter of the function specifies the receive-end socket descriptor;

The second parameter indicates a buffer for storing the data received by the RECV function;

The third parameter indicates the length of the BUF;

The fourth parameter is generally set to 0.

This only describes the execution flow of the recv function of the synchronous socket. When the application calls the Recv function, recv waits for the data in the send buffer of s to be transmitted by protocol, and if the protocol has a network error transmitting the data in the send buffer of S, then the RECV function returns SOCKET_ERROR if the send buffer of S is not counted According to or data is successfully sent after the agreement, RECV first check socket s receive buffer, if the s receive buffer is not data or the protocol is receiving data, then recv has been waiting, only to the agreement to receive the data. When the protocol has received the data, the RECV function will copy the data from the receive buffer of S to the BUF (note that the protocol receives more data than the BUF length, so it is necessary to invoke several recv functions in this case to copy the data in the receive buffer of S. The recv function is just copy data, and the true receive data is protocol to complete, and the Recv function returns the actual number of bytes of copy. If recv fails at copy, it returns SOCKET_ERROR, which returns 0 if the Recv function is interrupted while waiting for the protocol to receive data.

Note: On UNIX systems, if the RECV function is disconnected while waiting for the protocol to receive data, the process calling recv receives a sigpipe signal, and the process terminates the process by default.

SendTo and recvfrom are generally used in UDP protocols, but can also be used if the Connect function is called in TCP.

SendTo () and Recvfrom ()-Data transmission    using datagrams;
In the absence of a connected datagram socket, the local socket is not connected to the remote machine, so the data should be sent to the address of the eyesight, The SendTo () function prototype is:   
int sendto (int sockfd, const void *msg,int len unsigned int flags, const struct SOCKADDR *to, int tolen);
This function has two more arguments than the Send () function to represent the IP address and port number information of the target machine, and Tolen is often assigned sizeof (struct sockaddr). The Sendto function also returns the actual byte length of the data being sent or returns 1 if a Send error occurs.  &NBSP
Recvfrom () function prototype:   
int recvfrom (int sockfd,void *buf,int len,unsigned int lags,struct s Ockaddr *from,int *fromlen);
from is a struct-type variable that holds the IP address and port number of the source computer. Fromlen are often placed as sizeof (struct sockaddr). When Recvfrom () returns, the Fromlen contains the number of bytes of data actually deposited in from. The Recvfrom () function returns the number of bytes received or returns 1 when an error occurs, and the corresponding errno is placed.  &NBSP
Note that when you invoke the Connect () function for the datagram socket, you can also use Send () and recv () for data transfer, but the socket is still a datagram socket. and use the transport layer of UDP services. But when the data is sent or received, the kernel automatically adds the target and source address information.

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.