Like Tree2Likes
  • 1 Post By Salem
  • 1 Post By Salem

Using read on tcp socket stream unable to exit loop.

This is a discussion on Using read on tcp socket stream unable to exit loop. within the Linux Programming forums, part of the Platform Specific Boards category; I am have written some code to send a file using TCP sockets in linux. On the receiving end I ...

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,499
    Yes, look up the precedence of = and >
    dtow1 likes this.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,499
    > 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.
    dtow1 likes this.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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, 07: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

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