简体   繁体   中英

Java sockets are not reading data

I'm programming a server (Java) - client (Android/Java) application. The server is a W7. All the communication goes well until one read in the client that freezes and stops reading data until I send it 2 times more.

The data not read is a byte array. I repeat that all the communication goes well until this point.

Here's the code that I use to send the data:

Long lLength = new Long(length);
byte [] bLength = this.longToBytes(lLength.longValue());
dos.write(bLength);
dos.flush();

dos.write(bLength);
dos.flush();

dos.write(bLength);
dos.flush();

This code transforms a long value into an 8 bytes array. As I said, when the first write is executed (and the client is waiting for data), the read is not done. It is not done until I execute the last write().

And here's the code to read it:

byte length[] = {0,0,0,0,0,0,0,0};
dis.read(length);

I've used Wireshark to sniff the traffic, and I can see that the byte array is send, and the client answers with an ACK, but the read is not done.

In the client and the server, the sockets are setup like this:

socket = new Socket(sIP, oiPort.intValue());
dos = new DataOutputStream(socket.getOutputStream());
dis = new DataInputStream(socket.getInputStream());

This is driving me mad... I don't know why, at one moment, the application stops reading the data, when I send it the same way as always.

I suppose that the problem may be in the input buffers of the client socket... But I don't know what to do or what to try...

Say that I've also test the server in a WXPSP3 and it still doesn't work.

First thing I'd look at is the code for your longToBytes method. Is it really creating a byte array of 8 bytes? If it is generating an array of less than 8 bytes, then that explains the problem. (Your client is expecting 8 bytes, and will block until they all arrive.)

Next thing I'd ask myself is why I'm not just using writeLong and readLong . It would simplify your code, and quite possibly cure the problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM