11
I'm programming a class to communicate to a server but when it tries to construct the ObjectInputStream with the help of the inputstream the program is freezing. Theres is no Exception and the program is still running but hanging in the line where it tries to construct the ObjectInputstream.
Heres the code of the method where my problem is located:
@Override
public void connect(String ip, int port) throws UnknownHostException, IOException {
Socket socket = new Socket(ip, port);
out = new ObjectOutputStream(socket.getOutputStream());
InputStream is = socket.getInputStream();
in = new ObjectInputStream(is);
}
and this is the code for the whole class:
package Client;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
public class MessageStreamerImpl implements MessageStreamer {
ObjectOutputStream out;
ObjectInputStream in;
public MessageStreamerImpl(String ip, int port) throws UnknownHostException, IOException{
connect(ip, port);
}
public MessageStreamerImpl(){
}
@Override
public void send(Object message) throws IOException {
if(out == null) throw new IOException();
out.writeObject(message);
out.flush();
}
@Override
public Object receive() throws IOException{
try {
return in.readObject();
} catch (ClassNotFoundException e) {
throw new IOException();
}
}
@Override
public void connect(String ip, int port) throws UnknownHostException, IOException {
Socket socket = new Socket(ip, port);
out = new ObjectOutputStream(socket.getOutputStream());
InputStream is = socket.getInputStream();
in = new Obj