Poll Not recognizing Socket.

Poll Not recognizing Socket.

Post by Scott Wachtle » Thu, 09 Mar 2000 04:00:00



Hi,
  I'm trying to use poll in a socket program, but it doesn't seem to
recognize with then is a connection on a particular socket. I've
attached a "sample" program below which sort of illustrates my problem.
Poll never returns with a POLLNORM.
  Must I use accept to create a filedescriptor first? If so, how do I
avoid blocking, so that other filedescriptors also get checked? The Poll
man page seems to imply that polling should work after a listen is
called.
  Any help is appreciated.
   Sincerely,
     Scott

#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <poll.h>
#include <fcntl.h>
#include <fcntl.h>

#include <netinet/in.h>
#include <sys/socket.h>

void main(){

 int s;
  struct pollfd poll_cli[2];
  struct sockaddr_in saun;
   int len,result;

if((s=socket(AF_INET,SOCK_STREAM,0))<0){
 perror("server: socket");
 exit(1);
 }

saun.sin_family= AF_INET;
saun.sin_port = htons(38320);
saun.sin_addr.s_addr = htonl(INADDR_ANY);

len = sizeof(saun);
if(bind(s,(struct sockaddr*)&saun,len)<0){
 perror("server: bind");
 exit(1);
 }

if(listen(s,5)<0){
 perror("server: listen");
 exit(1);
 }

poll_cli[0].fd = s;
poll_cli[0].events == POLLNORM;

for(;;){
  if((result=poll(poll_cli,2,30))<0){}

   if(result >0){

    printf("Activity on socket\n");
   if(poll_cli[0].revents == POLLNORM){   /* Never True */
      printf(" POLLNORM \n");
   }

Quote:}
}
}

 
 
 

1. poll() not reporting POLLHUP's on sockets?

On SunOS 5.6/5.7, I run a poll() for POLLIN conditions on a
socket obtained through the following sequence:

socket(AF_INET, SOCK_STREAM, 0) ->
-> bind(...) -> listen(...) -> accept(...)

The client connects through a matching sequence:

socket(...) -> connect(...)

My problem is, when the client shuts down:

shutdown(...) -> close(...) -> exit(...)

the poll() call on the server returns, but the "revents" field
for the socket concerned is set to be POLLIN, not the
POLLHUP I would expect.

Similarly, when I poll() for conditions POLLIN | POLLRDNORM,
I get POLLIN | POLLRDNORM back when the socket is shut
by the client.

When I try to read() from the socket in either case, it
returns 0, not -1, and errno remains 0.

This behavior seems abnormal; any ideas?

Zheng-Zheng Zhou

2. disable StayOnTop in afterstep

3. To POLL or Not To POLL the mouse

4. Linux Tips HOWTO

5. poll.h file and poll function not found

6. ipchains not supported

7. select () does not recognize a socket change

8. Bus error of Netscape under Linux

9. Managing read/write socket without polling

10. poll and tcp sockets defect

11. polling a socket for disconnects...?

12. How to do polling for socket in kernel

13. polling sockets