1

I'm trying to implement multiplayer in a game I've been writing, and I've gotten everything to successfully connect (I think..), but when I'm running it, there's an EOFException thrown by the client, and the object (an ArrayList) isn't successfully received.

Code for the server thread:

    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();
            }

    }
}

And then the code for the client:

    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();
            }

    }
}

What does this mean? And perhaps more importantly, how can I fix it? Thanks in advance..

2
  • Are you are trying to send serialized object(s) over the network interface (wi-fi/mobile)? Between Android devices or from a server to an Android device? Commented May 14, 2012 at 21:09
  • I'm trying to send an ArrayList between two Android devices over wifi. The IP address and sockets are correct because the connection itself is successful, just not the data transfer. Commented May 14, 2012 at 21:23

2 Answers 2

1

I don't see you closing your outputstream.

See this SO topic: Problem serializing and deserializing ArrayList

Sign up to request clarification or add additional context in comments.

1 Comment

True but the only way he can get an EOFException is if he is closing something, either one of the streams or the socket.
0

Turns out the server wasn't properly initiating it's input and output streams, even though its sockets were successful. Dunno why, but it only works if I started with the output stream first, then the input (?). Having some other really strange bugs, but at least the communication seems to work.. I'll look more in to them before posting here about it. Thanks guys!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.