I'm making a development for connecting one client to a specific tipe of server over TCP/IP. I am creating a code that to send bytes over TCP/IP and receive bytes.
The problem is when I receive the packets in certain point it breaks.
String input;
Traductor traductor = new Traductor();
PrintWriter writer = new PrintWriter(this.socket.getOutputStream(), true);
// Write a message to server
byte[] theBytesInmediate = new byte[]{0x11, 0x00, 0x14, 0x50, 0x00, 0x01, 0x04, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0x7F, (byte) 0x0A, 0x02, (byte)0x2A, 0x00, 0x02, 0x02, 0x31, 0x00};
DataOutputStream salida = new DataOutputStream(this.socket.getOutputStream());
salida.write(theBytesInmediate);
System.out.println("Packet sent");
InputStream input2 = socket.getInputStream();
String tmp ="";
while(true){
DataInputStream entrada = new
DataInputStream(this.socket.getInputStream());
System.out.println("bytes: " + entrada.readByte());
try{
tmp =tmp + entrada.readUTF();
System.out.println("readUTF: " + tmp);
}catch(IOException e){
System.out.println(e);
}
System.out.println("read: " + entrada.read());
System.out.println("readChar: " + entrada.readChar());
System.out.println("readLong: " + entrada.readLong());
}
Here is my output in console:
One problem is when I try to get all the packet if you see on the image it breaks, and only get part.
The second problem if I delete this lines:
System.out.println("read: " + entrada.read());
System.out.println("readChar: " + entrada.readChar());
System.out.println("readLong: " + entrada.readLong());
The getting packet stops in first P blank blank blank, part of packet.
I test with BufferedReader reader = new BufferedReader(new InputStreamReader(input2)); but the reading packet is incorrect.
And I know that the packets are incorrect because I am checking it with Wireshark.
The important part of packet I need is that tells 13001 there is for mi id.
Thanks a lot to all.
Update:
With my new code thanks to EJP, is working now, but one line is missin at last (I think).
PrintWriter writer = new PrintWriter(this.socket.getOutputStream(), true);
byte[] theBytesInmediate = new byte[]{0x11, 0x00, 0x14, 0x50, 0x00, 0x01, 0x04, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0x7F, (byte) 0x0A, 0x02, (byte)0x2A, 0x00, 0x02, 0x02, 0x31, 0x00};
DataOutputStream salida = new DataOutputStream(this.socket.getOutputStream());
String str = new String(theBytesInmediate, StandardCharsets.UTF_8);
writer.println(str);
//writer.flush();
System.out.println("Packet sent");
InputStream input2 = this.socket.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(input2));
String tmp ="";
while(true)
{
System.out.println("read: " + reader.readLine().toString());
System.out.println("read: " +reader.read());
}
If you see in the output is missin the large paquet where the ip from the last id is. Is not a large problem. But if I need to end the while, is needed.
Thanks a lot to all.
You are reading with readUTF()
but you are not writing with writeUTF()
. Have a look at the Javadoc. There is no chance that readUTF()
can read data written with write()
.
You also need to decide as between Reader
and Writer
on the one hand and input/output streams on the other hand. Don't mix them.