0

ゲームのソケットを介してオブジェクトを送信しようとしていますが、送信に時​​間がかかり、ゲームがハングする可能性があります。BufferedOutputStreams と BufferedInputStreams を使用してデータを送信したいのですが、クライアント側で BufferedOutputStream を使用すると、サーバー側で ObjectInputStream が初期化されません。奇妙なことは、エラーがスローされないことです。

関連するコードのみを提供します。そうしないと、何が起こっているのかを説明するのに長い時間がかかるからです。ゲームごとに 2 つのクライアントが初期化されます。

/*Server Code*/

ObjectOutputStream toClients;//stream to both players
ObjectInputStream fromClients;//stream from both players
Socket client1;//player one socket
Socket client2;//player two socket
public RunGame(Socket client1, Socket client2)throws IOException//constructor of a new thread
{
    this.client1=client1;
    this.client2=client2;
}
public void run()//for the thread
{
    try{
        this.createGame();
        /*
         rest of code for server when running game
         */
    }
    catch(IOException e){e.printStackTrace();}
    catch(ClassNotFoundException e){e.printStackTrace();}
}
public void createGame()
{
    try{
        System.out.println("about to create");//this prints out
        fromClients=new ObjectInputStream(client1.getInputStream());//first initialization

        System.out.println("created");//this doesn't
        String s1=(String)fromClients.readObject();

        fromClients=new ObjectInputStream(client2.getInputStream());//sets input to player 2
        String s2=(String)fromClients.readObject();
    }
    catch(IOException e){e.printStackTrace();}
    catch(ClassNotFoundException e){e.printStackTrace();}
}

/*Client Code*/
Socket sock;//created in the constructor of the thread
ObjectOutputStream toServer;
ObjectInputStream fromServer;
public void run()
{
    try{
    System.out.println("about to create");//this prints
    toServer=new ObjectOutputStream(new BufferedOutputStream(sock.getOutputStream(),8*1024));//bufferedoutputstream is here
    toServer.writeObject("String that is to be sent to server");
    System.out.println("written");//this also prints
    }
    catch(IOException e){e.printStackTrace();}
    catch(ClassNotFoundException e){e.printStackTrace();}
    /*
     rest of client code
     */
}

私はすべてのフォーラムを調べてきましたが、うまくいくものを見つけることができませんでした。あなたが与えることができるどんな助けにも感謝します!

4

1 に答える 1

1

そうしないと.flush()、出力がソケットに送信されません。ObjectOutputStreamBufferedOutputStream

于 2012-06-21T23:19:46.730 に答える