This is my client application that is connected to a server. I use an object called DataPackage to send information back and forth. The code runs in its own thread.
The variable in is an ObjectInputStream and out is an ObjectOutputStream.
DataPackage dp = null;
while (true) {
try {
dp = (DataPackage) in.readObject();
if (dp != null) {
// Do stuff with received object
}
if (sendSomething) { // Send stuff
out.writeObject( new DataPackage("some data") );
out.flush();
out.reset();
}
} catch (ClassNotFoundException | IOException e1) {
e1.printStackTrace();
}
try {
t.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
The problem with my code is that I never reach the part where I send stuff, unless I receive something from the server. The loop waits for something from the server before it continues to if (sendSomething). How should I structure my code so that I can send stuff any time and receive stuff any time? I hope you understand.