Re: Socket is still connected after Server-Side socket termination.
To make things easier here is the code of the method run of the
looping thread :
StringBuffer outgoingBuffer;
List<Message> incomming;
while (running) {
try {
// Count the number of
messages the outgoing queue has
int messageCount = outgoing.size();
outgoingBuffer = new StringBuffer();
for (int i=0;i<messageCount;i++) {
outgoingBuffer.append(new
String(parser.serialize(outgoing.poll())));
}
out.write(outgoingBuffer.toString().getBytes());
int availableBytes = in.available();
byte[] toDeserialize = new byte[availableBytes];
in.read(toDeserialize);
// Use parser to serialize/
deserialize message (from/to JSON)
incomming = parser.deserialize(toDeserialize);
if (incomming.size()>0) {
System.out.println("server: "+new String(toDeserialize));
for (Message message:incomming) {
// Send all
registered messageListeners the message that has been received.
eventManager.fireEvent(message);
}
}
Thread.sleep(100);
} catch(SocketException e) {
for (int i=0;i<CONNECTION_ATTEMPTS;i++) {
try {
System.out.println("Attempt to connect...");
disconnect();
parser.reset();
connect(hostname,port);
out.write(parser.serialize(createRecoveryMessage()));
System.out.println("Connected!");
break;
} catch(Exception e1) {
e1.printStackTrace();
try { Thread.sleep(1000); } catch(InterruptedException e2) {}
}
}
} catch(IOException e) {
} catch(InterruptedException e) {
}
}
try {
socket.close();
} catch(IOException e) {e.printStackTrace();}
}