Linux network programming - complete read and write functions

  

Computer shop news

1 Write function write ssize_t write(int fd,const void *buf,size_t nbytes) The write function writes the contents of the nbytes byte in the buf to the file descriptor fd. The number of bytes written. Returns -1 on failure and sets the errno variable. In the network program, there are two possibilities when we write to the socket file descriptor. 1) The return value of write is greater than 0, indicating that it is written. Partial or all data. 2) The returned value is less than 0. At this time, an error occurs. We need to process according to the error type. If the error is EINTR, it indicates that an interrupt error occurred during writing. If the IPIPE indicates that the network connection appears. The problem (the other party has closed the connection). In order to deal with the above situation, we write 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 /* There is no way to other errors, we have to retreat */return(-1); } bytes_left-=written_bytes; ptr+=written_bytes; /* Continue writing from the rest of the place */} return(0); } 2 Read function read ssize_t read(int fd,void *buf,size_t nbyte) The read function is responsible for reading the content from fd. When the read is successful, Read returns the number of bytes actually read. If the returned value is 0, it indicates that the end of the file has been read. If it is less than 0, it indicates that an error has occurred. If the error is EINTR, the reading is caused by the interrupt. If it is ECONNREST, the network connection is indicated. Something went wrong. As above, we also write a read function of our own. 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 transfer With the above two functions , we can pass data to the client or the server. For example, we need to pass a structure. You can use the following way /* client writes to the server * /struct my_struct my_struct_client; write (fd, (void *) & My_struct_client,sizeof(struct my_struct); /* server 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 passing data on the network, we generally convert the data into char type data transfer. The same is true when receiving. We don't need to pass pointers on the network (because passing pointers doesn't make any sense) , we must pass the pointer to the content)

Copyright © Windows knowledge All Rights Reserved