-
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
-
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
-
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?