socket blocking problem

This is a discussion on socket blocking problem within the Linux Programming forums, part of the Platform Specific Boards category; select() is staying blocked after close() is called, surely this isn't normal? The program connects to my local webserver and ...

  1. #1
    Registered User
    Join Date
    May 2006
    Location
    England
    Posts
    2

    socket blocking problem

    select() is staying blocked after close() is called, surely this isn't normal? The program connects to my local webserver and waits for input, about 1 second after waiting, threadfunction calls close(), select never returns. Well that's what I get when the program is run from a shell, if it's run in gdb, select returns -1 and errno is 9 (bad file descriptor).

    Is this normal behaviour? ...I can't see how it is. Can somebody please compile my code and report the behaviour. I'm feeling my system is screwed up

    Code:
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <sys/select.h>
    #include <unistd.h>
    #include <netinet/in.h>
    #include <pthread.h>
    #include <stdio.h>
    #include <errno.h>
    
    void * threadfunction(void *arg)
    {
    	printf("threadfunction started\n");
    	
    	int fd=*(int *)arg;
    	
    	sleep(1);
    	
    	printf("closing socket\n");
    	close(fd);
    }
    
    int main()
    {
    	int fd=socket(AF_INET,SOCK_STREAM,0);
    	
    	struct sockaddr_in addr;
    	addr.sin_family=AF_INET;
    	addr.sin_addr.s_addr=inet_addr("127.0.0.1");
    	addr.sin_port=htons(80);
    	
    	connect(fd,(struct sockaddr *)&addr,sizeof(struct sockaddr_in));
    	
    	fd_set readfds;
    	FD_ZERO(&readfds);
    	FD_SET(fd,&readfds);
    	
    	pthread_t thread;
    	pthread_create(&thread,NULL,threadfunction,&fd);
    	
    	printf("calling select()\n");
    	int ret=select(fd+1,&readfds,NULL,NULL,NULL);
    	
    	printf("select returned %i\n",ret);
    	
    	printf("errno is %i\n",errno);
    	
    	return 0;
    }
    gcc test.c -o test -lpthread

    If it needs to be known, i'm running:
    Slackware 10.2
    Linux kernel 2.4.31
    libpthread-0.10.so
    libc-2.3.5.so

    Thanx

  2. #2
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by killerbyte
    select() is staying blocked after close() is called, surely this isn't normal?
    Code:
    	int ret=select(fd+1,&readfds,NULL,NULL,NULL);
    The last argument is a pointer to a "struct timeval" whose members (tv_sec, tv_usec) tell the kernel how long to wait. If the last argument is NULL, then, indeed, it will block.

    You could try something like the following, (for a 2 second timeout):
    Code:
        struct timeval timeout = {2, 0};
    .
    .
    .
        printf("calling select()\n");
        ret = select(fd , &readfds, NULL, NULL, &timeout);
        printf("select returned %i\n", ret);

    D
    Last edited by Dave Evans; 05-21-2006 at 12:59 PM.

  3. #3
    Registered User
    Join Date
    May 2006
    Location
    England
    Posts
    2
    Ok, select times out, returns 0, how can I tell if the socket has been closed? If the socket isn't closed, i need to keep reading. Yes, the code i've provided doesn't entirely replicate the original code I was testing, just to show select doesn't return when it should.

    Anyway, i've solved the problem now. Select unblocks when I use shutdown() instead of close().

    Thanx

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Socket Destination Port Problem
    By radeberger in forum Networking/Device Communication
    Replies: 15
    Last Post: 03-26-2009, 11:00 AM
  2. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  3. socket message sending and receiving problem
    By black in forum C Programming
    Replies: 5
    Last Post: 01-15-2007, 03:46 AM
  4. sockets problem programming
    By kavejska in forum C Programming
    Replies: 0
    Last Post: 07-25-2005, 07:01 AM
  5. Client/Server Socket Receive Problem
    By mariabair in forum Networking/Device Communication
    Replies: 6
    Last Post: 12-25-2003, 09:01 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21