Comparison of Read, write, send, recv functions in socket __ function

Source: Internet
Author: User

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 this send ()/recv () would be dropped; Perhaps someone with a copy of the POSIX drafts for socket calls can check ...

Portability Note:non-unix systems may isn't allow read ()/write () on sockets, but recv ()/send () are usually OK. This is true in Windows and OS/2, for example.


After the TCP connection is established, we can use the socket as a file descriptor, and then 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 byte content in buf to the file descriptor, returns the number of bytes written successfully, and returns 1. and sets the errno variable. In a Web program, there are two possibilities when we describe the comfortable writing of data to a socket file:

1. The return value of write is greater than 0, which means that some or all of the data is written, so that the data is written in a while loop, but the BUF and nbytes parameters in the loop are updated by ourselves, that is to say, Writing functions in network programming is not responsible for all the data after the completion of the return, perhaps halfway back.

2, the return value is less than 0, this time error, you need to according to the type of error to deal with the corresponding.

If the error is eintr, there is an interrupt error at the time of writing, if the epipe indicates that there is a problem with the network connection.

Read function

ssize_t Read (int fd,void *buf,size_t nbyte)

The read function is responsible for reading content from FD, and when read succeeds, read returns the number of bytes actually read, and if the return value is 0, it indicates that the end of the file has been read and less than 0 indicates a read error.

If the error is eintr, there is an interrupt error at the time of writing, if the epipe indicates that there is a problem with the network connection.

With the above two functions, we can transfer data to the client or server. For example, if I want to transfer a struct, I 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 passing data over a network, we typically convert the data to the char type and receive the same. There is no need to pass pointers over the network.

recv function and send function

The RECV function and the read function provide the same functionality as the read and write functions, with the difference 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 previous three arguments are the same as the read and write functions. The fourth parameter can be 0 or a combination:

Msg_dontroute: Do not lookup table

is a flag used by the Send function, which tells the IP that the destination host does not have the necessary lookup table on the local network, which is commonly used in network diagnostics and routing programs.

Msg_oob: Accept or take out Out-of-band data

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

Msg_peek: View data, do not remove data from system buffers

Is the flag used by the RECV function to read content only from the system buffer, without knowing the contents of the system buffer. This will still be the same content the next time you read it, usually using this flag when you have a process reading and writing data.

Msg_waitall: Waiting for all data

Is the use flag of the RECV function, which means that when all the information arrives, the recv return blocks 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.