Anti-nuisance lawsuit warning: The purpose of these notes is to remind me, Zoegond, of stuff or to help me work stuff out. They may contain mistakes.

Quick

  • ($a, $b....) = unpack("A2A7...", $packed)
  • push( array, list )

Monday, February 6, 2012

Sockets with select

In the context of a single-threaded server such as you might construct in the absence of fork() on Win32, using mingw and winsock2.h; and suppose you only intend to cater for a single connection, so instead of having a sockets[] array for all the client sockets, you just have one int thesocket.

Do not do an FD_SET(thesocket, &read_fdset), when preparing to select, if thesocket is 0 (as it might be if you haven't had a connection yet or have closed one). This does not, as I expected, harmlessly AND 0 onto the fd_set, it appears to bugger it up completely so select() returns -1 without even waiting.

('Harmlessly AND 0' indeed. I was mixing up AND and OR there)

Also note that a single-threaded server that only expects one connection has to actively reject further connection attempts - you will find that FD_SET(serversocket, &read_fdset) is true after select() on every such attempt. To reject them, just closesocket() them.

Followers

Blog Archive