strange read() problem

This is a discussion on strange read() problem within the C Programming forums, part of the General Programming Boards category; Hi, I'm doing some raw socket UDP send and receive. After establishing a UDP "connection" using connect()*, I use read() ...

  1. #1
    Registered User
    Join Date
    Jun 2006
    Posts
    16

    strange read() problem

    Hi,

    I'm doing some raw socket UDP send and receive. After establishing a UDP "connection" using connect()*, I use read() and write() on the client side to send/receive char data.

    My client code works if I simply calls read in the following manner
    Code:
    n = read(sd, recBuf, numBytes);
    So I am putting a timeout mechanism around it using alarm():
    Code:
    signal(SIGALRM, sig_alarm);
    alarm(5);
    n = read(sd, recBuf, numBytes);
    //handle errors here; Note: there exists a signal handler for SIGALRM
    alarm(0);
    All of a sudden read() no longer works. I keep getting n = 0 bytes. I changed the server to send nothing and the timeout doesn't work either. The code return almost immediately with n = 0.


    I don't get how setting a signal can affect descriptor operation... SOS

    Thanks


    *UDP is connectionless. However, using connect() on UDP allows me to use read()/write() instead of sendto()/recvfrom().
    Last edited by pppbigppp; 09-25-2006 at 08:32 AM.

  2. #2
    Registered User
    Join Date
    Jun 2006
    Posts
    16
    a side question about read():

    Let say I create a text file containing 6 characters. It seems like when the file is written, it will end up with 7 character. So I guess there is a file delimiter character at the end.

    I want to read 2 char from the file at a time, with up to 3 reads. However, the special character is screwing up read(). read() doesn't seem to get EOF until the 7th character is read. It's identical to the 6th character when I printed it out.

    What's the most elegant way to get around this problem?

    if it helps, I'm using ubuntu and the its gcc package.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,535
    > It seems like when the file is written, it will end up with 7 character.
    In Linux, you get exactly what you asked for.

    Post your code which writes 6 characters, but you end up with 7 in the file.

    You're not making the mistake of using feof() to control a loop, because that too will give the "appearance" of there being more data in the file than there is, which is why you shouldn't do it, and there's a FAQ on the subject.

    > So I am putting a timeout mechanism around it using alarm():
    A better approach would be to use the timeout you get with select()
    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.

  4. #4
    Registered User
    Join Date
    Jun 2006
    Posts
    16
    Thanks for the reply, here is the bash shell code you asked for

    Code:
    echo "hahaha" > data.dat

    Regarding timeout, amazingly, I came to the same conclusion earlier. Still not sure why alarm() mess up read(), but select() handles the job perfect well

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,535
    echo normally appends a newline. Do "ls -l" on the file after you've created it.
    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.

  6. #6
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,163
    Try the -n option with echo:
    Code:
    DESCRIPTION
           NOTE:  your  shell  may have its own version of echo which
           will supercede the version described here. Please refer to
           your  shell's  documentation for details about the options
           it supports.
    
           Echo the STRING(s) to standard output.
    
           -n     do not output the trailing newline
    If you understand what you're doing, you're not learning anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strange behavior when intercepting read() with LD_PRELOAD
    By *DEAD* in forum Linux Programming
    Replies: 2
    Last Post: 05-25-2008, 03:58 AM
  2. Strange problem
    By G4B3 in forum C Programming
    Replies: 6
    Last Post: 05-14-2008, 02:07 PM
  3. Strange problem with classes in header files
    By samGwilliam in forum C++ Programming
    Replies: 2
    Last Post: 02-29-2008, 03:55 AM
  4. help! fifo read problem
    By judoman in forum C Programming
    Replies: 1
    Last Post: 08-16-2004, 09:19 AM
  5. read to end of line problem
    By one1082 in forum C++ Programming
    Replies: 3
    Last Post: 11-07-2003, 03:30 PM

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