Collect and organize the socket functions read write send and recv

Source: Internet
Author: User

1. send recv is the file descriptor for the set interface.
Read write requires a file descriptor.

Read and write can also process socket fd
The two are consistent on BSD4.4 and will eventually be tuned to a unified kernel processing function, but the recv and send options (flag)


What is the difference between read () and recv ()?
From Andrew Gierth (andrew@erlenstar.demon.co.uk ):

Read () is equivalent to recv () with a flags parameter of 0. other values for the flags parameter change the behaviour of recv (). similarly, write () is equivalent to send () with flags = 0.

It is unlikely that send ()/recv () wocould be dropped; perhaps someone with a copy of the POSIX drafts for socket CILS can check...

Portability note: non-unix systems may not allow read ()/write () on sockets, but recv ()/send () are usually OK. this is true on Windows and OS/2, for example.

========================================================== ========================================================== ======================================

Once we have established a tcp connection, we can use the obtained fd as a file descriptor.

The most basic functions in the network program are the read and write Functions.
Ssize_t write (int fd, const void * buf, size_t nbytes );
The write function writes the nbytes bytes in the buf to the file descriptor fd. the number of bytes written when the operation succeeds. -1. set the errno variable. in network programs, it is possible to write to the socket file descriptor.
1) the return value of write is greater than 0, indicating that some or all data is written. in this way, we use a while loop to keep writing data, but we need to update the buf parameters and nbyte parameters during the loop process. That is to say, the network write function is not responsible for returning all data after writing.
2) If the returned value is less than 0, an error occurs. We need to handle the error based on the error type.
If the error is EINTR, an interruption error occurs during write.
If it is EPIPE, the network connection is faulty (the other party has closed the connection ).
To handle the above situations, we compile a write function to handle these situations.

Int my_write (int fd, void * buffer, int length)
{
Int bytes_left;
Int written_bytes;
Char * ptr;

Ptr = buffer;
Bytes_left = length;
While (bytes_left> 0)
{
/* Start writing */
Written_bytes = write (fd, ptr, bytes_left );
If (written_bytes <= 0)/* Error */
{
If (errno = EINTR)/* interrupt error we continue to write */
Written_bytes = 0;
Else/* No other errors, so I had to retreat */
Return (-1 );
}
Bytes_left-= written_bytes;
Ptr + = written_bytes;/* continue writing from the rest */
}
Return (0 );
}

Read Function
Ssize_t read (int fd, void * buf, size_t nbyte)
The read function reads content from fd. when the read succeeds, read returns the actual number of bytes read. If the returned value is 0, it indicates that the object has been read, and if it is smaller than 0, it indicates that an error has occurred. if the error is EINTR, the read operation is interrupted. if the error is ECONNREST, the network connection is faulty. like above, we also write our own READ function.

Int my_read (int fd, void * buffer, int length)
{
Int bytes_left;
Int bytes_read;
Char * ptr;

Bytes_left = length;
While (bytes_left> 0)
{
Bytes_read = read (fd, ptr, bytes_read );
If (bytes_read <0)
{
If (errno = EINTR)
Bytes_read = 0;
Else
Return (-1 );
}
Else if (bytes_read = 0)
Break;
Bytes_left-= bytes_read;
Ptr + = bytes_read;
}
Return (length-bytes_left );
}

Data Transmission
With the above two functions, we can transmit data to the client or the server. For example, if we want to transmit a structure, we can use the following method:

/* Write the client to the server */

Struct my_struct my_struct_client;
Write (fd, (void *) & my_struct_client, sizeof (struct my_struct );

/* Server-side read */
Char buffer [sizeof (struct my_struct)];
Struct * my_struct_server;
Read (fd, (void *) buffer, sizeof (struct my_struct ));
My_struct_server = (struct my_struct *) buffer;

When data is transmitted over the network, data is generally converted to char type data transmission. when receiving the pointer, it is also worth noting that we do not need to pass the pointer on the Network (because it makes no sense to pass the pointer, we must pass the content pointed to by the pointer)


6.1 recv and send
The recv and send functions provide similar functions as read and write, but they provide the fourth parameter to control read/write operations.

Int recv (int sockfd, void * buf, int len, int flags)
Int send (int sockfd, void * buf, int len, int flags)

The preceding three parameters are the same as read and write parameters. The fourth parameter can be a combination of 0 or below.
_______________________________________________________________
| MSG_DONTROUTE | do not search for a table |
| MSG_OOB | accept or send out-of-band data |
| MSG_PEEK | views data and does not remove data from the System Buffer. |
| MSG_WAITALL | wait for all data |
| -------------------------------------------------------------- |

MSG_DONTROUTE: the flag used by the send function. This flag tells the IP address. The destination host is on the local network and there is no need to look up the table. This flag is generally used in network diagnostics and routing programs.
MSG_OOB: You can receive and send out-of-band data. We will explain this later.

MSG_PEEK: indicates the use of the recv function, indicating that the content is only read from the System Buffer, but not cleared from the system buffer. in this way, the content will remain the same for the next read. this flag can be used when multiple processes read and write data.

MSG_WAITALL is the sign used by the recv function, indicating that all information will not be returned until it arrives. when this flag is used, the recv is blocked until the specified condition is met or an error occurs. 1) when the specified byte is read, the function returns normally. return value is equal to len 2) when the end of the file is read, the function returns normally. return value less than len 3) When an operation error occurs,-1 is returned, and the error is set to the corresponding error code (errno)

Author: "YEYUANGEN's column"

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.