Thread: Packets kind of

  1. #1
    Registered User
    Join Date
    Jul 2003
    Posts
    110

    Packets kind of

    Hi im making a packet and my main problem is trailing junk in my character arrays.

    Code:
    struct stLogin
    {
     char i;
     char Username[30];
     char Password[30];
     char Unkown1;
    };
    
    
    void vClientConnection( char *szServerIP, int iServerListenPort )
    {
    	SocketObject	ClientSocketObject;
    	int i = 5;
    	stLogin LoginPacket;
    	char Overlow[62];
    	char Uname[30] = "Name";
    	char Pword[30] = "Password";
    	LoginPacket.i = 128;
    	strcpy(LoginPacket.Username,Uname);
    
    	strcpy(LoginPacket.Password,Pword);
    
    
    	cout << "<Client> Connecting to " << szServerIP << ", Port " << iServerListenPort << endl;
    	
    	// Connect to the IP and Port 
    	if( ClientSocketObject.Connect( szServerIP, iServerListenPort ) )
    	{
    		cout << "<Client> Connected" << endl;
    		
    		memcpy(Overlow,&LoginPacket,sizeof(stLogin));
    		for(i=0;i<63;i++)
    		{
    			cout << hex<<Overlow[i] << " ";
    		}
            cout << " " << endl;
    		ClientSocketObject.Send(Overlow,sizeof(stLogin),0);
    		
    		cout << "<Client> Disconnected From Server" << endl;
    	}
    	else {
    		cout << "<Client> Failed to Connect" << endl;
    	}
    }

    When I print out the Overlow it comes out like this.

    Code:
    <Client> Connecting to 192.168.1.103, Port 2593
    <Client> Connected
    Ç N a m e   ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ P a s s w o r d
    ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠ ╠
    <Client> Disconnected From Server

    So how do I eliminate the trailing junk so its null? (btw the ╠ ╠ ╠ ╠ ╠ ╠ is the junk).


    Thankyou

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well there's a whole bunch of problems.

    1. You dont initialise LoginPacket
    2. strcpy(LoginPacket.Username,Uname); only partially initialises the string (as far as the first \0 basically)

    So first step is begin with
    Code:
    stLogin LoginPacket = { 0 };
    > memcpy(Overlow,&LoginPacket,sizeof(stLogin));
    This assumes that the structure is packed (sizeof struct is the same as the sum of the sizes of its members). This cannot be guaranteed.
    You're in all sorts of doo-doo if you try and send a structure over the network.
    In particular, you're breaking Rule 8

    > for(i=0;i<63;i++)
    This steps off the end of the array.

    Read this as well
    http://cboard.cprogramming.com/showthread.php?t=41926
    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.

  3. #3
    Registered User
    Join Date
    Jul 2003
    Posts
    110
    AH yes my array was over. Also this isnt anything secure. Just something fast. But Id rather learn it the right way. Would it be better to send a class over a network?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Accessing and editing packets of other applications
    By Inder in forum Linux Programming
    Replies: 1
    Last Post: 09-01-2006, 12:00 PM
  2. Recieve packets
    By valt in forum C++ Programming
    Replies: 9
    Last Post: 02-04-2006, 12:41 AM
  3. Adding delay to all packets (incoming and leaving)
    By ingtabby in forum Windows Programming
    Replies: 1
    Last Post: 01-31-2006, 10:33 AM
  4. establish a connection using TCP and capture the incoming packets
    By shraddha in forum Networking/Device Communication
    Replies: 12
    Last Post: 10-22-2005, 02:15 AM
  5. What kind of job...
    By Discolemonade in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 08-15-2005, 08:23 PM