无法实例化ObjectInputStream

时间:2013-12-01 18:12:34

标签: java sockets objectinputstream

我正在尝试在第24行的客户端中实例化一个ObjectInputStream,但它不会,我可以实例化一个ObjectOutputStream而不是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());
        }

1 个答案:

答案 0 :(得分:1)

你还没有说发生了什么,但我怀疑它是封锁的。

ObjectInputStream阻塞,直到它从ObjectOutputStream获取一个头,这可能需要来自另一端的flush()