How can I listen on more than one port at a time?



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

Post New Answer

More Unix Socket Programming Interview Questions

system choose one for me on the connect() call? Should I bind() a port number in my client program, or let the?

0 Answers  


How does unix socket work?

0 Answers  


How do I get the port number for a given service?

1 Answers  


What's the difference between impact sockets and regular sockets?

0 Answers  


What exactly does SO_REUSEADDR do?

1 Answers  






How often should I re-transmit un-acknowleged messages?

1 Answers  


What is a socket file?

0 Answers  


What exactly does SO_KEEPALIVE do?

1 Answers  


Is there any advantage to handling the signal, rather than just ignoring it and checking for the EPIPE error? Are there any useful parameters passed to the signal catching function?

0 Answers  


What is the difference between connected and unconnected sockets?

1 Answers  


How can my client work through a firewall/proxy server?

1 Answers  


How would I put my socket in non-blocking mode?

1 Answers  


Categories