By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,122 Members | 1,237 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,122 IT Pros & Developers. It's quick & easy.

TCP send

P: n/a
Hi all,
I am writing an application that sends and receives large amount of
data to and from a server. I want to encode each packet similar to a
struct such that, each message has a 4 bit code (determining how it
should be parsed on the other end) as well as other info such as name,
uid, x and y positions. Here is a example of the structure:

typedef struct {
unsigned int msg:4;
unsigned int uidEmp;
char nameEmp[20];
char dataEmp[50];
}myPacket;

As you can see, my packet has various types of data types. I can
convert them all to char and then send it in a buffer form like

send(sd2,buf,strlen(buf),0);

But I think that would take way too much storage room. For instance my
msg variable of 4 bits will become a byte and same with other fields.
The reason why I am worried about the size of the packet is that,
there will be a blast of 100 packets of these types of data at once
and this may result in a major bottleneck. Is there any way to
minimize the size?

Mar 25 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Jack wrote:
Hi all,
I am writing an application that sends and receives large amount of
data to and from a server. I want to encode each packet similar to a
struct such that, each message has a 4 bit code (determining how it
should be parsed on the other end) as well as other info such as name,
uid, x and y positions. Here is a example of the structure:

typedef struct {
unsigned int msg:4;
unsigned int uidEmp;
char nameEmp[20];
char dataEmp[50];
}myPacket;

As you can see, my packet has various types of data types. I can
convert them all to char and then send it in a buffer form like

send(sd2,buf,strlen(buf),0);
I think that won't work -- strlen will stop at the first
\0, which may appear inside the int variables, and if not,
then definitely at the end of nameEmp[]. You'd better
use sizeof for that one.
But I think that would take way too much storage room. For instance my
msg variable of 4 bits will become a byte and same with other fields.
The reason why I am worried about the size of the packet is that,
there will be a blast of 100 packets of these types of data at once
and this may result in a major bottleneck. Is there any way to
minimize the size?
Sounds like premature optimization. You'll lose 4 bits in
seventy-something bytes, that's less than 1%. Why bother?

HTH,
- J.
Mar 25 '07 #2

P: n/a
"Jack" <ac*******@hotmail.comwrote in message
news:11**********************@l75g2000hse.googlegr oups.com...
Hi all,
I am writing an application that sends and receives large amount of
data to and from a server. I want to encode each packet similar to a
struct such that, each message has a 4 bit code (determining how it
should be parsed on the other end) as well as other info such as name,
uid, x and y positions. Here is a example of the structure:

typedef struct {
unsigned int msg:4;
unsigned int uidEmp;
char nameEmp[20];
char dataEmp[50];
}myPacket;

As you can see, my packet has various types of data types. I can
convert them all to char and then send it in a buffer form like

send(sd2,buf,strlen(buf),0);

But I think that would take way too much storage room. For instance my
msg variable of 4 bits will become a byte and same with other fields.
The reason why I am worried about the size of the packet is that,
there will be a blast of 100 packets of these types of data at once
and this may result in a major bottleneck. Is there any way to
minimize the size?
If it is a pod structure you need to send, then go ahead and send the pod
structure. Your data is fairly compact, on a 32 bit system I don't believe
there are any padding bytes except at the end, where there would be 2 bytes
(your milage may vary depending on os, system, etc...).

Now, as for sending 4 bits instead of 8, there is no way to send half a
byte, you send a byte or not. If you had 2 4 bit fields you could store
them in the same byte but why worry about an extra byte? sizeof myPacket on
my system would be 80 bytes (78 bytes of data, 2 padding bytes). So even if
you only had 77 bytes of data, it would still be 80.

100 x 80 = 8000. That's going to be 6 packets with an MTU of 1500 (largest
allowed by ethernet and a common value). I don't see you saving anything.
I would just go ahead and make sure your structures are defined to not have
any needless padding and use sizeof.
Mar 25 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.