0

したがって、この段階では、サーバーとクライアントがあり、それぞれに入力ストリームと出力ストリームの両方があります。

VLC ラッパーを使用してビデオ ストリーマーを実装しています。基本的に、クライアントでビデオを選択すると、サーバーが次に受信することを期待するオブジェクトの文字列が送信されます。(videoFile/commentWrite/ratingWrite)。ビデオを選択すると、これら 3 つのオブジェクトがすべて送信されるため、ストリームが更新され、コメント ボックスと評価ボックスが更新されます。

問題は、これらのヘッダーを送信してデータを受信する必要がある場合、次のようなものを使用する必要があることです。

        replyToServer.writeObject("videoFile");
        replyToServer.writeObject(selectedVideo);       

        //get comments
        listening();

        //get rating
        listening();

したがって、期待している多くの返信を前もって知っておくことは、ハードコーディングです。さらに、クライアントでの作業の任意の時点でサーバーから何かを受信したいが、単純な while ループを使用して listen() 関数をチェックできない場合は、他のすべての機能がブロックされるためです。

PS。リスナーは次のように構成されています。

public void listening() throws IOException, ClassNotFoundException {
    String mode = null;
            // blocks the process till gets the output.
        mode =  (String) inputFromServer.readObject();
        System.out.println("Mode Received:" +mode);

    if (mode.equals("videoList")) {
        getListFromSocket();
    } else if [.....]

それで...それで、スレッドを使用して実行できることがわかりました。Javaを使用してスレッドを実際に実行したことはあまりありませんが、(String) inputFromServer.readObject();で何らかの割り込みを行うことは可能ですか? またはinputFromServer.available()では、ストリームに何かがあることがわかるとすぐに、listening() 関数にジャンプしますか?

アップデート:

lxx コードを使用して、受信している null 例外が GUI の更新によるものであることに気付きました。その listen(); に関数 updateComments があります。コメントなどを見ると、すべて正常に動作しているように見えますが、それでもコンソールに null 例外が表示されます...

    public void updateComments(List<Comment> commentList) {



    commentLabel.setText("<html><div style='width:130px; border: 1px solid black; list-style-type: none; text-align:left;'>");
    if (commentList.size() == 0) {

         commentLabel.setText(commentLabel.getText()+"<font style='font-family: Century Gothic, sans-serif;  font-weight:normal; color:#444;  font-size:9px;'>No comments...</font><br>");

    } else {

    for (Comment tempComment : commentList) {

         commentLabel.setText(commentLabel.getText()+"<div style='margin:3px;'><font style='font-family: Century Gothic, sans-serif;  font-size:9px; color:#333;'>"+tempComment.getUser()+":</font><br>");
         commentLabel.setText(commentLabel.getText()+"<font style='font-family: Century Gothic, sans-serif; font-weight:normal; font-size:8px;'>"+tempComment.getText()+"</font><br>");
         commentLabel.setText(commentLabel.getText()+"<font style='font-family: Century Gothic, sans-serif; font-weight:normal; font-size:7px;'><i>"+tempComment.getDate()+"</i></font></div><hr>");
    }


    }
     commentLabel.setText(commentLabel.getText()+"</div></html>");

}
4

1 に答える 1

2

リスニング()、またはリスニング(ブロッキング操作)を呼び出すループをスレッドに入れてみませんか:

new Thread(new Runnable() {

    @Override
    public void run() {
        listening();

    }

}).start();

このように、ブロック操作は別のスレッドにあり、メイン コードは通常どおり実行され続けます。

于 2012-05-28T16:10:31.427 に答える