Reputation: 148
I created a class that implements the select function of the tcpsocket. (listenSock is a vector of TcpSocket - that works) I dont understand why but the second time select is called the program stop working.
TCPSocket* MultipleTCPSocketsListener::listenToSocket(){
//TODO: create local set for the select function (fd_set)
fd_set set;
FD_ZERO (&set);
FD_SET (0, &set);
//TODO: fill the set with file descriptors from the socket list using (FD_SET macro)
for ( int i = 0; i < listenSock.size(); i++ )
{
FD_SET (listenSock.at(i)->getSock(), &set);
}
//TODO: perform the select
int sel=select(sizeof(set)*8,&set,NULL,NULL,NULL);
//TODO: check the returned value from the select to find the socket that is ready
if (sel==-1) {
perror("select failed");
return NULL;
}
//TODO: if select return a valid socket return the matching TCPSocket object otherwise return NULL
if (sel > 0)
{
for ( int i = 0; i < listenSock.size(); i++ )
{
if (FD_ISSET(listenSock.at(i)->getSock(), &set)) return listenSock.at(i);
}
}
return NULL;
}
Upvotes: 2
Views: 659
Reputation: 2292
FD_SET (0, &set);
is useless. You instruct select to treat stdin as one of the sockets to watch.
Upvotes: 0