Complete read/write functions based on Linux network programming

Source: Internet
Author: User
The complete read/write function of Linux network programming-General Linux technology-Linux programming and kernel information. The following is a detailed description. In the embedded Linux system, once a TCP/UDP connection is established, the next step is communication. In Linux, the created Channel is regarded as a file descriptor, so that when the server communicates with the client, everything needs to be read and written into the file descriptor. Just like reading and writing to a file.
1 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 fd. the number of bytes written is returned when the file descriptor fd is successful. -1 is returned in case of failure, and the errno variable is set. In network programs, there are two possibilities when we write to the socket file descriptor.
1) The return value of write is greater than 0, indicating that some or all data is written.
2) If the returned value is less than 0, an error occurs. We need to handle the error according to 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 );
}
2. read function
The ssize_t read (int fd, void * buf, size_t nbyte) 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 the returned value is less than 0, an error occurs. 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 );
}
3. Data transmission
With the above two functions, we can transmit data to the client or the server. For example, we want to pass a structure. You can use the following method:
/* Write the client to the server */
Struct my_struct my_struct_client;
Write (fd, (void *) & my_struct_client, sizeof (structmy_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 transmitting data over the network, we generally convert the data 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)
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.