John Collins
John Collins

Reputation: 131

Cannot instantiate an ObjectInputStream

i'm trying to instantiate an ObjectInputStream in a client on line 24 but it won't, I can instantiate an ObjectOutputStream but not Input.

    private class ClientThread implements Runnable{
    Socket s;
    ObjectInputStream inObject;
    ObjectOutputStream outObject;
    ServerSocket ss;
    Integer portNo;
    ClientThread(int portNo){
        try{
            this.portNo = portNo;
            ss = new ServerSocket(portNo);  
        }               
        catch(IOException e){
            System.out.println("CT : "+e.getMessage());
        }
    }

    public void run(){

        boolean hasOpponent = false;
        try{
            while(!hasOpponent){
                s = ss.accept();                                
                if(s != null){
                    inObject = new ObjectInputStream(s.getInputStream());   // line 24  
                    System.out.println("1");     
                    hasOpponent = true; 
                    game = new Game(name1, name2);                  
                }
            }
        }
        catch(Exception e){
            System.out.println("D "+ e.getMessage());
        }

Upvotes: 0

Views: 275

Answers (1)

Peter Lawrey
Peter Lawrey

Reputation: 533472

You haven't said what does happen but I suspect it is blocking.

ObjectInputStream blocks until it get a header from an ObjectOutputStream which may require a flush() from the other end.

Upvotes: 1

Related Questions