TCP send

J

Jack

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?
 
J

Jacek Dziedzic

Jack said:
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.
 
J

Jim Langston

Jack said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,276
Latest member
Sawatmakal

Latest Threads

Top