Linux Socket send and RECV functions

Source: Internet
Author: User
Tags terminates

1 #include <sys/socket.h>2 ssize_t recv (int sockfd, void *buff, size_t nbytes, int flags); 3 ssize_t Send (int sockfd, const void *buff, size_t nbytes, int flags);

The first 3 parameters of recv and send are equivalent to read and write.

The flags parameter has a value of 0 or:

Flags Description Recv Send
Msg_dontroute Bypass routing Table Lookup
Msg_dontwait This operation is non-blocking only
Msg_oob Send or receive out-of-band data
Msg_peek A glimpse of foreign news
Msg_waitall Wait for all data

1. Send parsing

SOCKFD: Specifies the send-side socket descriptor.

Buff: holds the buffer to send data

Nbytes: The number of bytes of data actually to be improved

Flags: typically set to 0

1) Send compares the length of the send data nbytes and socket SOCKFD the length of the send buffer, if nbytes > Socket sockfd The length of the send buffer, the function returns SOCKET_ERROR;

2) If Nbtyes <= the length of the send buffer sockfd the socket, then send checks to see if the protocol is sending the data in the SOCKFD send buffer, and if it waits for the protocol to send the data out, If the protocol has not started sending data in the SOCKFD send buffer or there is no data in the SOCKFD send buffer, then send compares the remaining space of the SOCKFD send buffer and nbytes

3) If the nbytes > Socket sockfd The length of the remaining space in the send buffer, send waits for the protocol to send the data in the socket SOCKFD send buffer

4) If the Nbytes < socket SOCKFD The amount of space remaining in the send buffer, send will simply copy the data from the BUF to the remaining space (note that it is not sent to the other end of the connection in the send buffer of the socket SOCKFD, but the protocol is transmitted, Send simply copy the data in the BUF to the remaining space in the send buffer of the socket SOCKFD.

5) If the Send function copy succeeds, it returns the actual copy bytes, and if send has an error in copy data, send returns SOCKET_ERROR; If the network disconnects while waiting for the protocol to transmit data, the Send function also returns SOCKET_ERROR.

6) The Send function successfully copied the data from the buff to the remaining space in the SOCKFD to improve the 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 send will have to wait for the data in the socket's send buffer to continue before execution, if a network error occurs while waiting, then the socket function returns SOCKET_ERROR)

7) Under the UNIX system, if send is disconnected while waiting for the protocol to transmit data, the process that calls send receives a sigpipe signal, and the process terminates the processing of the signal.

2.recv function

SOCKFD: Receive End Socket descriptor

Buff: A buffer used to hold the data received by the RECV function

Nbytes: Indicates the length of the buff

Flags: 0 is the general position

1) recv wait for the data of the transmit buffer of s to be passed by the Protocol, if the Protocol has a network error while transmitting the data in the sock send buffer, then the RECV function returns SOCKET_ERROR

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

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

4) Under the UNIX system, if the Recv function waits for the protocol to receive data when the network disconnects, then the process that calls Recv receives a sigpipe signal, and the process terminates the default processing of the signal.

Linux Socket send and RECV functions

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.