1

So I wanna communicate between a C++ client and a Java server.
So far so good (yeah I know about the Endiness prob) but I would like to read a defined structure on my serverside which was defined on clientside.

Let's say my client sends a packet in this structure:

char *pLoginData = new char[512];
// ...
char *packet = pLoginData;
*(WORD*)pLoginData = (usernameLength + 1 + passwordLength + 1);

pLoginData += 2;

strcpy((char*)(pLoginData), username.c_str());
pLoginData += usernameLength + 1;

strcpy((char*)(pLoginData), password.c_str());
pLoginData += passwordLength + 1;

// now send it
m_iResult = send(connectSocket, packet, *(WORD*)(packet) + 2, 0);

On my serverside how far I got is:

// init socket, init DataInputStream..
// ...
messageByte[0] = in.readByte(); // defined as: byte[] messageByte = new byte[1000];
System.out.println("Packet len: " + messageByte[0]);

bytesRead = in.read(messageByte);
msg += new String(messageByte, 0, bytesRead); // defined as: String msg = null;

System.out.println(msg);

Output is unfortunately:

Packet len: 16
null testuser testpw 

(there is a space after testpw, why? because look on how the client constructs the packet)
As you can see, my output contains the word "null" (dunno why?), but you see aswell that the word "null" isn't counted, cause packet len returns 16 which is: username (space) pw (space) (testuser testpw )

Hope you guys could help me out here :)
Thanks in advance!

0

1 Answer 1

2

As you said in a comment in the code, you're defining msg as a null string and then appending to it.

You should declare msg as an empty string.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.