Socket Programming select () This article comes from the magical effect: Author: (2001-09-13 09:00:00)
Sending station: Tue Aug 4 15:43:41 1998), transfer [original text by CPU] WINSOCK API users know: Winsock programming is very convenient to be a driving Mechanism, whether it is the underlying API WSaasyncSelect () or the asynchronous Socket class: CasyncSocket, all of the information such as FD_ACCEPT, FD_READ, FD_CLOSE are captured and processed. FD_ACCEPT Notification Process There is a client Socket request connection, and the fd_read notification process Local Socket has Dongdong readable, and the FD_CLOSE notification process The other Socket has been closed. So, is the BSD Socket really trumed? Not too! The 'Cause CPU Love Unix So. BSD UNIX has a system call in the aromatic SELECT () to provide a similar message drive mechanism. CPU solemnly announced: Winsock's WSAAsyncSeclet () is just this SELECT () FORK version! BILL is also forking, Xixi. SELECT () mechanism provides a fd_set data structure, in fact, an array of long types, each array element can handle with an open file (regardless of a Socket handle, or Other files or named pipes or device handles) Establish a connection, the job creation is completed by the programmer, and when calling Select (), modify the content of the fd_set according to the IO status, thereby notifying the process () process Which socket or file readable, the specific explanation below: #include
FD_ISSET (int FD, FDSET * fdset): Check if the FDSET Contacts File Handle FD is readable,> 0 means readable and written. (About FD_SET and related macro definitions /usr/include/s/types.h) This, your socket only needs to be read when there is Dongdong reading, which is approximately as follows: ... int suckfd; fd_set fdr; Struct timeval timeout = ..; ... for (;;) {fd_zero (& fdr); fd_set (sockfd, & fdr); Switch (SockFD 1, & FDR, NULL, & TIMEOUT) {Case-1: Error Handled BY U; CASE 0: Timeout Hanled By U; Default: IF (fd_isset (sockfd)) {now u read or recv something; / * if sockfd is faather and server socket, u can no can access () * /}}} So A FD_ISSET (SockFD) is quite notified with SOCKFD readable. As for the function of Struct Timeval, please man select. Different TimeVal settings make SELECT () exhibit timeout, no timeout blocking and polling three features. Since TimeVal can be accurate to one million seconds, Windows's setTimer () is not countless. You can make a super clock with select (). FD_ACCEPT implementation? Still as above, because the client Socket requests the connection, the connection request message will be sent. At this time, SELECT () will of course end, fd_isset (sockfd) is of course greater than zero, because there is a message to be read! As for this application, mainly in the server's parent Socket, if you don't like active accept (), you can change to accept (). As for the implementation and processing of FD_Close, a pile of CPU processing time is not complete. - Discussion on the problem of using select () detection of the other Socket close: Still a local Socket has Dongdong read, because the other Socket is closed, send a closed connection notification message, will be detected by select (). For TCP connection (three handshake) and close (secondary handshake) mechanism, please refer to books about TCP / IP. I don't know why, UNIX seems to have no notification process about the Socket or PIPE to close the signal, or it may be limited by the CPU.