简体   繁体   中英

Why does my Winsock app sometimes wait at listen() and sometimes at accept()?

Hey guys I'm using Visual C++ 2010, trying to write a server/client app using Winsock... I'm not sure why, but sometimes the server waits at the listen() function, and sometimes at the accept(). Just wondering, what is the mechanism of these two functions? Isn't it supposed to wait at listen() until it hears an incoming connection and then proceed to accept()?

It doesn't. Your observations are faulty. listen() is not a blocking call. All it does is put the port into LISTENING state, and that either succeeds or it fails. There's nothing to block for.

It is accept() which blocks, waiting for an inbound connection.

The listen function puts the socket into listening mode. You have to call this functions only once. Normally, this function returns immediately.

The incoming connections are accepted by calling accept function. In the blocking mode (default) it returns only when a new connection is accepted or an error occurs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM