How can I listen on more than one port at a time?
Answer / chaitanya
The best way to do this is with the select() call. This tells the kernel to let you know when a socket is available for use. You can have one process do i/o with multiple sockets with this call. If you want to wait for a connect on sockets 4, 6 and 10 you might execute the following code snippet:
fd_set socklist;
FD_ZERO(&socklist); /* Always clear the structure first. */
FD_SET(4, &socklist);
FD_SET(6, &socklist);
FD_SET(10, &socklist);
if (select(11, NULL, &socklist, NULL, NULL) < 0)
perror("select");
Is This Answer Correct ? | 0 Yes | 0 No |
What is the function of socket?
When will my application receive SIGPIPE?
What is a sae socket?
What are the types of sockets?
Is tcp or unix socket faster?
How can I tell when a socket is closed on the other end?
How would I put my socket in non-blocking mode?
How can I set the timeout for the connect() system call?
How can I listen on more than one port at a time?
How should I choose a port number for my server?
What is a socket set used for?
What is the difference between SO_REUSEADDR and SO_REUSEPORT?