Sending data packets

This is a discussion on Sending data packets within the Networking/Device Communication forums, part of the General Programming Boards category; I have a class and functions for making a "packet" to send. Basically it's just code to be decoded on ...

  1. #1
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640

    Sending data packets

    I have a class and functions for making a "packet" to send. Basically it's just code to be decoded on the other side. I use HIBYTE and LOBYTE to change characters to numbers, but I don't think it works. Is there anything wrong with the code?

    Code:
    class CPaintTool
    {
    public:
    	BYTE ToolID;	//All use
    
    	CWCOORD pStart;	//All use
    	CWCOORD pEnd;		//Rectangle and Line only
    
    	WORD LineWidth;	//All use
    
    	WORD Radius;	//Circles only
    
    	BYTE cRed;		//All use
    	BYTE cGreen;	//All use
    	BYTE cBlue;		//All use
    
    	char szPacket[20];
    
    	void MakePacket();
    	void BreakPacket();
    };
    
    void CPaintTool::MakePacket()
    {
    	ZeroMemory(&szPacket, 20);
    	wsprintf(
    		szPacket,
    		"%c%c%c%c%c%c%c%c%c%c%c%c%c%c\r\n",
    		ToolID,
    		HIBYTE(pStart.x),
    		LOBYTE(pStart.x),
    		HIBYTE(pStart.y),
    		LOBYTE(pStart.y),
    		HIBYTE(pEnd.x),
    		LOBYTE(pEnd.x),
    		HIBYTE(pEnd.y),
    		LOBYTE(pEnd.y),
    		HIBYTE(LineWidth),
    		LOBYTE(LineWidth),
    		cRed,
    		cGreen,
    		cBlue
    		);
    }
    
    void CPaintTool::BreakPacket()
    {
    	ToolID = (BYTE)szPacket[0];
    	pStart.x = MAKEWORD((BYTE)szPacket[2], (BYTE)szPacket[1]);
    	pStart.y = MAKEWORD((BYTE)szPacket[4], (BYTE)szPacket[3]);
    	pEnd.x = MAKEWORD((BYTE)szPacket[6], (BYTE)szPacket[5]);
    	pEnd.y = MAKEWORD((BYTE)szPacket[8], (BYTE)szPacket[7]);
    	LineWidth = MAKEWORD((BYTE)szPacket[10], (BYTE)szPacket[9]);
    	cRed = (BYTE)szPacket[11];
    	cGreen = (BYTE)szPacket[12];
    	cBlue = (BYTE)szPacket[13];	
    }
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,506
    Using sprintf() to "format" binary data which may well contain \0 characters is a bad move.

    Just do
    szPacket[1] = HIBYTE(pStart.x);
    etc
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    When I breakpoint after making the packet, it says that it's a bad pointer, and each character in szPacket is bad. I don't get it

    Code:
    class CWCOORD
    {
    public:
    	WORD x;
    	WORD y;
    };
    
    class CPaintTool
    {
    public:
    	BYTE ToolID;	//All use
    
    	CWCOORD pStart;	//All use
    	CWCOORD pEnd;		//Rectangle and Line only
    
    	WORD LineWidth;	//All use
    
    	WORD Radius;	//Circles only
    
    	BYTE cRed;		//All use
    	BYTE cGreen;	//All use
    	BYTE cBlue;		//All use
    
    	char szPacket[20];
    
    	void MakePacket();
    	void BreakPacket();
    	CPaintTool();
    };
    
    CPaintTool::CPaintTool()
    {
    	ToolID = 0;
    	pStart.x = 0;
    	pStart.y = 0;
    	pEnd.x = 0;
    	pEnd.y = 0;
    	LineWidth = 0;
    	Radius = 0;
    	cRed = 0;
    	cGreen = 0;
    	cBlue = 0;
    	ZeroMemory(szPacket, 20);
    }
    
    void CPaintTool::MakePacket()
    {
    	ZeroMemory(szPacket, 20);
    	szPacket[0] = ToolID;
    	szPacket[1] = HIBYTE(pStart.x);
    	szPacket[2] = LOBYTE(pStart.x);
    	szPacket[3] = HIBYTE(pStart.y);
    	szPacket[4] = LOBYTE(pStart.y);
    	szPacket[5] = HIBYTE(pEnd.x);
    	szPacket[6] = LOBYTE(pEnd.x);
    	szPacket[7] = HIBYTE(pEnd.y);
    	szPacket[8] = LOBYTE(pEnd.y);
    	szPacket[9] = HIBYTE(LineWidth);
    	szPacket[10] = LOBYTE(LineWidth);
    	szPacket[11] = HIBYTE(Radius);
    	szPacket[12] = HIBYTE(Radius);
    	szPacket[13] = cRed;
    	szPacket[14] = cGreen;
    	szPacket[15] = cBlue;
    	szPacket[16] = '\r';
    	szPacket[17] = '\n';
    	szPacket[18] = '\0';
    }
    
    void CPaintTool::BreakPacket()
    {
    	ToolID = (BYTE)szPacket[0];
    	pStart.x = MAKEWORD((BYTE)szPacket[2], (BYTE)szPacket[1]);
    	pStart.y = MAKEWORD((BYTE)szPacket[4], (BYTE)szPacket[3]);
    	pEnd.x = MAKEWORD((BYTE)szPacket[6], (BYTE)szPacket[5]);
    	pEnd.y = MAKEWORD((BYTE)szPacket[8], (BYTE)szPacket[7]);
    	LineWidth = MAKEWORD((BYTE)szPacket[10], (BYTE)szPacket[9]);
    	Radius = MAKEWORD((BYTE)szPacket[12], (BYTE)szPacket[11]);
    	cRed = (BYTE)szPacket[13];
    	cGreen = (BYTE)szPacket[14];
    	cBlue = (BYTE)szPacket[15];	
    }
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  4. #4
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    I take it you noticed this in the original code.
    ZeroMemory(&szPacket, 20);

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,506
    > it says that it's a bad pointer, and each character in szPacket is bad.
    How can an array be a bad pointer,. unless you've failed to construct the class which surrounds it.

    And what do you mean each character is bad?
    For sure it isn't going to be a printable string, so there's no use trying to print it to the screen (the \r\n\0 at the end are redundant)

    In order to make sense of szPacket in the debugger, you need to display it in hex mode, not ascii mode.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    isn't that what I'm doing with the HIBYTE/LOBYTE crap? How would I save it in hex? All that happens is I MakePacket(), send it to the other computer, BreakPacket, and then DrawObj(). I checked on BreakPacket, and szPacket contains bad values.
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  7. #7
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    isn't that what I'm doing with the HIBYTE/LOBYTE crap?
    Frankly, no.

    Note what Salem REALLY said: "you need to display it in hex mode". As in, in the debugger you have it display the value in hex, not as a string.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. xor linked list
    By adramalech in forum C Programming
    Replies: 23
    Last Post: 10-14-2008, 10:13 AM
  2. Replies: 5
    Last Post: 08-16-2007, 11:43 PM
  3. Sending requests with packets
    By joeprogrammer in forum Networking/Device Communication
    Replies: 14
    Last Post: 05-21-2006, 07:33 PM
  4. Replies: 1
    Last Post: 02-06-2003, 02:33 PM
  5. Sending raw packets.
    By Denethor2000 in forum C++ Programming
    Replies: 3
    Last Post: 06-04-2002, 02:08 PM

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