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 the server application 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 using the Send function. The server often uses the Send function to send a reply to the client program.

The first parameter of the function specifies the sending end socket descriptive descriptor;

The second parameter indicates a buffer where the application is to 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 running flow of the Send function that describes the synchronous socket. When this function is called, send is compared to the length of the sending buffer of the data to be sent, Len and socket s, assuming Len is greater than the length of the send buffers of s, the function returns SOCKET_ERROR; If Len is less than or equal to the length of the send buffer of s, then send checks the protocol is sending the data in the send buffer of s, assuming that it is waiting for the protocol to send out the data, assuming that the protocol has not yet started sending the data in the send buffer of s, or that there is no data in the send buffer of s, then send is the remaining space and Len of the send buffer of S. Assuming that Len is larger than the remaining space, send waits for the protocol to send the data in the transmit buffer of S, assuming that Len is less than the amount of space left to copy the data in the BUF to the rest of the space (note that it is not the send buffer that sends the data to the connection and one end of the Instead of the protocol, send does not copy the data in the BUF to the remaining space in the send buffer of s). Assuming that 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, assuming that the network is disconnected while waiting 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 left buffer of S, but at this point the data is not necessarily immediately transmitted to the connected side.

If a network error occurs in the protocol during the transfer process. Then the next socket function will return SOCKET_ERROR.

(Each of the socket functions except for send will always wait for the data in the socket's send buffer to continue until the protocol is completed, assuming a network error occurred while waiting.) Then the SOCKET function returns SOCKET_ERROR)

Note: Under UNIX systems, assuming that the network disconnects when send waits for the protocol to transmit data, the process that calls send receives a sigpipe signal, and the process terminates the process by default processing of that signal.

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 the receiving end socket descriptive descriptor;

The second parameter indicates a buffer. This buffer 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 is just a description of the running flow of the RECV function describing the synchronous socket. When the application calls the Recv function, recv waits for the data in the send buffer of s to be completed by the protocol, assuming a network error occurs when the protocol transmits the data in the send buffer of S. Then the RECV function returns SOCKET_ERROR, assuming that there is no data in the send buffer of s or the data is successfully sent by the protocol, recv first checks the socket s receive buffer, assuming that there is no data in the s receive buffer or the protocol is receiving data. Then recv has been waiting. The data is received only after the protocol.

When the protocol takes the data to completion, the RECV function will copy the data from the receive buffer of S to the BUF (note that the data received by the Protocol may be greater than the length of the buf, so in such a case the RECV function can be called several times to copy the data in the receive buffer of S. The recv function is just copy data, the real receive data is the protocol to complete, the RECV function returns the actual number of bytes in the copy. Assuming the recv error in copy, it returns SOCKET_ERROR, assuming that the recv function is interrupted while waiting for the protocol to receive data. Then it returns 0.

Note: Under UNIX systems, assuming that the recv function is disconnected while waiting for the protocol to receive data, the process that calls Recv receives a sigpipe signal. The default processing for the signal by the process is the process termination.

SendTo and Recvfrom are commonly used in UDP protocols, but are assumed to be useful after the Connect function is called in TCP.

SendTo () and Recvfrom ()--transmit data using datagrams
In a non-connected datagram socket, the local socket is not connected to the remote machine. So when you send data, you should refer to the address of your 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 parameters than the Send () function. To represents the IP address and port number information for the target machine, and Tolen is often assigned to sizeof (struct sockaddr).

The Sendto function also returns the length of the data bytes actually sent or returns 1 if a Send error occurs.
The Recvfrom () function prototype is:
int recvfrom (int sockfd. void *buf,int len,unsigned int lags,struct sockaddr *from. int *fromlen).
From is a struct sockaddr variable that holds the IP address and port number of the source machine.

Fromlen is often used as sizeof (struct sockaddr). When Recvfrom () returns, Fromlen includes the number of bytes of data actually deposited from the from. The Recvfrom () function returns the number of bytes received or returns 1 when an error occurs, collocated with the corresponding errno.


One thing to be aware of. When you call the Connect () function for a datagram socket, you can also use the transfer of the full send () and recv () data. However, the socket is still a datagram socket and uses the Transport Layer UDP service. But to send or receive the datagram, the kernel will actively add value for the purpose and source address information.

Send,recv,sendto,recvfrom

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.