sending data as a structure type via socket in tcpip

hi All:

I have a data stream socket, the send and read functions only accept
arguments as char *,
what if I want to send data as a user defined structure? how can I do this?

“ran zhang” <rzhang@vamcointernational.com> wrote in message
news:a2mkt5$omp$1@inn.qnx.com

hi All:

I have a data stream socket, the send and read functions only
accept
arguments as char *,
what if I want to send data as a user defined structure? how can I do
this?

On my machine send and recv() both use void *.

Even if it would use char *, it wouldn’t make any difference. Send and read
do not care about the structure of format of what they send. They only
deal with char (byte) at the lowest level.

To send a structure in C (or int, or float) you just cast it.

struct foo {
bla…
} abc ;

send( s, (char *) &abc, sizeof ( abc ), 0 );


When you read (of course your protocol/program must expect this
perticular data format):

read (s, (char *)&abc), sizeof( abc), 0 );

Does that answer your question.

Mario Charest <goto@nothingness.com> wrote:

“ran zhang” <> rzhang@vamcointernational.com> > wrote in message
news:a2mkt5$omp$> 1@inn.qnx.com> …
hi All:

I have a data stream socket, the send and read functions only
accept
arguments as char *,
what if I want to send data as a user defined structure? how can I do
this?

On my machine send and recv() both use void *.

Even if it would use char *, it wouldn’t make any difference. Send and read
do not care about the structure of format of what they send. They only
deal with char (byte) at the lowest level.

To send a structure in C (or int, or float) you just cast it.

struct foo {
bla…
} abc ;

send( s, (char *) &abc, sizeof ( abc ), 0 );



When you read (of course your protocol/program must expect this
perticular data format):

read (s, (char *)&abc), sizeof( abc), 0 );

You also should be aware that a “data stream socket” do not gurentee
you that each “send()” will match each “recv()”.

-xtang

If you’re using a function that takes a void*, you should not cast the
address of your structure. The conversion is done implicitly for you. The
cast can hide errors that are hard to track down (since you told the
compiler not to complain) and doesn’t buy you anything.


Cheers,
Adam

QNX Software Systems Ltd.
[ amallory@qnx.com ]

With a PC, I always felt limited by the software available.
On Unix, I am limited only by my knowledge.
–Peter J. Schoenster <pschon@baste.magibox.net>
“Mario Charest” <goto@nothingness.com> wrote in message
news:a2mmrp$q80$1@inn.qnx.com

“ran zhang” <> rzhang@vamcointernational.com> > wrote in message
news:a2mkt5$omp$> 1@inn.qnx.com> …
hi All:

I have a data stream socket, the send and read functions only
accept
arguments as char *,
what if I want to send data as a user defined structure? how can I do
this?

On my machine send and recv() both use void *.

Even if it would use char *, it wouldn’t make any difference. Send and
read
do not care about the structure of format of what they send. They only
deal with char (byte) at the lowest level.

To send a structure in C (or int, or float) you just cast it.

struct foo {
bla…
} abc ;

send( s, (char *) &abc, sizeof ( abc ), 0 );


When you read (of course your protocol/program must expect this
perticular data format):

read (s, (char *)&abc), sizeof( abc), 0 );

Does that answer your question.

\