1

私のサーバークラスでは、ObjectOutputStreamを介して2D char配列を書き出しており、クライアントはこれをObjectInputStreamで読み取ります。ObjectOutputStreamその後、同じものから同じものに別の2D配列を送信しようとしましたObjectInputStreamが、プログラムがクラッシュします。私の改行は中に残っていると思いますが、ObjectOutputStreamそれを取り除く方法がわかりません。、、、してみましinFromServ.read()た。どれも問題を修正しませんでした=/。どんな助けでもいただければ幸いです!inFromServ.readLine()inFromServ.flush()

クライアントコード:

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

サーバーコード:

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"

クライアント側はwhile(true)ループ内にあります。printBoard((char [] [])inFromServ.readObject()); try / catchがそれを取得し、プログラムを停止するまでcatchステートメントを何度も吐き出すのは2回目です。

4

1 に答える 1

1

「クラッシュ」することはなく、例外がスローされます。また、ロジックにバグがあり、例外が発生したときに読み取りを放棄しません。致命的であり、唯一の適切な応答はソケットを閉じることです。それを読むのをやめなさい。そしてクライアントを放棄します。SocketTimeoutException

次回「クラッシュ」を報告するときは、実際の例外とスタックトレースを提供してください。

于 2012-12-04T09:07:02.007 に答える