Last condition is not passed. Problems with closing the connection in the run() method. I already tried with putting the connection into the try-with-resources-statement and with closing the connection in the finally-block. Both solutions did not work.
package com.codegym.task.task30.task3008;
import java.io.Closeable;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.net.SocketAddress;
public class Connection implements Closeable {
private final Socket socket;
private final ObjectOutputStream out;
private final ObjectInputStream in;
public Connection(Socket socket) {
this.socket = socket;
ObjectOutputStream outStream = null;
ObjectInputStream inStream = null;
try {
outStream = new ObjectOutputStream(socket.getOutputStream());
inStream = new ObjectInputStream(socket.getInputStream());
}
catch (IOException e) {
e.printStackTrace();
}
this.out = outStream;
this.in = inStream;
}
public void send(Message message) throws IOException {
synchronized(out) {
Object obj = (Object) message;
out.writeObject(obj);
}
}
public Message receive() throws IOException, ClassNotFoundException {
synchronized(in) {
Object obj = in.readObject();
Message message = (Message) obj;
return message;
}
}
public SocketAddress getRemoteSocketAddress() {
return this.socket.getRemoteSocketAddress();
}
public void close() throws IOException {
this.socket.close();
this.out.close();
this.in.close();
}
}