467,161 Members | 873 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,161 developers. It's quick & easy.

sending a struct over TCP

Hi, All,
is it possible to send a struct using the the send function. Here is
what I mean

typedef struct{
int ID;
char name[20];
}sampleStruct;

int main(){
sampleStruct a;
//lets assume we have filled out our sin_family and etc. so
we'll have something like:
sd = socket (AF_INET, SOCK_STREAM, 0);

connect (sd, (struct sockaddr*) &socketChannel, sizeof(struct
sockaddr);
a.ID = 1;
a.name="Bob";
send(sd, (void*) a, sizeof(a), 0);
...
...
...
}

Is there anyway to make this work or do I need to make a char buffer
and write

buffer[100] = "1 Bob";
send(sd, (void*) buffer, strlen(buffer), 0);

Thanks

Mar 27 '07 #1
  • viewed: 24705
Share:
6 Replies
On Mar 27, 9:03 am, "Jack" <accpac...@hotmail.comwrote:
Hi, All,
is it possible to send a struct using the the send function. Here is
what I mean

typedef struct{
int ID;
char name[20];

}sampleStruct;

int main(){
sampleStruct a;
//lets assume we have filled out our sin_family and etc. so
we'll have something like:
sd = socket (AF_INET, SOCK_STREAM, 0);

connect (sd, (struct sockaddr*) &socketChannel, sizeof(struct
sockaddr);

a.ID = 1;
a.name="Bob";
send(sd, (void*) a, sizeof(a), 0);
..
..
..

}

Is there anyway to make this work or do I need to make a char buffer
and write

buffer[100] = "1 Bob";
send(sd, (void*) buffer, strlen(buffer), 0);

Thanks
Dear Jack,

You can send it using the same send function without having another
char buffer.
since a is an object of your struct you need to give the address of
it.
i.e
send(sd, (void*)&a, sizeof(a), 0); will resolve your issue.

One thing you've to notice while dealing with structure variables.

the size of the structure will be aligned according the pack size you
are specifying. Normally the default alignment for the structure
members will be 4 bytes in size.

i.e if you declare

struct MyStruct
{
int a;
char buff[21]; //Oops I meant 20 letters for name and extra one
byte for null character
};

You might be expecting a size 25 bytes for this structure but if you
are giving alignment bytes 4, the size of the struct will be 28 bytes.
(24 for buff and 4 for variable a)

As per your code you will be sending some extra bytes because of the
alignment and you've to make sure that, at the client side too, you
are having same structure alignment. else you may face some unexpected
behavior.

You can specify the alignment using "#pragma pack" directive

#pragma pack(1) // alignment will be 1 byte you will get the exact
size of members
#pragma pack(2) // alignment will be 2 byte members will be aligned to
2 bytes
#pragma pack(4) // alignment will be 4 byte members will be aligned to
4 bytes
#pragma pack(8) // alignment will be 8 byte members will be aligned to
8 bytes

Normally this will be set to the wordlength of the processsor. i.e 4
bytes if you are using 32 bit architecture or 8 bytes if you are using
64 bit architecture.
and for strict size information we will use 1 byte alignment.

Mar 27 '07 #2
Jack wrote:
Hi, All,
is it possible to send a struct using the the send function. Here is
what I mean

typedef struct{
int ID;
char name[20];
}sampleStruct;

int main(){
sampleStruct a;
//lets assume we have filled out our sin_family and etc. so
we'll have something like:
sd = socket (AF_INET, SOCK_STREAM, 0);

connect (sd, (struct sockaddr*) &socketChannel, sizeof(struct
sockaddr);
a.ID = 1;
a.name="Bob";
send(sd, (void*) a, sizeof(a), 0);
..
..
..
}

Is there anyway to make this work or do I need to make a char buffer
and write

buffer[100] = "1 Bob";
send(sd, (void*) buffer, strlen(buffer), 0);

Thanks
You can send any bytes you want. It's up to your send and recv code to
deal with what the bytes mean. Note that you need the '&'

send(sd, (void*)&a, sizeof(a), 0);

--
Scott McPhillips [VC++ MVP]

Mar 27 '07 #3
On Mar 27, 8:03 am, "Jack" <accpac...@hotmail.comwrote:
Hi, All,
is it possible to send a struct using the the send function. Here is
what I mean

typedef struct{
int ID;
char name[20];

}sampleStruct;

int main(){
sampleStruct a;
//lets assume we have filled out our sin_family and etc. so
we'll have something like:
sd = socket (AF_INET, SOCK_STREAM, 0);

connect (sd, (struct sockaddr*) &socketChannel, sizeof(struct
sockaddr);

a.ID = 1;
a.name="Bob";
send(sd, (void*) a, sizeof(a), 0);
..
..
..

}

Is there anyway to make this work or do I need to make a char buffer
and write

buffer[100] = "1 Bob";
send(sd, (void*) buffer, strlen(buffer), 0);

Thanks
of course,structure object just a seriate memory buffer, the same as
array of char or other, so you can using send() function to send the
structure(memory)

Mar 27 '07 #4
Hello, Jack.

Don't forget only that different architectures has different
endienless (little and big)
So, when on the other side you receive a packet ()

recv(sd, (void*) &a, sizeof(a), 0);

the a.id maybe not 1(0x00000001), but (0x01000000)

Good Day
--
Alexander Pazdnikov

Mar 27 '07 #5
And if your struct has pointers to other types of data you must
consider another way of sending your struct.

For ex.

struct tag_MyStruct {
int a, b;
fooStruct *pFoo;
};

You will need to traverse those pointers and write the data to a
single stream of bytes to send() the struct.

Mar 27 '07 #6
On Mar 27, 12:54 pm, "Scott McPhillips [MVP]" <org-dot-mvps-at-
scottmcpwrote:
send(sd, (void*)&a, sizeof(a), 0);
You never need to cast to void* (unless it is corresponding
to the ... part of a variadic function).

It's good programming practice to avoid using casts unless
absolutely necessary, and in this case the cast is worse than
useless.

Also, 'send', 'connect', and 'socket' are not standard C++
functions; OP would be better off posting to comp.unix.programmer.
Mar 27 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Leo | last post: by
2 posts views Thread by Fernando Barsoba | last post: by
9 posts views Thread by thorley@gmail.com | last post: by
1 post views Thread by simu@snowplow.ch | last post: by
2 posts views Thread by Sean | last post: by
8 posts views Thread by Thomas Dybdahl Ahle | last post: by
2 posts views Thread by Jack | last post: by
4 posts views Thread by =?Utf-8?B?RWl0YW4=?= | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.