Comparison of read, write, send, and Recv functions in socket

Source: Internet
Author: User

After the TCP connection is established, we can use the obtained socket as a file descriptor. As a result, we think of the basic Read and Write Functions in the network program.

Write function

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. The number of written bytes is returned successfully, and-1 is returned if a failure occurs. Set the errno variable. In network programs, there are two possible reasons for describing comfortable Data Writing to socket files:

1. the return value of write is greater than 0, indicating that some or all data is written, so that data is continuously written in a while loop, however, the Buf and nbytes parameters in the loop process are updated by ourselves. That is to say, in network programming, writing a function is not responsible for returning all the data after writing it. Maybe it will be returned in the middle!

2. If the returned value is less than 0, an error occurs. Handle the error according to the error type.

If the error is eintr, it indicates that an error occurs during the write process. If the error is epipe, it indicates that the network connection is faulty.

Read Function

Ssize_t read (int fd, void * Buf, size_t nbyte)

The READ function reads content from Fd. When the read is successful, the READ function returns the actual number of bytes read. If the returned value is 0, the read has ended, if the value is smaller than 0, it indicates a read error.

If the error is eintr, it indicates that an error occurs during the write process. If the error is epipe, it indicates that the network connection is faulty.

With the above two functions, we can transmit data to the client or server! For example, if you want to transmit a struct, you can use the following method:

Client to server:

Struct student Stu;

Write (sock, (void *) & Stu, sizeof (struct student ));

Server read:

Char buffer [sizeof (struct student)];

Struct * my_student;

Read (sock, (void *) buffer, sizeof (struct student ));

My_student = (struct student) buffer;

When data is transmitted over the network, we generally convert the data to the char type and receive the data in the same way. There is no need to pass pointers on the network.

Recv and send Functions

The Recv and read functions provide the same functions as the Read and Write functions. The difference is that they provide four parameters.

Int Recv (int fd, void * Buf, int Len, int flags)

Int send (int fd, void * Buf, int Len, int flags)

The preceding three parameters are the same as the Read and Write Functions. The fourth parameter can be 0 or a combination:

Msg_dontroute: no table search

Is the flag used by the send function. This flag tells the IP address that there is no need to search for a table on the local network of the target host. This flag is generally used in network diagnostics and routing programs.

MSG_OOB: accept or out-of-band data

Indicates that out-of-band data can be received and sent.

Msg_peek: Views data and does not remove data from the system buffer.

Is a flag used by the Recv function, indicating that the content is only read from the System Buffer, but the content of the system buffer is not clear. In this way, the content is still the same during the next read, which is usually used when a process reads and writes data.

Msg_waitall: waiting for all data

It is the flag used by the Recv function, indicating that it will not be returned until all information arrives. When this flag is used, the Recv return will be blocked until the specified condition is met or an error occurs.

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.