Created
September 22, 2010 14:45
-
-
Save pgte/591810 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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