Skip to content

Instantly share code, notes, and snippets.

@pgte
Created September 22, 2010 14:45
Show Gist options
  • Save pgte/591810 to your computer and use it in GitHub Desktop.
Save pgte/591810 to your computer and use it in GitHub Desktop.
/* create socket */
serverSocket = socket(...);
/* bind the socket to an address and port */
bind(serverSocket, ...);
/* socket will listen for incoming connections */
listen(serverSocket, ...);
listen_fds = [];
listen_fds << serverSocket;
timeout = 10ms;
for(;;) {
/* Listen to all file descriptors in listen_fds */
active_fds = listen_on_all(listen_fds, timeout);
if(active_fds.count > 0) {
for_each active_fd in active_fds {
if (serverSocket == active_fd) {
new_fd = accept(serverSocket, (...));
listen_fds << new_fd;
} else {
// handle data from a client
(...)
// disconnect if needed
close(active_fd);
listen_fds.delete(active_fd);
}
}
} else { // if(active_fds.count > 0)
// Timeout
// do some more stuff
do_some_other_stuff(...)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment