2

I am trying to send two byte arrays over a Java socket. I have captured the data via wireshark and it shows that the first byte array sends; the second, however, doesn't send at all. I am using Linux Mint and Oracle's JRE (not OpenJDK).

byte[] packet_one = new byte[] {(byte)0x00,(byte)0x00,(byte)0x00,(byte)0x50};
byte[] packet_two = new byte[] {(byte)0x00,(byte)0x00,(byte)0x00,(byte)0x78};
Socket sck;
DataInputStream dis;
DataOutputStream dos;

try {
sck = new Socket(IP_ADDRESS,PORT);    
dis = new DataInputStream(sck.getInputStream());     
dos = new DataOutputStream(sck.getOutputStream());

int recv_header = dis.ReadInt(); // This receives the correct response.

dos.write(packet_one); // Sends fine.

dos.write(packet_two); //Does not send.

int data_length = dis.readInt(); // No more data received.

}catch(Exception e) {
 System.out.println("Error: " + e.getMessage());    
}

So, dos.write(packet_one) works (confirmed by wireshark). Writing packet_two doesn't work. The data_length returns 0 since I don't receive anymore data either. No errors or exceptions get caught.

I have also tried using dos.flush(), but it doesn't make a difference.

Any ideas on why this may be the case?

prem kumar
  • 5,641
  • 3
  • 24
  • 36
sofiax
  • 559
  • 4
  • 3

1 Answers1

0

Maybe you're exiting the program before there's time to send the buffered bytes?

Perhaps if you add something like:

for (;;) {
    final byte x = dis.readByte();
    System.out.println("got byte: " + (x & 0xFF));
}

the program won't exit, and you'll also see if there are more bytes that the server sent back. Might offer clues.

And, in case you don't know, readInt() reads for bytes and returns an int made from those bytes, which is 0 if all the for bytes read are zero. So your comment 'No more data received.' feels a bit ambiguous.

You could also try inserting a

sck.setTcpNoDelay(true);

after creating the socket. Writes should then go on the network immediately. I think.

Hope this helps.

Jonas N
  • 1,757
  • 2
  • 21
  • 41