I am currently creating an echo server that disconnects clients after a maxWaitTime of being in idle.
I was hoping the program would block the socket until the clien
As others have commented, you have some logic holes in your code. By your own admission:
I know retval = 0 whenever it goes through the select and that the fd_set sock goes to [256, (31 zeroes)] and after the select, sock goes to [32 zeroes].
That should have been an indication to you that something was going wrong. The socket was not in the fd_set
after select()
exited, which meant the socket was not readible yet. retval=0
means select()
timed out.
You have to reset not only the fd_set
every time select()
is called, but also the timeval
as well. Try this instead:
int opt = 3;
setsockopt(sockfd, SOL_SOCKET, SO_RCVLOWAT,&opt,sizeof(opt));
for ( ; ; )
{
timeout.tv_sec = maxWaitTime;
timeout.tv_usec = 0;
FD_ZERO(&sock);
FD_SET(sockfd,&sock);
int retval = select(sockfd+1, &sock, NULL, NULL, &timeout);
if (retval <= 0)
{
quitProgram(number); /* error or connection timed out */
}
else
{
if ( (n = Readline(sockfd, line, MAXLINE)) <= 0)
{
return; /* error or connection closed by other end */
}
Writen(sockfd, line, n);
}
}