0

Here is the deal. I gotta program a little client/server chat app. The constraint is that i have to send the data from one to another under a packet form. The packet is composed as follow : size ( 1byte) idClient (5bytes) opCode(1Byte) and message(1-256bytes) For now i have managed to cut the message in chuncks of 256 bytes max. and send them but the server hangs when the message is longer than one packet.

So here is the server side :

boolean b = true
DataInputStream in = new DataInputStream(socket.getInputStream());
while (true) {                 // listen to messages from client
     while (b) {               //listen until all packets are received
         int msgLength = in.readInt(); // the length of the message sent
         ...
         ...
         in.readFully(packet);//size of byte[]packet depend on msgLength (is managed)
         ...
         ...
         ...


     }
}

and here is the client side : The method runs and send the data of each packets

DataOutputStream out = new DataOutputStream(connection.getOutputStream());
while(i < message.length){ //cuts the message into chunks of 256 bytes max 
                          //(the last packet of the message is under 256 all 
                          //the others are 256 bytes)
     ...
     ...
     out.writeInt(message.length);
     out.write(packet);
     out.flush();
}

When the server receive a message in only one packet everything is fine. But when the message is bigger, at the 2nd iteration of the 2nd while loop (always server side) the first in.readInt() works fine but the in.readFully() waits FOREVER for the data that was supposed to be already sent. The worst is that i made it work juste by replacing DataO/IputStream by ObjectO/IputStream for a time. Right now both are making the program wait forever if not crashing by saying :

 java.io.StreamCorruptedException: invalid type code: 00

or

 java.io.StreamCorruptedException: invalid type code: AC

or

 java.io.StreamCorruptedException: invalid header...

Please help i'm desperate ><

Tirvax
  • 35
  • 1
  • 10
  • Could you please make code examples more detailed? For instance, why two `while` loops in server? – borowis Mar 16 '17 at 01:44
  • 1
    On the client, you're writing the length of `message`, but you're writing the content of `package`. You should be consistent; either write `message` or write the length of `package`. – Erwin Bolwidt Mar 16 '17 at 01:45
  • @ErwinBolwidt I changed it thanks – Tirvax Mar 16 '17 at 01:50
  • @borowis the code is pretty long x) but the first loop is to receive the different messages (the server stays up until there is a massage sent) the second is to receive the different packet of a message – Tirvax Mar 16 '17 at 01:52
  • You changed it and what happened? – user207421 Mar 16 '17 at 01:56
  • @EJP I just changed the name here. The first int sent is the size of the message sent. Then there is the packet (readFully()). I need to know the size of the message to know the number of packet sent, to retrieve them in the server. – Tirvax Mar 16 '17 at 02:02
  • Sigh. If you changed the name you changed the value being transmitted, so you changed the semantics, so there was a different outcome. What was it? Or else you effectively changed nothing, in which case you need to review your change. Your present code sends the message size where it should send the chunk size. So fix that. – user207421 Mar 16 '17 at 02:06
  • @EJP thanks, made the trick ;) – Tirvax Mar 16 '17 at 02:40

0 Answers0