Click to See Complete Forum and Search --> : select()


MasterDucky
November 14th, 2009, 07:19 AM
In this Beej's example i dont understand how being in the main infinite loop with select() can the program continue listening and fetching new sockets
when its listening out of the loop.

I understand that select() is non-blocking but the program flow is still in an infinite loop.


int main(void)
{
fd_set master; // master file descriptor list
fd_set read_fds; // temp file descriptor list for select()
int fdmax; // maximum file descriptor number

int listener; // listening socket descriptor
int newfd; // newly accept()ed socket descriptor
struct sockaddr_storage remoteaddr; // client address
socklen_t addrlen;

char buf[256]; // buffer for client data
int nbytes;

char remoteIP[INET6_ADDRSTRLEN];

int yes=1; // for setsockopt() SO_REUSEADDR, below
int i, j, rv;

struct addrinfo hints, *ai, *p;

FD_ZERO(&master); // clear the master and temp sets
FD_ZERO(&read_fds);

// get us a socket and bind it
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE;
if ((rv = getaddrinfo(NULL, PORT, &hints, &ai)) != 0) {
fprintf(stderr, "selectserver: %s\n", gai_strerror(rv));
exit(1);
}

for(p = ai; p != NULL; p = p->ai_next) {
listener = socket(p->ai_family, p->ai_socktype, p->ai_protocol);
if (listener < 0) {
continue;
}

// lose the pesky "address already in use" error message
setsockopt(listener, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int));

if (bind(listener, p->ai_addr, p->ai_addrlen) < 0) {
close(listener);
continue;
}

break;
}

// if we got here, it means we didn't get bound
if (p == NULL) {
fprintf(stderr, "selectserver: failed to bind\n");
exit(2);
}

freeaddrinfo(ai); // all done with this

// listen
if (listen(listener, 10) == -1) {
perror("listen");
exit(3);
}

// add the listener to the master set
FD_SET(listener, &master);

// keep track of the biggest file descriptor
fdmax = listener; // so far, it's this one

// main loop
for(;;) {
read_fds = master; // copy it
if (select(fdmax+1, &read_fds, NULL, NULL, NULL) == -1) {
perror("select");
exit(4);
}

// run through the existing connections looking for data to read
for(i = 0; i <= fdmax; i++) {
if (FD_ISSET(i, &read_fds)) { // we got one!!
if (i == listener) {
// handle new connections
addrlen = sizeof remoteaddr;
newfd = accept(listener,
(struct sockaddr *)&remoteaddr,
&addrlen);

if (newfd == -1) {
perror("accept");
} else {
FD_SET(newfd, &master); // add to master set
if (newfd > fdmax) { // keep track of the max
fdmax = newfd;
}
printf("selectserver: new connection from %s on "
"socket %d\n",
inet_ntop(remoteaddr.ss_family,
get_in_addr((struct sockaddr*)&remoteaddr),
remoteIP, INET6_ADDRSTRLEN),
newfd);
}
} else {
// handle data from a client
if ((nbytes = recv(i, buf, sizeof buf, 0)) <= 0) {
// got error or connection closed by client
if (nbytes == 0) {
// connection closed
printf("selectserver: socket %d hung up\n", i);
} else {
perror("recv");
}
close(i); // bye!
FD_CLR(i, &master); // remove from master set
} else {
// we got some data from a client
for(j = 0; j <= fdmax; j++) {
// send to everyone!
if (FD_ISSET(j, &master)) {
// except the listener and ourselves
if (j != listener && j != i) {
if (send(j, buf, nbytes, 0) == -1) {
perror("send");
}
}
}
}
}
} // END handle data from client
} // END got new incoming connection
} // END looping through file descriptors
} // END for(;;)--and you thought it would never end!

return 0;
}

hoxsiew
November 14th, 2009, 10:31 PM
Listen will block until there is a connection. Once a connection is established, "listener" is added to the select pool with FD_SET, then in the main loop the first thing checked is if the socket to be serviced is the listener, in which case, a new socket is created with accept() and added to the pool, else, service any of the connection sockets in the pool.

MasterDucky
November 15th, 2009, 08:40 AM
Thanks Hoxsiew i get it better now! :)

So accept() can create as many sockets as it is precised in listen().

hoxsiew
November 15th, 2009, 09:15 AM
I don't understand the question. Select doesn't create anything, it works on a socket pool represented by the fd_set structure. Sockets are added to the pool with the FD_SET macro.

MasterDucky
November 15th, 2009, 02:59 PM
Yes but i thought that its the


socket(p->ai_family, p->ai_socktype, p->ai_protocol);


function that creates the sockets and it is outside of the main loop.

hoxsiew
November 15th, 2009, 04:48 PM
I'm not totally sure why that loop is doing what it is doing, but it loops through the interfaces until it gets a valid socket, then drops out so only one "listener" is established.

Payne747
November 19th, 2009, 05:13 PM
I'm not sure if it's the same issue, but in my select() tests, I was encountering an infinite loop. You need to make sure you're breaking out the loop when the socket is closed by the remote host (i.e. when recv() returns 0). select() will still flag a socket as being ready to read if it's been closed and you've not done anything about it.

MasterDucky
November 20th, 2009, 02:05 PM
I think that this part should take care of it


if (nbytes == 0) {
// connection closed
printf("selectserver: socket %d hung up\n", i);
} else {
perror("recv");
}
close(i); // bye!
FD_CLR(i, &master); // remove from master set



For me its working well only i had to switch the if with the else statement.