Welcome to the fabulous world of networks. I discovered my passion. :) However I have a very strange behavior in my app, and I would need your help to solve this one.
I made a simple server-client app. The sending Thread :
new Thread(new Runnable() {
public void run() {
try {
ObjectOutputStream objectOutputStream = new ObjectOutputStream(new BufferedOutputStream(socket.getOutputStream()));
objectOutputStream.writeObject(message);
objectOutputStream.flush();
} catch (Exception exception) {
exception.printStackTrace();
}
}
}).start();
The receiving Thread :
new Thread(new Runnable() {
public void run() {
try {
while (!Thread.currentThread().isInterrupted()) {
ObjectInputStream objectInputStream = new ObjectInputStream(new BufferedInputStream(socket.getInputStream()));
Message message = (Message) objectInputStream.readObject();
Log.i("DEBUG", message);
}
} catch (Exception exception) {
try {
socket.close();
} catch (Exception exception) {
exception.printStackTrace();
}
}
}
}).start();
It works just fine, however if I send simultaneously 3 messages , my receiving thread only receives the 2 first ones . It does not matter if I change the order. The third is always lost.
I think it's a buffer size problem. How can I deal with that? Thank you. :)
BufferedReader buffers the input, just as the name says. This means that it reads from the input source into a buffer before passing it onto you. The buffer size here refers to the number of bytes it buffers.
ObjectInputStream objectInputStream = new ObjectInputStream(new BufferedInputStream(socket.getInputStream(), size));
you can use size of BufferedInputStream
and the reading of the buffer
is slow so send the data with some delay `
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.