Complete read/write Functions
Once we establish a connection, our next step is to communicate. in Linux, We will regard the channel we have established as a file descriptor. In this way, when the Server communicates with the client, everything needs to be read and written to the file descriptor. just like reading and writing to a file.
1. Write a 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 written bytes is returned when the Buf is successful.-1 is returned if the Buf fails. The errno variable is set.ProgramWhen we write to the socket file descriptor, there are two possibilities.
(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 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 );
}
2. 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 );
}
3. 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)