在Android编程中使用套接字和对象IO流

时间:2012-05-14 21:00:30

标签: java android sockets

我正在尝试在我正在编写的游戏中实现多人游戏,并且我已经获得了成功连接的所有内容(我认为......),但是当我运行它时,客户端会抛出一个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();
            }

    }
}

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

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:0)

结果是服务器没有正确启动它的输入和输出流,即使它的套接字成功了。不知道为什么,但只有先从输出流开始,然后输入(?)才能工作。有一些其他非常奇怪的错误,但至少沟通似乎工作..我会在发布之前更多地了解它们。谢谢你们!