ObjectInputStream blocks on initialization Sockets

Andrea Dattero :

As a school project I have to make an application on Android and Java that shares Objects for this it is required using Object Input and Output streams(it doesn't matter if it's not an efficient communication).

Problems:

  • The Server side stops and sits there when the ObjectInputStream is initialized
  • When i manually force close the application (client side) it throws an Exception

Server Side

public class Connection extends Thread {

    public final String address;
    public final int port;
    public final ArrayList<Device> devices;

    public Connection(String address, int port) {
        this.address = address;
        this.port = port;
        this.devices = new ArrayList<>();
    }

    // listen for connections
    @Override
    public void run() {
        try {
            ServerSocket server = new ServerSocket(this.port);
            while(true){
                Socket socket = server.accept();
                // it stops here idk why
                ObjectInputStream input = new ObjectInputStream(socket.getInputStream());
                String name = (String) ;
                devices.add(new Device(name,socket,input));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

Client side

public class Connection extends Thread {

    private Socket connection;
    private ObjectInputStream input;
    private ObjectOutputStream output;
    private final String name;
    private final String address;
    private final int port;

    public Connection(String name, String address, int port) {
        this.name = name;
        this.address = address;
        this.port = port;
    }

    @Override
    public void run() {
        try {
            System.out.println("Initializing connection");
            // initializes the socket and the streams
            initConnection();
            System.out.println("Connection established");
            // sends the id of the device
            sendIdentification(); // sends this.name but the code stops before sending the id idk why

            /*The code continues here but you shouldn't care cause the problem comes before*/

        } catch (Exception e) {
            // on connection failed tries reconnecting
            System.out.println("Trying connecting to Server please wait...");
            new Thread(new Connection(name,address,port)).start(); // you should not care even about this cause the connection the first time always goes ok
        }
    }

    // init the connection
    private void initConnection() throws Exception {
        this.connection = new Socket(address, port);
        this.input = new ObjectInputStream(this.connection.getInputStream());
        this.output = new ObjectOutputStream(this.connection.getOutputStream());
    }

}

Exception

java.io.EOFException
    at java.io.ObjectInputStream$PeekInputStream.readFully(ObjectInputStream.java:2681)
    at java.io.ObjectInputStream$BlockDataInputStream.readShort(ObjectInputStream.java:3156)
    at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:862)
    at java.io.ObjectInputStream.<init>(ObjectInputStream.java:358)
    at bane.connection.Connection.run(Connection.java:31)
Tix :

Try call socket.getOutputStream() before getInputStream in server side code.

.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=417423&siteId=1