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)
Bill · Gates attitude towards software is: If you want good software, you have to pay for it.
First, the Linux system startup process About the Linux system startup process we can divide into th
The Linux operating system has been around for 20 years without knowing it. Just 20 years ago, just
The Linux command line is useful, efficient, and fun, but sometimes dangerous, especially if you are
How to hide PHP version in Linux server
How to install and configure network devices on Linux system
Rescue mode after a serious fault in the Linux operating system
Check whether the wireless network card supports creating hotspots under Linux
Simple learning of make and makefile
Basic tutorial on the use of crontab under Linux
How to set Sysctl.conf to improve Linux performance (the most complete sysctl.conf excellent
The difference between Linux and Windows
Linux modify user group method
linux stack of neighborhood subsystem (two related processes)
Win8 download other regional store applications
NetBeans integrated development environment under CentOS
Linux configuration Python environment method
Shutdown does not have to worry. Accelerate Win2003 shutdown operation
How to optimize memory in win7
How to play traditional Chinese characters in Win7 system?
Win10 application can not start properly. Prompt error 0xc000007b Solution
Windows Server 2008 WDS Deployment Client System
Adjusting the maximum number of files displayed by Jumplists in Win 7
How to manually modify the default ost data file location of Outlook 2013 under Win8