1

我正在尝试在我一直在编写的游戏中实现多人游戏,并且我已经成功连接了一切(我认为..),但是当我运行它时,客户端抛出了 EOFException,并且未成功接收对象(一个 ArrayList)。

服务器线程的代码:

    class ServerThread implements Runnable
{
    ServerSocket server = null;
    Socket controlSocket = null;
    ObjectOutputStream outStream = null;
    ObjectInputStream inStream = null;
    @Override
    public void run() {
        setupConnection();
        while(true){
            sendObject(out.getStuff());     
        }


    }
    void setupConnection(){
        Log.e("OUTPUTSHOOTER","init-connect");
        try {
            server = new ServerSocket(SERVERPORT);
            Log.e("OUTPUTSHOOTER","server initiated port: "+SERVERPORT);
        controlSocket = server.accept();
        Log.e("OUTPUTSHOOTER","connected");
        inStream =  new ObjectInputStream(controlSocket.getInputStream());
        outStream = new ObjectOutputStream(controlSocket.getOutputStream());
        } catch (StreamCorruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        Log.e("OUTPUTSHOOTER",server+" "+controlSocket+" "+inStream+" "+outStream);
        }

    public Object recieveObject(){
        Object o = null;
        try {
            o = inStream.readObject();
        } catch (OptionalDataException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return o;
    }
    public void sendObject(Object o)
    {
            try {
                outStream.writeObject(o);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

    }
}

然后是客户端的代码:

    class ClientThread implements Runnable
{
    Socket controlSocket = null;
    ObjectOutputStream outStream = null;
    ObjectInputStream inStream = null;
    @Override
    public void run() {
        setupConnection();
        while(true){
            Log.e("OUTPUTSHOOTER","recieving");
            Object in = recieveObject();
            if(in!= null && in instanceof ArrayList)
            {
                Log.e("OUTPUTSHOOTER","loading");
                out.load((ArrayList<UniverseObject>)in);
            }       
        }


    }

    void setupConnection(){
        Log.e("OUTPUTSHOOTER","ip: "+SERVERIP);
        while(controlSocket == null) {
            try {
                controlSocket = new Socket(SERVERIP,SERVERPORT);
                Log.e("OUTPUTSHOOTER","socket connected");
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
        try {
            Log.e("OUTPUTSHOOTER","attempting streams");
            outStream = new ObjectOutputStream(controlSocket.getOutputStream());
            Log.e("OUTPUTSHOOTER","output working");
            inStream =  new ObjectInputStream(controlSocket.getInputStream());
            Log.e("OUTPUTSHOOTER","streams connected");

        } catch (StreamCorruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
    }
    }
    public Object recieveObject(){
        Object o = null;
        try {
            o = inStream.readObject();
        } catch (OptionalDataException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return o;
    }
    public void sendObject(Object o)
    {
            try {
                outStream.writeObject(o);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

    }
}

这是什么意思?也许更重要的是,我该如何解决?提前致谢..

4

2 回答 2

1

我没有看到你关闭你的输出流。

请参阅此 SO 主题:Problem serializing and deserializing ArrayList

于 2012-05-14T22:02:01.013 回答
0

事实证明服务器没有正确启动它的输入和输出流,即使它的套接字是成功的。不知道为什么,但只有当我先从输出流开始,然后是输入(?)时它才有效。还有一些其他非常奇怪的错误,但至少沟通似乎有效.. 在发布到这里之前,我会更多地研究它们。多谢你们!

于 2012-05-16T01:57:14.603 回答