Thread: How do I prefix the length of message in TCP/IP

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    164

    How do I prefix the length of message in TCP/IP

    I'm sending messages over TCP/IP, I need to prefix message length in a char array and then send it. How do I do it?

    Also can you please provide an example of how to extract it at the another end. And if possible, please explain.

    I'm using C++ and Winsock.

  2. #2
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    What have you done?

  3. #3
    pwning noobs Zlatko's Avatar
    Join Date
    Jun 2009
    Location
    The Great White North
    Posts
    132
    Hello.

    This should get you started.
    Code:
    /* Assuming that the length of your messages fits into a short */
    
    void writer(void)
    {
    	/* Determine message and length (len) of message */
    	/*
    	...
    	*/
    
    	/* Encode len into network byte order */
    	short netLen = htons(len);
    	/* put indvidual bytes of len into the write buffer */
    	*pBuffer++ = char(netLen >> 8); // most significant byte will be written first 
    	*pBuffer++ = char(netLen & 0xFF); // least significant second
    
    	/* fill up rest of buffer */
    	/* 
    	... 
    	*/
    	
    	/* write to socket len + 2 */
    }
    
    void reader(void)
    {
    	char buffer[128];
    
    	/* read from socket 2 bytes into buffer and do the reverse of what the writer did */
    	short netLen = short(buffer[0]) << 8; // extract the most significant byte
    	netLen |= buffer[1]; // add in the least significant byte
    	short len = ntohs(netLen); // translate from network byte order to local machine order
    
    	/* ret from socket len bytes */
    
    }

  4. #4
    Registered User
    Join Date
    Nov 2007
    Posts
    164
    Can you please explain why you are using the bit shift operator and the ANDing it with 0xFF?

    How does it works?

  5. #5
    pwning noobs Zlatko's Avatar
    Join Date
    Jun 2009
    Location
    The Great White North
    Posts
    132
    The ANDing with 0xFF is not actually necessary, it's just my habit. I'll show you another way to do it too, and you can choose based on your style.

    The netLen is a short, so it takes up 2 bytes, but your transmission buffer is accessed by byte because it is usually a char* or unsigned char*. My preference is to take apart the netLen and stuff the parts into the transmission buffer byte-by-byte.

    If netLen is (in hex) 0x1234, you need to take the byte 0x12 and put it into the buffer. You can get the byte by shifting netLen right 8 bits and putting it into the buffer. To get the byte 0x34, you could just cast it as a char and place the result into the buffer. The cast will chop off the high byte of 0x12. In my sample code, I zero out the high byte before the cast. I guess that's a waste of time.


    The other way to do it is cast the char* buffer as a short* for the netLen assignment. For example:
    Code:
    char* buffer = some_memory;
    *((short*)buffer) = netLen;
    buffer += 2;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem in message handling VC++
    By 02mca31 in forum Windows Programming
    Replies: 5
    Last Post: 01-16-2009, 09:22 PM
  2. HELP!!!!emergency ~expert please help
    By unknowppl in forum C Programming
    Replies: 1
    Last Post: 08-19-2008, 07:35 AM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  4. trouble joining strings
    By readi83 in forum C Programming
    Replies: 3
    Last Post: 03-03-2005, 03:10 PM
  5. input/output
    By dogbert234 in forum Windows Programming
    Replies: 11
    Last Post: 01-26-2005, 06:57 AM