Sockets: write() writes OK, yet read() reads garbage...?

Sockets: write() writes OK, yet read() reads garbage...?

Post by simon gola » Tue, 28 May 1996 04:00:00



Strange thing happens. There is a TCP socket established between A and B. Some
information flows OK between the two. However, one particular sequence of
communication does not. A writes a command to B, and immediately goes into
read() for a responce. B does something with the message and writes back to A a
struct, with some fields set to important values. When A reads it (as the same
struct - both use the same header file), however, it contains garbage. All the
fiels are set to 0 (zero). And it reads the correct size of the struct:

  n = read( sock, &preamble, sizeof(preamble) );
  if( n < 0 || n > sizeof(preamble) )
   error...

What/where can the problem be? Does A (or maybe B) have to flush the socket
after it wrote a command, to make sure it does not read its own packet back?

Thanks.
--
Simon   B-)>

 
 
 

Sockets: write() writes OK, yet read() reads garbage...?

Post by edma » Wed, 29 May 1996 04:00:00


: Strange thing happens. There is a TCP socket established between A and B. Some
: information flows OK between the two. However, one particular sequence of
: communication does not. A writes a command to B, and immediately goes into
: read() for a responce. B does something with the message and writes back to A a
: struct, with some fields set to important values. When A reads it (as the same
: struct - both use the same header file), however, it contains garbage. All the
: fiels are set to 0 (zero). And it reads the correct size of the struct:

:   n = read( sock, &preamble, sizeof(preamble) );
:   if( n < 0 || n > sizeof(preamble) )
:    error...

What about (n < sizeof(preamble)), or (n = 0)?

In the first case, all the information is not yet written, in the second, the
other side of the connection has gone away....

Before going any further, are processes A and B on the same physical machine?

Also sizeof may not work, as the structure may be getting padded for alignment;
I had a case where I needed to change my structure declarations (longs first,
then shorts, then chars...there's "packed struct", but my compiler wasn't happy
with that.

Ed Atlee


 
 
 

Sockets: write() writes OK, yet read() reads garbage...?

Post by simon gola » Wed, 29 May 1996 04:00:00




> Newsgroups: comp.unix.programmer
> Date: 28 May 1996 14:35:37 GMT
> Organization: FAA Technical Center, Pomona, NJ


> : Strange thing happens. There is a TCP socket established between A and B. Some
> : information flows OK between the two. However, one particular sequence of
> : communication does not. A writes a command to B, and immediately goes into
> : read() for a responce. B does something with the message and writes back to A a
> : struct, with some fields set to important values. When A reads it (as the same
> : struct - both use the same header file), however, it contains garbage. All the
> : fiels are set to 0 (zero). And it reads the correct size of the struct:

> :   n = read( sock, &preamble, sizeof(preamble) );
> :   if( n < 0 || n > sizeof(preamble) )
> :    error...

> What about (n < sizeof(preamble)), or (n = 0)?

Sorry, forgot to mention. Yes for both. And the de* tells me that the correct
size was read (n is 18 bytes, which is what I wrote).

Also, both processes do run on the same physical machine.

--
Simon   B-)>

 
 
 

Sockets: write() writes OK, yet read() reads garbage...?

Post by edma » Wed, 29 May 1996 04:00:00



: > What about (n < sizeof(preamble)), or (n = 0)?

: Sorry, forgot to mention. Yes for both. And the de* tells me that the correct
: size was read (n is 18 bytes, which is what I wrote).
:  
: Also, both processes do run on the same physical machine.

: Simon   B-)>

Almost sounds like some kind of pointer problem then, but you said that
other exchanges work okay, so I don't know...let you know if anything comes to
mind, though...

Ed

 
 
 

Sockets: write() writes OK, yet read() reads garbage...?

Post by Dr. Charles E. Campbell J » Thu, 30 May 1996 04:00:00



> Strange thing happens. There is a TCP socket established between A and B. Some
> information flows OK between the two. However, one particular sequence of
> communication does not. A writes a command to B, and immediately goes into
> read() for a responce. B does something with the message and writes back to A a
> struct, with some fields set to important values. When A reads it (as the same
> struct - both use the same header file), however, it contains garbage. All the
> fiels are set to 0 (zero). And it reads the correct size of the struct:

>   n = read( sock, &preamble, sizeof(preamble) );
>   if( n < 0 || n > sizeof(preamble) )
>    error...

    If the two processes are not on the same type of machine, the structure's
    components may not be compatable.

    Floats, doubles, and especially *structures* are difficult to pass along
    using sockets.  Personally, I usually just write-to-ascii and transfer
    that.  There is *no* problem with passing such things between processes
    on the same machine or between the same type of machine.

    Different machines, however, may represent floating point in different
    ways (sometimes the same machine can have different ways to represent
    floats, for example, an SGI's C/C++ compilers' floats are not the same
    as those that get loaded into its homogeneous transform hardware).

    Many machines these days do use IEEE's standard for floating point, and
    so floats between those machines *may* transfer -- test and see.

    Even integers have problems: big-endian, little endian (ie. which byte
    is high, which low).

    Structures are a worse problem.  Not only may they include multibyte
    integers, floats, and doubles, with all the problems mentioned above,
    different compilers may do different things to satisfy memory
    constraints (such as, floats must begin on addresses evenly divisible
    by 8, etc).  They insert "padding" bytes (unused null bytes) in various
    places, and not even all compilers for the same platform may do this
    the same way.

    Your machine may support "XDR: External Data Representation standard"
    (1014 Sun Microsystems, Inc., 1987 June; 20p Format: TXT=39316 bytes)
    If so, check out "man xdr_simple" for such functions as xdr_bool,
    xdr_double, xdr_float, etc.  You may use these functions to describe
    simple data structures in a machine-independent fashion.

    The Simple Sockets Library (see
    http://www.auroraonline.com/sock-faq/ssl.tar.gz)
        supplies Sprintf() which I find rather useful to handle transferring
    data.  Its not perfect; ideally there'd be a way to transfer floats etc
    portably.  At least tcp/ip comes with a "net-standard" way to transfer
    integers.

    Good Luck!

--
        Charles E Campbell, Jr, PhD            _   __   __      
        Goddard Space Flight Center           / /_/\_\_/ /      


 
 
 

Sockets: write() writes OK, yet read() reads garbage...?

Post by Aaron 'Shadow' Jackso » Thu, 30 May 1996 04:00:00



> Strange thing happens. There is a TCP socket established between A and B. Some
> information flows OK between the two. However, one particular sequence of
> communication does not. A writes a command to B, and immediately goes into
> read() for a responce. B does something with the message and writes back to A a
> struct, with some fields set to important values. When A reads it (as the same
> struct - both use the same header file), however, it contains garbage. All the
> fiels are set to 0 (zero). And it reads the correct size of the struct:

>   n = read( sock, &preamble, sizeof(preamble) );
>   if( n < 0 || n > sizeof(preamble) )
>    error...

Debugging network code of this nature can be tedious.  I wont go into some
of the comments others have made about alignment, endianess, I assume you
know about all of those.  But if you are going to do this, I suggest two
changes to any protocol which exchanges data like this.  Place magic data and
and a transaction # at the front.  It makes debugging these a lot easier since
you will have a means to determine whether or not you are at the head, middle
or end of a stream, and you will be able to see if the data you are sending
is not what you think it is.

Inquire for more info.

-- Aaron

 
 
 

Sockets: write() writes OK, yet read() reads garbage...?

Post by Mike Shann » Fri, 07 Jun 1996 04:00:00



Quote:

>Strange thing happens. There is a TCP socket established between A and B. Some
>information flows OK between the two. However, one particular sequence of
>communication does not. A writes a command to B, and immediately goes into
>read() for a responce. B does something with the message and writes back to A a
>struct, with some fields set to important values. When A reads it (as the same
>struct - both use the same header file), however, it contains garbage. All the
>fiels are set to 0 (zero). And it reads the correct size of the struct:

>  n = read( sock, &preamble, sizeof(preamble) );
>  if( n < 0 || n > sizeof(preamble) )
>   error...

>What/where can the problem be? Does A (or maybe B) have to flush the socket
>after it wrote a command, to make sure it does not read its own packet back?

No, don't need to 'flush' a socket. Why don't you post the definition and
exact declaration of the 'preamble' structure? You might be sending its
address rather than contents to B. Have you tried writing the 18 bytes
received by B in a file?

Mike Shannon
Houston, Texas

<!>  Once we had computers and dumb terminals.
<!>  Now we have computers and stupid terminals.
<!>  Thanks, Microsoft.

 
 
 

1. write (socket, buf, 0) and read (socket, buf, 0)

Hi,

is there any pre-defined behaviour for these system functions
when i call write (socket, buf, 0) or read (socket, buf, 0) ?
Or should i better test it for every UNIX platform where i am
going to use my program?

And my 2nd question is: the LINUX man page for write(2) says,
that write returns 0 if nothing has been written. But what does
it really mean and what should i do then if i am trying to
transmit some buffer over socket ? Should i keep calling write,
still trying to write the whole buffer out (i am afraid to
stuck in a never-ending loop) or should i assume that the TCP-
connection has been closed at the other end (but what if not) ?

Thanks for your help!
Alex

--
russkaya literatura v ------ http://www.simplex.ru/lit.html
internete http://www.friends-partners.org/~afarber/lit.html
java preferans ------------ http://www.simplex.ru/pref.html
besplatnye kommercheskie ob'yavleniya http://www.simplex.ru

2. redirecting input to output

3. Question on writing c program implement cp function using UNIX system calls - read, write, etc.

4. Recommend graphics card: xpert@work/diamond fire gl pro?

5. Need help writing C program using UNIX system calls (read, write, etc) that copies files

6. How to find program location ?

7. Reading AND writing to HPFS - is it possible yet?

8. Caldera 2.2 and Memorex Cd-RW

9. help: read ok, write fails, on scsi-atapi cd-rw

10. problems with floppy drive (read OK, write NOT)

11. Re3: idetape broke in 2.4.x-2.4.9-ac5 (write OK but not read)

12. Read & Read/Write Groups

13. How to share read-only to one client and read-write to all others?