Detecting connection requests without looping
I need to implement some kind of server which first waits for user开发者_StackOverflow中文版s to connect. I use C++ and SDL_Net, and my code currently checks socket activity in a loop. The problem is that is consumes a lot of CPU performance doing nothing actually, so I'm wondering if there's a way to detect connection request/socket activity with an event or some kind of callback registration? I'm not afraid of WinAPI so any solution is welcome. Thank you in advance,
Vincent
You can use an SDLNet_SocketSet
, create one, then check with SDLNet_CheckSockets
and SDLNet_SocketReady
What socket API are you using? Winsock? If so, you need to call listen()
on the socket to wait for incoming connections. This call will block the calling thread, so no CPU time is used while waiting.
See http://msdn.microsoft.com/en-us/library/ms738545(v=vs.85).aspx for more details.
精彩评论