C Board  

Go Back   C Board > General Programming Boards > Networking/Device Communication

Reply
 
LinkBack Thread Tools Display Modes
Old 03-07-2005, 02:08 PM   #1
Registered User
 
Join Date: Feb 2005
Posts: 30
packing a message

hi

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?

Code:
     unsigned char buffer [100];
     int id, i=0, type;
     char data [1024];


      printf ("\nEnter an id number:");
      scanf("%d", &id);

      printf ("\nEnter a type between <3 :");
      scanf("%d", &type);

       printf ("\nEnter a message of 50 characters at max:");
       scanf("%s", data);

       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);

        i++;
       //pack the data
        memcpy(&buffer[i], &data[0], length);
       i = i+length;
thanks
majoub is offline   Reply With Quote
Old 03-08-2005, 01:15 PM   #2
and the hat of Jobseeking
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 21,689
id is a 4-byte number, and what it is doing is extracting each byte from the id.
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.

Salem is offline   Reply With Quote
Old 03-08-2005, 07:29 PM   #3
Registered User
 
Join Date: Feb 2005
Posts: 30
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...
majoub is offline   Reply With Quote
Old 03-09-2005, 12:39 PM   #4
and the hat of Jobseeking
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 21,689
> 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.
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.

Salem is offline   Reply With Quote
Old 03-09-2005, 01:48 PM   #5
Registered User
 
Join Date: Feb 2005
Posts: 30
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:

Code:
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

	int n;

	while(total < len) 
	{
		n = send(s, buf+total, bytesleft, 0);
		if (n == -1) 
		{ 
			break; 
		}

		total += n;
		bytesleft -= n;
	}//end while
	
	//return -1 on failure
	//return 1 in case of success

	if(n >= 0)
			return 1;
	else
		return -1;

}
anyway thanks salem
majoub is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Strange string behavior jcafaro10 C Programming 2 04-07-2009 07:38 PM
Global Variables Taka C Programming 34 11-02-2007 03:25 AM
Message class ** Need help befor 12am tonight** TransformedBG C++ Programming 1 11-29-2006 11:03 PM
Dialog Box Problems Morgul Windows Programming 21 05-31-2005 05:48 PM
Tab Controls - API -KEN- Windows Programming 7 06-02-2002 09:44 AM


All times are GMT -6. The time now is 12:42 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22