1

On my server class I am writing a 2D char array out through an ObjectOutputStream and the client reads this with an ObjectInputStream. Afterwards I try to send another 2D array through the same ObjectOutputStream to the same ObjectInputStream but the program crashes. I guess my newline is left within the ObjectOutputStream but I am unsure of how to get rid of that. I tried inFromServ.read(), inFromServ.readLine(), inFromServ.flush(). None of which fixed the problem =/. Any help would be appreciated!

Client code:

ObjectInputStream inFromServ = new ObjectInputStream(socket.getInputStream());
printBoard((char[][])inFromServ.readObject()); //Prints the first 2D char array fine

System.out.println(inFromServ.readObject()); //Reads in a string fine
System.out.println(inFromServ.readObject()); //Reads in another string fine

System.out.println("Your shots board:"); //Writes this out fine
printBoard((char[][])inFromServ.readObject()); //Crashes here

Server code:

ObjectInputStream in = new ObjectInputStream(clients[0].getInputStream());
ObjectOutputStream out=new ObjectOutputStream(clients[0].getOutputStream());
char p1brd[][]=new char[10][10];
char p1shots[][]=new char[10][10];

out.writeObject(p1brd); //Writes out the first board fine
out.writeObject("some string"); //Writes this string fine
out.writeObject("some more string"); //Writes this string fine
out.writeObject(p1shots); //Don't even think it gets here... If i put a thread.sleep(10000) before this line it still crashes instantaneously after writing "some more string"

The client side is within a while(true) loop. Once the printBoard((char[][])inFromServ.readObject()); is called for the second time my try/catch gets a hold of it and spews out the catch statement over and over until I stop the program.

1
  • 1
    How does it crash? I assume there's an exception? Do both the client and the server crash? Please read tinyurl.com/so-list and edit your question with more information. Commented Dec 4, 2012 at 6:59

1 Answer 1

1

It doesn't 'crash' at all, it throws an exception, and you have a bug in your logic whereby you don't abandon reading when you've got an exception. Any exception reading from a socket other than SocketTimeoutException is fatal and the only proper response is to close the socket; stop reading it; and abandon the client.

Next time you report a 'crash' please provide the actual exception and stack trace.

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

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.