Why does windows select () not always notify thread B select () when thread A closes its end to a pair of sockets?

The situation under Windows XP (SP3) started me up, and I get to the end of my cable, so maybe someone can give some inspiration.

I have a network program in C ++ (without a GUI). This program is built to compile and run on Windows, MacOS / X, and Linux, so it uses select () and non-blocking I / O as the basis for the event loop.

In addition to its network responsibilities, this program should read text commands from stdin and gracefully exit when stdin closes. On Linux and MacOS / X, this is simple enough - I just include STDIN_FILENO in my read fd_set to select (), and select () returns when stdin is closed. I check that FD_ISSET (STDIN_FILENO, & readSet) is true, try reading some data from stdin, recv () returns 0 / EOF, and so I exit the process.

On Windows, on the other hand, you cannot select STDIN_FILE_HANDLE because it is not a real socket. You also cannot do non-blocking reads on STDIN_FILE_HANDLE. This means that there is no way to read stdin from the main thread, since ReadFile () can be blocked indefinitely, as a result of which the main thread stops serving its network function.

No problem, I say, I'll just create a thread to handle stdin for me. This thread will run in an infinite loop, blocking ReadFile (stdinHandle), and whenever ReadFile () returns data, stdin-thread will write this data to the TCP socket. This socket connection of the other end will select () 'd on the main thread, so the main thread will see the stdin data included in the connection and process the β€œstdin” in the same way as in any other OS. And if ReadFile () returns false to indicate that stdin is closed, stdin-thread just closes the end of the pair of sockets so that the main thread is notified via select (), as described above.

, Windows socketpair(), , listen(), connect() accept() ( CreateConnectedSocketPair() . , , .

, 100%. , stdin , , , , stdin . , ( printf() - ), stdin-thread closesocket() , , - select() - (, ), select() , ... , - , - , FD_ISSET (main_thread_socket_for_socket_pair, & readSet ) 0, .

, , , Windows select() , - select() , stdin -. ? ( , Windows 7, )

+5
6

, , Windows stdin. , , , , , , , .

, ; , , .

-Jeremy

+1

, ? . , CreateConnectedSocketPair() , stdin- ?

0

. CreateConnectedSocketPair() socket1 (), newfd send/recv. , "socket1 = newfd"? listenfd?

0

, - "stdin " TCP-, , , , " stdin "

0

, , .

, , (FIN) .

, , . () , , . , - " " / , , .

, , wirehark FINs RST ( -). FIN, , . RST, , , "ping" , , - .

0

, select(), , , .

: " STDIN_FILE_HANDLE. , stdin , ReadFile() ", . ReadConsoleInput, WSAEventSelect WaitForMultipleObjects. stdin , , ReadConsoleInput ( select() Unix).

ReadFileEx WaitForMultipleObjectsEx, APC ( , , WaitForMultipleObjectsEx ).

- stdin, , ( closesocket ). select() , fds, , .

, , -. : " , , , ". " ", shutdown(), closesocket().

0

All Articles