PassFcntlSet File Description Descriptor PropertiesFcntl is the use of F_SETFL,F_GETFL, set the file of flags, clogging is set to non-clogging, non-clogging is set to plug (this can be encapsulated as a basic function in server development)1. Get the flags of the file, which is the second parameter of the Open function:
Flags = FCNTL (fd,f_getfl,0);
2, set the file flags:
Fcntl (Fd,f_setfl,flags);
3. Add a flags for the file. For example, the file is blocked and you want to set it to non-clogging:
Flags = FCNTL (fd,f_getfl,0); Flags |= O_nonblock; Fcntl (Fd,f_setfl,flags);
Or one step the way:
Fcntl (Socket,f_setfl,fcntl (SOCKET,F_GETFL) | O_nonblock);
4, cancel a file of a flags, for example, the file is non-clogging, want to set up a blockage:
Flags = FCNTL (fd,f_getfl,0); Flags &= ~o_nonblock; Fcntl (Fd,f_setfl,flags);
PassSocket APIcreate a non-cloggingSocket
</pre><pre name= "code" class= "CPP" > #include <sys/types.h> #include <sys/socket.h>int socket ( int domain,int type,int protocal);
TypeSpecify the type of service, the type of service is mainlySock_streamServices,Sock_dgreamservice, it is worth noting that theLinuxKernel version number2.6.17up,typeParticipants can accept the following two logos at the same time as these services:Sock_nonblock,sock_cloexec,each of them represents the newly createdsockset to non-clogging, as well as inForkcall when creating a child process is closed in a child processsocket.However, the previous version number is not supported.
1. Creating blockagesSocket
int fd_sock = socket (af_inet,sock_stream,0);
2. create non-clogging sockets
int fd_sock = socket (af_inet,sock_stream| sock_nonblock,0);
The above code indicates that a new socket created is non-clogging
Linux File Description Descriptor set to non-clogging method