structure containing pointer to char over the network 
Author Message
 structure containing pointer to char over the network

Gurus,

I would appreciate if someone points me in the right direction in the folowing:

On the client I have a struct{
                int a,
                char b[34],
                int c,
                char * d
                } mystruct,

I am trying to do
               write ( socket, &mystruct, sizeof(mystruct) )
When I read it back on  the server piece-by piece,
I have problems getting the value from char * field.

It seems, the sizeof reports size of the pointer instead of the contents.
What is the correct way to transmit such structure over the network
and read fields one by one on the server.
P.S. I cannot read the whole stucture at one time, therefore, I am bound to read
each field at a time.

Thanks, everyone.



Thu, 17 Mar 2005 10:35:48 GMT  
 structure containing pointer to char over the network
Quote:

> Gurus,

> I would appreciate if someone points me in the right direction in the folowing:

> On the client I have a struct{
>                 int a,
>                 char b[34],
>                 int c,
>                 char * d
>                 } mystruct,

> I am trying to do
>                write ( socket, &mystruct, sizeof(mystruct) )
> When I read it back on  the server piece-by piece,
> I have problems getting the value from char * field.

> It seems, the sizeof reports size of the pointer instead of the contents.
> What is the correct way to transmit such structure over the network
> and read fields one by one on the server.
> P.S. I cannot read the whole stucture at one time, therefore, I am bound to read
> each field at a time.

> Thanks, everyone.

This question is off-topic here, you will need to consult a news group
for your particular platform (perhaps comp.unix.programmer)

<OT>
The reason you can't read from the char * on the other side is because
the memory it points to is not in the struct.  The write call
writes a contiguous block which means it will write the address it
points to not the data it points to.
</OT>

--
Sam Reynolds



Thu, 17 Mar 2005 10:50:59 GMT  
 structure containing pointer to char over the network

Quote:

> Gurus,

> I would appreciate if someone points me in the right direction in the folowing:

> On the client I have a struct{
>                int a,
>                char b[34],
>                int c,
>                char * d
>                } mystruct,

> I am trying to do
>               write ( socket, &mystruct, sizeof(mystruct) )
> When I read it back on  the server piece-by piece,
> I have problems getting the value from char * field.

> It seems, the sizeof reports size of the pointer instead of the contents.
> What is the correct way to transmit such structure over the network
> and read fields one by one on the server.
> P.S. I cannot read the whole stucture at one time, therefore, I am bound to read
> each field at a time.

While write() is offtopic here, if we forget about sockets and replace
the write with a reference to fwrite(&mystruct, sizeof(mystruct), 1,
stream), then it turns out your question is a C question after all.

The answer is, you need to work out a way to marshall and unmarshall
your structs yourself, that take into account the meaning of the struct
members.  Instead of writing the value of mystruct.d, you'll need to
write what it points to.  You haven't given enough information to work
out the best way to do that though - is d a pointer to a null-terminated
string? an array of mystruct.c chars? an array terminated by a -1
sentinel? something else?

        - Kevin.



Thu, 17 Mar 2005 11:13:35 GMT  
 structure containing pointer to char over the network

Quote:
>I would appreciate if someone points me in the right direction in the folowing:

>On the client I have a struct{
>                int a,
>                char b[34],
>                int c,
>                char * d
>                } mystruct,

>I am trying to do
>               write ( socket, &mystruct, sizeof(mystruct) )
>When I read it back on  the server piece-by piece,
>I have problems getting the value from char * field.

You sent *A POINTER*.  You didn't send the string (or whatever) it
points at.  Don't be at all surprised when you can't access something
you didn't send.  Generally, a client and a server have *NO* shared
memory between them (especially if they are on different machines).

Don't send pointers to another process via a network or files:  the
pointer is the equivalent of "upper left dresser drawer in front
bedroom on 1 Main Street", and it won't mean the same thing in
Detroit as it does in San Francisco.  The pointer cannot be
meaningfully dereferenced in another process, and if you're lucky,
it will cause an instant smegmentation violation, rather than just
retrieving garbage.

Quote:
>It seems, the sizeof reports size of the pointer instead of the contents.

What's in the struct *IS* the pointer, not the contents.

Quote:
>What is the correct way to transmit such structure over the network
>and read fields one by one on the server.

Generally, send the fields one at a time, translating pointers to
what you really want to send.  It can get really involved defining
a file format to send what started off as one pointer to a huge
linked list, which may require sending the whole list.

                                                Gordon L. Burditt



Thu, 17 Mar 2005 12:49:36 GMT  
 structure containing pointer to char over the network
Quote:

> Don't send pointers to another process via a network or files:
> the pointer is the equivalent of "upper left dresser drawer in
> front bedroom on 1 Main Street", and it won't mean the same
> thing in Detroit as it does in San Francisco.  The pointer
> cannot be meaningfully dereferenced in another process, and if
> you're lucky, it will cause an instant smegmentation violation,

                                         ^^^^^^^^^^^^^

Quote:
> rather than just retrieving garbage.

Is that a Red Dwarf reference, or just a typo?

--
Simon.



Fri, 18 Mar 2005 00:09:13 GMT  
 structure containing pointer to char over the network

Quote:


> > Don't send pointers to another process via a network or files:
> > the pointer is the equivalent of "upper left dresser drawer in
> > front bedroom on 1 Main Street", and it won't mean the same
> > thing in Detroit as it does in San Francisco.  The pointer
> > cannot be meaningfully dereferenced in another process, and if
> > you're lucky, it will cause an instant smegmentation violation,
>                                          ^^^^^^^^^^^^^
> > rather than just retrieving garbage.

> Is that a Red Dwarf reference, or just a typo?

Well... if the gestalt entity known as Grant Naylor ever coined the term, it
was strictly off the record! :P

--
Peter



Fri, 18 Mar 2005 19:39:44 GMT  
 structure containing pointer to char over the network

Quote:

>> Don't send pointers to another process via a network or files:
>> the pointer is the equivalent of "upper left dresser drawer in
>> front bedroom on 1 Main Street", and it won't mean the same
>> thing in Detroit as it does in San Francisco.  The pointer
>> cannot be meaningfully dereferenced in another process, and if
>> you're lucky, it will cause an instant smegmentation violation,
>                                         ^^^^^^^^^^^^^
>> rather than just retrieving garbage.

>Is that a Red Dwarf reference, or just a typo?

No, it's a pointer to Red Dwarf.  C doesn't have references.

                                        Gordon L. Burditt



Sun, 20 Mar 2005 13:33:20 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. Pointers to Structure that contains pointer to Function

2. dereferencing pointer to a structure containing a pointer to int

3. Size of structure containing char fields

4. reading a structure containing pointers

5. How Do I fwrite() and fread() A Structure that contains pointers to dynamic data

6. self contained pointers to structures

7. Structure containing pointer problem

8. Marshaling a structure containing a aray of different structures

9. memory block containing pointers, aka pointer to pointer i believe

10. Comparison with pointer to pointer to char, and char

11. Structure pointers, char, and passing

12. How to include char arrays of pointers in structures

 

 
Powered by phpBB® Forum Software