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