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?

5
  • Can you post the receiver/client code? Commented Jan 21, 2016 at 12:21
  • This is the client code. The server's source code is outside my control. Commented Jan 21, 2016 at 12:22
  • make dos.flush(); to force data to be written Commented Jan 21, 2016 at 12:22
  • I have also tried dos.flush(). It doesn't seem to make a difference in this case. Commented Jan 21, 2016 at 12:24
  • if the first write() works and flushing die outputstream doesn't make a difference, close the stream after the first write and open it again to send the second package. Commented Jan 21, 2016 at 12:28

1 Answer 1

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.

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.