Thread: read() does not return 0 when reaches EOF but waits timeout

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    2

    read() does not return 0 when reaches EOF but waits timeout

    Hi all, I've written a readn() functions like the Stevens' well-known one which also specifies a timeout via the select() function:

    Code:
    int readn (int sockfd, void *buff, size_t n, int timeo){
    	int n_left;
    	int n_read;
    	char *ptr;
    	ptr = buff;
    	n_left = n;
    	struct timeval timeout;
    	fd_set fds;
    
    	timeout.tv_sec = timeo;    
      	timeout.tv_usec = 0; 
      	FD_ZERO(&fds);
      	FD_SET(sockfd, &fds);
    
    	while(n_left>0){
    		
    		if (select(sockfd+1, &fds, 0, 0, &timeout)>0){ 
    			if((n_read=read(sockfd,ptr,n_left))<0){	
    				if(errno == EINTR)
    					n_read=0;
    				else{
    					return -1;	
    				}
    			}
    			else if(n_read==0){			
    				printf("n_read=0\n");
    				break;
    			}
    			n_left-=n_read;	
    			printf("%s\n", ptr);
    			ptr+=n_read;			
    			
    		}
    		else{
    			return -1;
    		}
    	}
    	return (n-n_left);
    }
    When i call readn() in my main()

    Code:
    readn(sockfd,buffer,MAXLINE, 5)
    I notice that the inner read() function doesn't return 0 when it reaches EOF (I notice that it has finished reading by printing ptr), but waits the timeout specified by select(). If I don't specify a timeout at all, read() holds forever.

    On the other side, if in my main() I call the simple read() function instead of readn(),

    Code:
    read(sockfd,buffer,MAXLINE)
    it returns 0, working as intended.

    I really can't figure out what the problem may be, since what I see is that the same read() call works as it should outside readn(), and holds until interrupted inside readn(). Thanks for your patience,

    Regards, Stefano

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    What is the value of "timeo" that is being passed to readn().
    Does it timeout after "timeo" seconds or does it wait indefinitely?

  3. #3
    Registered User
    Join Date
    Aug 2009
    Posts
    2
    Quote Originally Posted by itCbitC View Post
    What is the value of "timeo" that is being passed to readn().
    Does it timeout after "timeo" seconds or does it wait indefinitely?
    As you can see my readn() call line is:

    Code:
    readn(sockfd,buffer,MAXLINE, 5)
    so the value of timeo parameter is 5. The inner read() timeouts after 5 seconds (so, select() should not be the problem), and waits indefinitely only if i don't specify a timeout mechanism i.e., i don't use select() at all but simply use a "canonical" readn() implementation:

    Code:
    int readn (int sockfd, void *buff, size_t n, int timeo){
    	int n_left;
    	int n_read;
    	char *ptr;
    	ptr = buff;
    	n_left = n;
    
    	while(n_left>0){
    		
    			if((n_read=read(sockfd,ptr,n_left))<0){	
    				if(errno == EINTR)
    					n_read=0;
    				else{
    					return -1;	
    				}
    			}
    			else if(n_read==0){			
    				printf("n_read=0\n");
    				break;
    			}
    			n_left-=n_read;	
    			printf("%s\n", ptr);
    			ptr+=n_read;
    	}
    	return (n-n_left);
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 6 measly errors
    By beene in forum Game Programming
    Replies: 11
    Last Post: 11-14-2006, 11:06 AM
  2. Linking OpenGL in Dev-C++
    By linkofazeroth in forum Game Programming
    Replies: 4
    Last Post: 09-13-2005, 10:17 AM
  3. DirectInput help
    By Muphin in forum Game Programming
    Replies: 2
    Last Post: 09-10-2005, 11:52 AM
  4. string class errors
    By CodeMonkey in forum C++ Programming
    Replies: 13
    Last Post: 07-20-2003, 11:20 PM
  5. My graphics library
    By stupid_mutt in forum C Programming
    Replies: 3
    Last Post: 11-26-2001, 06:05 PM