Thread: Using read on tcp socket stream unable to exit loop.

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    19

    Using read on tcp socket stream unable to exit loop.

    I am have written some code to send a file using TCP sockets in linux. On the receiving end I have the code fragment shown below. It works great and writes the file correctly, but since read blocks it just sits here once all the bytes have been written.

    I was wondering if anyone could point me in the right direction to break out of the loop once the last byte has been written. Thanks.




    Code:
    	while(read_size = read(sd, message, sizeof(message))>0){
    		written=write(pfd, message, strlen(message)); 
    	}

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Yes, look up the precedence of = and >
    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
    Sep 2011
    Posts
    19
    Thank you for answering. Im not sure if I understand how to apply what you said though. The > has a higher precedence than the =. So the value of read_size is the result of read(..)>0.

    If I change the code to the following, it is still blocking. I made the change to mke sure that read_size is being assigned the result before the comparison though. Thank you for your help.

    Code:
    	while((read_size = read(sd, message, sizeof(message)))>0){
    		written=write(pfd, message, strlen(message)); 
    	}

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > written=write(pfd, message, strlen(message));
    Replace strlen() with read_size

    There's no guarantee that each message is \0 terminated.
    read() certainly doesn't supply one.
    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
    Sep 2011
    Posts
    19
    Thank you Salem, my program is now working.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unable to read input
    By Mentallic in forum C Programming
    Replies: 3
    Last Post: 07-27-2012, 04:00 AM
  2. Unable to read from a file
    By Cnewbi in forum C Programming
    Replies: 7
    Last Post: 01-06-2012, 08:12 AM
  3. Continuous socket stream,
    By zacs7 in forum C Programming
    Replies: 3
    Last Post: 05-08-2007, 04:19 AM
  4. unable to initialize memory stream
    By PING in forum Tech Board
    Replies: 1
    Last Post: 10-22-2005, 10:05 PM
  5. stream socket problem
    By WL in forum C Programming
    Replies: 2
    Last Post: 10-01-2001, 11:07 PM