new ObjectInputStream() blocks

public class SerProg {


    static ServerSocket ser=null;
    static Socket cli=null;
    static ObjectInputStream ins=null;
    static ObjectOutputStream outs=null;


    public static void main(String[] args) {

        try {

            ser=new ServerSocket(9000,10);
            cli=ser.accept();

            System.out.println("Connected to :"+cli.getInetAddress().getHostAddress()+" At Port :"+cli.getLocalPort());

            ins=new ObjectInputStream(cli.getInputStream());
            outs=new ObjectOutputStream(cli.getOutputStream());

            String str=(String)ins.readObject();


        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


    }

}

And the Client

public class SerProg {

    /**
     * @param args
     */
    static ServerSocket ser=null;
    static Socket cli=null;
    static ObjectInputStream ins=null;
    static ObjectOutputStream outs=null;


    public static void main(String[] args) {
        // TODO Auto-generated method stub

        try {

            ser=new ServerSocket(9000,10);
            cli=ser.accept();

            System.out.println("Connected to :"+cli.getInetAddress().getHostAddress()+" At Port :"+cli.getLocalPort());

            ins=new ObjectInputStream(cli.getInputStream());
            outs=new ObjectOutputStream(cli.getOutputStream());

            String str=(String)ins.readObject();


        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


    }

}

The Connection is established successfully but in the Server Code Line

ins=new ObjectInputStream(cli.getInputStream());

the code halts and does not proceed,what might be the problem ??

Answers


You need to create the ObjectOutputStream before the ObjectInputStream at both sides of the connection(!). When the ObjectInputStream is created, it tries to read the object stream header from the InputStream. So if the ObjectOutputStream on the other side hasn't been created yet there is no object stream header to read, and it will block indefinitely.

Or phrased differently: If both sides first construct the ObjectInputStream, both will block trying to read the object stream header, which won't be written until the ObjectOutputStream has been created (on the other side of the line); which will never happen because both sides are blocked in the constructor of ObjectInputStream.

This can be inferred from the Javadoc of ObjectInputStream(InputStream in):

A serialization stream header is read from the stream and verified. This constructor will block until the corresponding ObjectOutputStream has written and flushed the header.

This is also described in section 3.6.2 of Fundamental networking in Java by Esmond Pitt.


Need Your Help

Bundle 'org.eclipse.core.runtime' cannot be resolved

eclipse eclipse-plugin eclipse-pde

I recently upgraded from Eclipse Kepler to Luna. A plugin I had been working on is now showing build errors without any source being changed.

Does a docker container have its own TCP/IP stack?

networking docker tcp-ip linux-containers

I'm trying to understand what's happening under the hood to a network packet coming from the wire connected to the host machine and directed to an application inside a Docker container.