Hello Friends,
I have 2 progs, client.c and server.c
I am having a strange problem with my polling function in server program.
The server program contains a function, that is polling on a file descriptor. When poll() syscall detects data to be read on descriptor, I am getting POLLIN in revent.
Now, at last the client closes the socket, at tht time the poll() syscall should give me the POLLHUP in revents. Now, here is the problem, instead of giving me the POLLHUP it continuously giving me the POLLIN revent, so my program goes into infinite loop and CPU usage goes higher and higher.
I have tried to make socket readonly using fcntl(), but I can not get satisfactory results.
Here is the part of code....
Code:
void my_poll()
{
int poll_ret=0,temp=0,len=0;
for(;;)
{
poll_ret = poll(array_fd,2,1000); //struct pollfd array_fd[2]; 1000 milisec timeout
if(poll_ret > 0)
{
for(temp = 0;temp < 2;temp++)
{
if((array_fd[temp].revents & POLLHUP))
{
//at the time of client socket close, I am not reaching here
printf("Got POLLHUP revent. Client has closed the socket");
close(array_fd[temp]);
array_fd[temp].fd =-1;
poll_ret--;
if(poll_ret <= 0)
break;
}
if(array_fd[temp].revents & POLLIN)
{
char buff[255];
//I am getting this revent, and I can read the data
printf("Got POLLIN revent, Now going to read the data...\n");
if( (len = recv( array_fd[temp].fd, buff,sizeof(buff),0)) > 0)
{
printf("Len Of Recv bytes %d \n",len);
}
poll_ret--;
if(poll_ret <= 0)
break;
}//if over
}//for over
}//if over
}//for over
}//end of function