OS: Linux
Complier: gcc

I have a TCP listener which accept multiple TCP connections from the server. I am using select() to multiplex these connections. However, my select() always fails in the recv() part of the code. I can see in the application log that all the TCP connections (total 4) are being received from server. But When actual data is sent from server, selec() fails with "bad descriptor" message. Can someone help me out here?


Here is my select() loop. Sorry for the big code.

Code:
tv.tv_sec = 0;
tv.tv_usec = 0;
if (listen(tcpthread->tcpsocket, BACKLOG) == -1) 
{
    perror("listen");
    pthread_exit(NULL);
}
   memset(debugstr,'\0',sizeof(debugstr));
   sprintf(debugstr,"Listening..\n\n");
   dbgtrace(__FUNCTION__,debugstr);
   undbgtrace(__FUNCTION__,debugstr);
	
   FD_SET(tcpthread->tcpsocket,&master);
   fdmax=tcpthread->tcpsocket;
for (;;)
{
    read_fds=master;
    if ((nb=select(fdmax+1,&read_fds,NULL,NULL,&tv)) == -1)
    {	
	perror("select");
	pthread_exit(NULL);
    }
    for (i=0;i<=fdmax;i++)
   {
        if (FD_ISSET(i,&read_fds))
	{
	    if (i == tcpthread->tcpsocket)
	        {
		    addrlen=sizeof(XSocket_in);
		    RemoteSockId=accept(tcpthread->tcpsocket,(XSocket *) &RemoteSock,&addrlen);
		    if (RemoteSockId < 0)
		    {
		         perror("accpet");
			 pthread_exit(NULL);
		    } else {
			 FD_SET(RemoteSockId,&master);
			 if (RemoteSockId > fdmax)
			 {
			     fdmax=RemoteSockId;
			 }
						
			 if (counter < MAX_DISPLAY_CONNS)
			 {
			     tcpthread->tcpconns[counter]=(display_t *) malloc (sizeof(display_t));
			    if (getsockname(RemoteSockId,(XSocket *) &(tcpthread->tcpconns[counter]->local), &sin_size) < 0)
			    {
		                perror("getsockname");
				pthread_exit(NULL);
			    } 

			    memset(srcip,'\0',sizeof(srcip));
    	           strcpy(srcip,inet_ntoa(tcpthread->tcpconns[counter]->local.sin_addr));
							

			if (getpeername(RemoteSockId,(XSocket *) &(tcpthread->tcpconns[counter]->remote),&sin_size) < 0)
			{
			    perror("getpeername");
			    pthread_exit(NULL);
			} 

			memset(dstip,'\0',sizeof(dstip));		strcpy(dstip,inet_ntoa(tcpthread->tcpconns[counter]->remote.sin_addr));

			memset(debugstr,'\0',sizeof(debugstr));
			sprintf(debugstr,"Thread-%d: Display TCP Connection from Remote %s/%d Local %s/%d Socket: %d \n",pthread_self(),dstip, ntohs(tcpthread->tcpconns[counter]->remote.sin_port),srcip,ntohs(tcpthread->tcpconns[counter]->local.sin_port),RemoteSockId);
			dbgtrace(__FUNCTION__,debugstr);
			undbgtrace(__FUNCTION__,debugstr);
			counter++;
                  }     
   		  if (counter == (MAX_DISPLAY_CONNS - 1))
		  {
		       pthread_mutex_lock(&count_mutex);
		       activecount++;
		       pthread_mutex_unlock(&count_mutex);
		       tcpthread->status=OPEN;
		   } else {
			tcpthread->status=NOTOPEN;
		   }
						
		}
            } else {
			bytes_received = (i,(char *)databuff,sizeof(databuff),0);
			if (bytes_received < 0)
			{
		  	     perror("recv");
			     FD_CLR(i,&read_fds);
			     close(i);
     			     for (j=0;j<MAX_DISPLAY_CONNS;j++)
			     {
			         free(tcpthread->tcpconns[j]);
				 tcpthread->tcpconns[j] = NULL;
				 if (delete_conn_from_list(&tcpthread) == TRUE)
				{
				    memset(debugstr,'\0',sizeof(debugstr));
				    sprintf(debugstr,"Connection Entry removed\n");
				    dbgtrace(__FUNCTION__,debugstr);
				}
			 }
			pthread_mutex_lock(&count_mutex);
			activecount--;
			pthread_mutex_unlock(&count_mutex);
			pthread_exit(NULL);
		} else if (bytes_received == 0)
		{
		     FD_CLR(i,&read_fds);
		     close(i);
 		     disconnect++;
		    if (disconnect == MAX_DISPLAY_CONNS)
		    {
			for (j=0;i<MAX_DISPLAY_CONNS;j++)
			{
			    free(tcpthread->tcpconns[j]);
			    tcpthread->tcpconns[j] = NULL;
			    if (delete_conn_from_list(&tcpthread) == TRUE)
			    {
				memset(debugstr,'\0',sizeof(debugstr));
				sprintf(debugstr,"Connection Entry removed\n");
				dbgtrace(__FUNCTION__,debugstr);
			     }
			}
			pthread_mutex_lock(&count_mutex);
			activecount--;
			pthread_mutex_unlock(&count_mutex);
			pthread_exit(NULL);
		    }
		} else {
		     memset(debugstr,'\0',sizeof(debugstr));
		     sprintf(debugstr,"Thread-%d: %d bytes received\n", pthread_self() , bytes_received);
		     dbgtrace(__FUNCTION__,debugstr);
		     printBuffer(databuff,bytes_received);
		     tcpthread->bytesin+=bytes_received;

		     bytes_sent = send(i,(char *)databuff,sizeof(databuff),0);
		     if (bytes_sent < 0)
		     {
			perror("send");
			FD_CLR(i,&read_fds);
			close(i);
			for (j=0;j<MAX_DISPLAY_CONNS;j++)
			{
   			    free(tcpthread->tcpconns[j]);
			    tcpthread->tcpconns[j] = NULL;
			    if (delete_conn_from_list(&tcpthread) == TRUE)
			    {
				memset(debugstr,'\0',sizeof(debugstr));
				sprintf(debugstr,"Connection Entry removed\n");
				bgtrace(__FUNCTION__,debugstr);
			     }
			}
			pthread_mutex_lock(&count_mutex);
			activecount--;
			pthread_mutex_unlock(&count_mutex);
			pthread_exit(NULL);
		    } else {
			memset(debugstr,'\0',sizeof(debugstr));
			sprintf(debugstr,"Thread-%d: %d bytes sent\n",pthread_self(),bytes_sent);
			dbgtrace(__FUNCTION__,debugstr);
			tcpthread->bytesout+=bytes_sent;
		    }
	        }
	    }
        }
    }
}
When I run it I see following output on console

handle_tcp_display_conns : Thread-16387: Display TCP Connection from Remote 10.6.250.60/45870 Local 92.178.18.66/4674 Socket: 7
handle_tcp_display_conns : Thread-16387: Display TCP Connection from Remote 10.6.250.60/45871 Local 10.6.250.55/6005 Socket: 8
handle_tcp_display_conns : Thread-16387: Display TCP Connection from Remote 10.6.250.60/45872 Local 10.6.250.55/6005 Socket: 9
handle_tcp_display_conns : Thread-16387: Display TCP Connection from Remote 10.6.250.60/45873 Local 10.6.250.55/6005 Socket: 10
select: Bad file descriptor <<< It fails here. expecting data from Server
Thanks,