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 is a socket file?

508


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?

1038


How can I tell when a socket is closed on the other end?

967


How many sockets can a port have?

477


What is active unix domain sockets?

547






Are sockets files?

499


Why do I get EPROTO from read()?

1007


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

1105


Can multiple sockets use the same port?

490


What are the pros/cons of select(), non-blocking I/O and SIGIO?

969


What are the types of sockets?

487


What is sae socket?

436


How do I use TCP_NODELAY?

1105


What is a sae socket?

509


How can I write a multi-homed server?

1273