Premt Premt - 4 years ago 189
Java Question

Java tcp socket does not receive properly

Hi let me get straight to the problem. I have a big JSON packet that the server sends to this client once the client is authenticated

But the packet comes back in a weird way like it's split or something example:

The JSON should be:

Received: {"UserID":1,"PlayerID":2,"EXP":0,"Lvl":1,"Coins":0,"ItemSlots":30}


When it comes through:

Received: {"UserID":1,"PlayerID":2,"EXP":0,"Lvl":1,"Coins":0,
Received: "ItemSlots":30}


Why does it split the packet or something when it comes to the client and how can I fix this anyway?

Java Receive Code:

private class ClientThread extends Thread {
public void run() {
try {
while (selector.select() > 0) {
for (SelectionKey sk : selector.selectedKeys()) {

selector.selectedKeys().remove(sk);
if (sk.isReadable()) {

SocketChannel sc = (SocketChannel)sk.channel();
ByteBuffer buff = ByteBuffer.allocate(1024);
String content = "";

while (sc.read(buff) > 0) {
sc.read(buff);
buff.flip();
content += charset.decode(buff);
buff.clear();
}

System.out.println("Recieved: " + content);
sk.interestOps(SelectionKey.OP_READ);
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}


Thanks have a wonderful day.

Answer Source

Hi lemme get straight to the problem so i got a big JSON packet that the server sends to this client once the client is authenticated

You mean you have a big JSON message. Packets are things that network protocols used to exchange information.

But the packet comes back in a weird way like its split or something example:

Unless you're looking at the wire, you aren't looking at packets. You're looking at the bytes you got from your end of the TCP connection.

The JSON should be:

Recieved: {"UserID":1,"PlayerID":2,"EXP":0,"Lvl":1,"Coins":0,"ItemSlots":30}

When it comes through:

Recieved: {"UserID":1,"PlayerID":2,"EXP":0,"Lvl":1,"Coins":0, Recieved: "ItemSlots":30}

Excellent. You got the same bytes. Now make a JSON parser that figures out where the message ends and parses it.

Why does it split the packet or something when it comes to the client

It splits the message into packets because that's how TCP gets the message to the other side. TCP is not a message protocol and it doesn't know or care what the application considers to be a message -- that's the application's job.

and how i can i fix this anyway?

Write a JSON parser to figure out where the messages end. You haven't implemented any code to receive JSON over TCP yet, so that won't work until you do.

TL;DR: If you want an application-level message protocol, you need to implement one. TCP is not one.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download