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

Answer Posted / 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       View All Answers


Please Help Members By Posting Answers For Below Questions

What's better 6pt or 12pt sockets?

479


How is a socket created?

507


How many sockets can a cpu have?

498


Why does it take so long to detect that the peer died?

1148


What is socket address with example?

493






What is the purpose of socket?

503


What is a socket file?

510


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

474


Where is the socket located?

476


Whats the difference between select() and poll()?

1210


Are sockets files?

500


How does a socket work?

491


How do I use TCP_NODELAY?

1107


What is af_inet in socket?

475


How can I set the timeout for the connect() system call?

1107