Thread: read Causes Freezing?

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    3

    read Causes Freezing?

    Hey all. I'm currently writing a basic TCP client script with basic write and read functions implemented. As I understand, it makes sense to enter an infinite while loop to read responses from the server until read returns zero (in which case break; is sent to break out of the while loop). It makes sense in theory. But, whenever read can't find any bytes to read, it just freezes up my program (I assume until it's given bytes to read). So, is there a way to avoid this? I want to use the infinite while loop technique, but it won't work because instead of returning zero or -1 for error, it just freezes everything.

    Thanks in advance.

    I'm currently trying this:

    Code:
    while((n = read( sockfd, buffer, 200)) > 0)
    	{
    		//n = read( sockfd, buffer, 200);
    		//if(n == 0 || n == -1) exit(1);
    		if(n > 0) buffer[n] = 0;
    		printf( buffer );
    		printf("%d", n);
    		if(n < 1) exit(1);
    	}

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    One way is to use the select() call to determine if there's anything available before you try and read it.
    Select has the ability to return after a timeout.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jul 2008
    Posts
    3
    Quote Originally Posted by Salem View Post
    One way is to use the select() call to determine if there's anything available before you try and read it.
    Select has the ability to return after a timeout.
    So, is read supposed to cause freezing like this? Because, I've been reading tutorials and checking source code all day and I see everyone using the infinite while loop until read returns 0.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well the read will block until there's something to read, or the other end closes the connection.

    The other way would be to make the socket non-blocking, then deal with the return values of -1 AND an errno value of EAGAIN.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Jul 2008
    Posts
    3
    Could you tell me how to make the socket non-blocking?

  6. #6
    Registered User
    Join Date
    Oct 2007
    Posts
    242
    Quote Originally Posted by Masna View Post
    Could you tell me how to make the socket non-blocking?
    Code:
    #include <stdio.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    
    // for fcntl which "stops" the blocking of blocking functions:
    #include <fcntl.h>
    #include <unistd.h>
    
    int main(void)
    {
    int mysocket;
    if( (mysocket = socket(AF_INET, SOCK_STREAM, 0) == -1) return printf("Bye.");
    fcntl(mysocket, F_SETFL, O_NONBLOCK);
    // some more code...
    }
    ---
    BTW, your code is containing many errors.
    Last edited by eXeCuTeR; 07-18-2008 at 04:33 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Read() problems
    By yay4rei in forum C Programming
    Replies: 2
    Last Post: 07-21-2005, 10:47 AM
  2. How can I know the actual bytes read in a file read
    By pliang in forum C++ Programming
    Replies: 1
    Last Post: 06-08-2005, 04:23 PM
  3. What Would You Use To Read User Input?
    By djwicks in forum C Programming
    Replies: 11
    Last Post: 04-05-2005, 03:32 PM
  4. Read Array pro!!Plz help!!
    By Supra in forum C Programming
    Replies: 2
    Last Post: 03-04-2002, 03:49 PM
  5. Help! Can't read decimal number
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 09-07-2001, 02:09 AM