Differences between Linux I/O functions and Linux I/O functions

Source: Internet
Author: User

Differences between Linux I/O functions and Linux I/O functions

I/O functions in Linux mainly include read, write, recv, send, recvmsg, sendmsg, readv, and writev. This article mainly introduces their usage and differences.

Read function:

#include <unistd.h>
ssize_t read(int fd,void *buf,size_t count);

The read function reads the count byte from the file corresponding to the file descriptor fd and stores it in the buf buffer zone. If count is 0, read returns 0, and no other operations are performed. If the count value is greater than SSIZE_MAX, the result is unpredictable. When the read is successful, the pointer to the file's read position moves backward. The size is the number of bytes successfully read.

If the read operation succeeds, the number of bytes read back is accessed. If the returned value is-1, an error occurs in the read function. If the end of the file is reached, 0 is returned. The ssize data type is different from the int and long data types. It is the number of symbols. In actual implementation, it may be int, or long.

Write function:

#include <unistd.h>
ssize_t write(int fd,const void *buf,size_t count);

The parameter description is similar to read.

 

Recv function:

#include <sys/types.h>
#include <sys/socket.h>
ssize_t recv(int s,void *buf,size_t len,int flags);

The recv function is used to receive data. This function receives data from socket s and stores it in the buffer buf. The buf length is len. the operation method is specified by flags. The first parameter s is the socket file descriptor, it is returned by the system calling the socket function. The second parameter buf is a pointer pointing to the buffer zone of the receiving network. The third parameter len indicates the buffer size, in bytes. The fourth parameter flags is used to set the method for receiving data:

Value and meaning of the recv () function flasgs
MSG_DONTWAIT Non-blocking, return immediately, not waiting
MSG_ERRQUEUE Receive error messages from socket error queue
MSG_OOB Receive external data
MSG_PEEK View data without clearing the Data Buffer
MSG_TRUNC Returns all data, even if the specified buffer is too small.
MSG_WAITALL Wait for all messages

MSG_DONTWAIT: this flag sets a single IO operation as non-blocking, instead of opening the non-blocking flag on the socket, executing IO, and then disabling the non-blocking flag.

MSG_ERRQUEUE: the transmission of the error depends on the protocol used.

MSG_OOB: this flag can receive out-of-band data, rather than general data.

MSG_PEEK: This indicates viewing readable data. After the recv function is executed, the kernel will not discard the data.

MSG_TRUNC: After receiving data, if the user's buffer size is not enough to completely copy the data in the buffer, the data is cut off, only by copying the user's buffer size data, other data will be discarded.

MSG_WAITALL: indicates that the kernel does not return the number of bytes before reading the request. If the system supports this flag, you can remove the readn () function and replace it with the following:

# Define readn (fd, ptr, n) recv (fd, ptr, n, MSG_WAITALL ).

Even if MSG_WAITALL is set, if a catches a signal, B ends the connection, and c encounters an error on the socket, the number of bytes returned by this function is still less than the number of requests.

When the MSG_WAITALL flag is specified, the function copies data of the same length as the user. If the data in the kernel cannot meet the requirements, it will wait until the data is sufficient to return.

The Return Value of the function recv () is the number of successfully received bytes. An error occurs when the returned value is-1.

The value and meaning of the recv function errno
EAGAIN The socket is defined as non-blocking, and the operation uses the blocking method; or the timeout time is defined, but no data is received.
EBADF The parameter s is not a valid descriptor.
ECONNREFUSED The remote host does not allow this operation
EFAULT Receive the pointer of the buffer outside this process
EINTR Received interrupt signal
EINVAL Invalid parameter passed
ENOTCONN Socket s indicates a streaming socket, which is not connected
ENOTSOCK The parameter is not a socket descriptor.

Recv () is usually used for TCP. UDP uses the recvfrom function to receive data. Of course, after the datagram socket is bound to the address and port, you can also use the recv function to receive data.

Recv () Copies data from the receipt buffer of the kernel to the user-specified buffer. When the data in the kernel is smaller than the specified buffer hour, generally (the MSG_WAITALL flag is not used) it copies all the data in the buffer to the user buffer and returns the length of the data. When there is more data in the kernel than the specified buffer, the data in the user-specified length receiving buffer is copied to the user-specified address, and the rest of the data needs to be copied when the receiving function is called next time, after the kernel copies user-specified data, it destroys and adjusts the copied data.

 

Send function:

#include <sys/types.h>#include <sys/socket.h>ssize_t send(int s,const void*buf,size_t len,int flags);

The send function sends the data in the buffer buf to len in the way specified by the socket file descriptor s in flasg (the meaning is the same as that in recv ), the returned value is the number of successful bytes.

Because the data in the buf in the user buffer is sent by sending, it may not be all sent, so check the return value of send. When the return value of send () is smaller than len, some data in the surface buffer is not successfully sent. In this case, you need to resend the remaining data. The common method of sending the remaining data is to offset the data location in the original buf, And the offset is the number of bytes that have been sent successfully.

When send () returns-1, the error is returned.

Readv function:

#include <sys/uio.h>ssize_t readv(int s,const struct iovec*vector,int count);

Read () can receive data from multiple buffers. The readv function reads the data of the count block from the socket descriptor s and places it in the buffer vector. The returned value is the number of bytes of successfully received data. An error occurs when the value is-1.

The parameter vector is a vector pointer, and the structure struct iover is defined in the file <sys/uio. h>:

1 struct iovec2 {3 void * iov_base; // vector buffer address 4 size_t iov_len; // size, in bytes 5}

Writev function:

# Include <sys/uio. h> ssize_t writev (int s, const struct iovec * vector, int count); // do you think it is the same as readv?

 

Summary:

The following table summarizes the differences and features of each function:

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.