Socket Programming SELECT ()

zhaozj2021-02-08  250

Socket Programming SELECT ()

Sending station: South China Net Woodlight Station (Tue Aug 4 15:43:41 1998), transfer

[Original text from the CPU]

I have used Winsock API users know: Winsock programming has a very convenient place to be its

Sitis drive mechanism, whether it is the underlying API WSAAsyncSelect () or an asynchronous Socket class for MFC:

CasyncSocket provides messages such as FD_ACCEPT, FD_READ, FD_CLOSE

The programmer captures and processes. FD_ACCEPT notification process has a client Socket request connection,

FD_READ Notification Process Local Socket has Dongdong readable, FD_CLOSE notification process each other SOCKET has

shut down. So, is the BSD Socket really trumed?

Not too! 'Cause CPU Love Unix So.

A system call in the BSD UNIX is a similar message-driven mechanism.

CPU solemnly announced: Winsock's WSAAsyncSeclet () is just this SELECT () FORK version!

Bill is also forking, Xixi.

The data structure of a fd_set is provided in the mechanism of select (), is actually an array of long types.

Each array element can be handled with an open file (regardless of a socket handle, or other

Document or named pipe or device handle) Establish a connection, the work of establishing the connection is completed by the programmer,

When calling SELECT (), modify the contents of FD_SET according to the IO status, thereby notifying

Which socket or file readable is available in the process of select (), which explains below:

#include

#include

#include

int SELECT (NFDS, Readfds, Writefds, ExcePTFDS, TIMEOUT)

Int nfds;

FD_SET * READFDS, * WRITEFDS, * EXCEPTFDS;

Struct TimeVal * Timeout;

NDFS: SELECT monitored file handle, depending on the number of files opened in the process, is generally set to monitor each file

The maximum file number plus one.

ReadFDS: SELECT Surveillance Reader Handle Collection.

Writefds: Select monitors can be written. Handle collection.

Exceptfds: excellence is a collection of exception files for SELECT.

Timeout: The timeout end time of this SELECT (). (See /usr/sys/select.h,

Can be accurate to one thousand thousands of seconds! )

When the files in ReadFDs or WriteFDs can be read or handable or timeout, this SELECT ()

At the end of the return. The programmer uses the macro provided by a group of systems to be judged at the end of Select ().

Which file is readable or writable. It is readFDS for Socket.

Several related macro releases are as follows:

FD_ZERO (fd_set * fdset): Clear the contact of FDSET with all file handles.

FD_set (int FD, FD_SET * FDSET): Create a file handle FD connection with fdset.

FD_CLR (INT FD, FD_SET * FDSET): Clear the connection of the file handle FD with fdset.

FD_ISSET (int FD, FDSET * fdset): Check if the file handle FD contact for fdset

Ready to write,> 0 means readable and written.

(For the definition of fd_set and related macro, see /us/include/s/types.h), your socket only needs to be read in Dongdong reading, roughly as follows:

...

Int sockfd;

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 now

ACCEPT () * /

}

}

}

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. due to

TimeVal can be accurate to one million seconds, so Windows's setTimer () is not considered.

what. You can make a super clock with select ().

FD_ACCEPT implementation? Still as above, because the client Socket requests to connect, will send

Connection request packet, SELECT () is of course end, fd_isset (sockfd) is of course large

Zero, because there is a message to be read! As for this application, mainly in the party's father

Socket, if you don't like active accept (), you can change to accept () as above.

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 () to detect the other Socket close:

Still a local socket has Dongdong read, because the other Socket is closed, it will send a closed connection.

Inform the message, it will be detected by select (). About TCP connection (three handshake) and close

Close (secondary handshake) mechanism, please refer to books about TCP / IP.

I don't know why, UNIX seems to have not provided notification process About Socket or Pipe other party close

The signal may also be limited by the CPU. In short, when the other party is closed, a RECV () or read (),

Return to -1 immediately, at this time, the value of Errno in the global variable is 115, the corresponding sys_errlist [errno]

For "Connect Refused" (please refer to /usr/include/sys/errno.h). So, on

In the for (;;) ... Select () block, when there is something readable, you must check RECV () or

Return value of READ (), return -1 to process the processing of turning off local socket, otherwise select () will

Always think that there is something to read, and the result has a few CPUs to break the pin. Don't believe you can try:

Check RECV () returns the result, and will write to the standard output from the stuff (actually confidant) ...

There are similar problems in the programming of the famous pipeline. For details, please refer to: Publish a useful

Socket customer square original code. As for the processes that the other party suddenly close, the other party can simply capture the signal SIGPIPE and make

There is a handling of the local Socket, etc. Sigpipe's explanation is: Write the pipeline without reader.

This is not described here, please detail Man Signal.

The above is the experience of the CPU accumulated in TCP / IP data transmission experiments. If there is an error, please mad.

Hey, yesterday was born in the Hacker District, gains a short way. Ren CPU (Pentium's Heart) Z80

Supplementation About SELECT in asynchronous (non-blocking) Connect, just started to engage in Socket programming

I have always used blocking connect, non-blocking Connect's problem is because of the proxy scan at the time.

提 提

By communication with netizens and finding related FAQ, I finally know how to solve this problem. Similarly

Use SELECT to solve this problem very well. The process is like this:

1. Set the open Socket to non-blocking, you can use FCNTL (socket, f_setfl, o_ndlay)

Become (some system can also use FnedLay).

2. Send the connection call, return -1, but Errno is set to EinProgress, meaning that CONNECT is still

It has not been completed yet.

3. Set the open socket to be monitored (note that it is not readable) file set with SELECT,

If you can write, use

GetSockopt (socket, SOL_Socket, SO_ERROR, & ERROR, SIZEOF (Int);

Come get the value of Error, if zero, then Connect is successful.

You can see similar procedures in many UNIX version of the ProxyScan program, in Solaris Essence

Zone -> Programming Tips There is a connect module with long-time parameters.

转载请注明原文地址:https://www.9cbs.com/read-1665.html

New Post(0)