packing a message
I've been reading some tutorials on packing a message before sending it to the server..and I haven't been doing very well..
I have here an example of a message being packed can someone explain the few lines that use hexadecimals?
unsigned char buffer ;
int id, i=0, type;
char data ;
printf ("\nEnter an id number:");
printf ("\nEnter a type between <3 :");
printf ("\nEnter a message of 50 characters at max:");
int length = strlen(message);
//pack the id number
buffer[i++] = (unsigned char)((id & 0xff000000) >> 24);
buffer[i++] = (unsigned char)((id & 0xff0000) >> 16);
buffer[i++] = (unsigned char)((id & 0xff00) >> 8);
buffer[i++] = (unsigned char)(id & 0xff);
//pack the type
buffer[i] = (unsigned char)(type & 0x03);
buffer[i] = buffer[i]<<6;
//pack the length
buffer[i] = buffer[i] | (length & 0x0000003F);
//pack the data
memcpy(&buffer[i], &data, length);
i = i+length;
id is a 4-byte number, and what it is doing is extracting each byte from the id.
I solved my pcking problems but it seems that when I'm trying to send a large number of bytes say 50KB I'm loosing part of the data...
> I'm trying to send a large number of bytes say 50KB I'm loosing part of the data...
Since your output buffer is only 100 bytes, I'm not surprised something else isn't happening as well.
no the size of the buffer is not the issue here...(my buffer is of size 65000)...
when the message is long you might loose some bytes while sending it...actualy you don't loose them, they are still in the buffer but were not read for some reason...
anyway I found a solution for this problem...
here is the function if that might interest you:
anyway thanks salem
int sendall(int s, char *buf, int len)
//when we send a message to the server (or client) the bytes in the message might not be all sent
//this methos makes sure all bytes were sent
int total = 0; // total number of bytes we're sending
int bytesleft = len; // how many we have left to send
while(total < len)
n = send(s, buf+total, bytesleft, 0);
if (n == -1)
total += n;
bytesleft -= n;
//return -1 on failure
//return 1 in case of success
if(n >= 0)