0

ある種の回線を管理するためのクライアント/サーバー アプリがあります。すべてのクライアントが私のラインにオブジェクトを追加します。

行が変更されたり、行が挿入または削除されたりするたびに、サーバーが jpanel のスクリーン キャプチャをクライアントに送信するようにします。jpanelをjpegにキャプチャして送信することさえできました。しかし、アプリのフローが停止します。最初の更新後に、リスニング サーバー ソケットを終了する eofexception が発生します。

クライアントを更新する正しい方法は何ですか? クライアント側でも常にリッスンするようにサーバーソケットを設定する必要がありますか?

助けてください、私はこれで2週間ほど立ち往生しています。

これは私のリッスン スレッド (サーバー) です。

public class ListeningThread implements Runnable {

static boolean listening = true;    
public BufferedReader in;


public void run() {

    ServerSocket echoServer = null;
    String line;
    DataInputStream is = null;
    PrintStream os = null;
    Socket clientSocket = null;

   try {
       echoServer = new ServerSocket(RequestReciever._communicationPort);
    }
    catch (IOException e) {
       System.out.println(e);

    }   

// Create a socket object from the ServerSocket to listen and accept 
// connections.
// Open input and output streams

try {


// As long as we receive data, send it to be phrased to a request.
        while (true) {

         clientSocket = echoServer.accept();
         is = new DataInputStream(clientSocket.getInputStream());
         os = new PrintStream(clientSocket.getOutputStream());

 // An option for a stop listening button. currently not available !
         if( listening==true )  {
         line = is.readUTF();
         os.println(line); 
         System.out.println(line);
         RequestReciever.pharseToRequest(line);
//           clientSocket = null;


       }
       else  {
                echoServer.close();
                is.close();
                os.close();
                break;
            }
       }

}   

catch (IOException e) {
       e.printStackTrace();
       System.out.println("Listening Thread Unknown error");
    }
}
}

これは私のフェーズメソッドです:

 public static void pharseToRequest(String input) {

    List<String> list = new ArrayList<String>(Arrays.asList(input.split(";;;")));
    if (list.get(0).equalsIgnoreCase("Login") && list.get(1).equalsIgnoreCase ("Login") && list.get(2).equalsIgnoreCase("5"))
    {
        _adminClients.add(list.get(4));
        updateScreenCapture();
        AdminClientUpdate tmp = new AdminClientUpdate(list.get(4));
        Thread aCU = new Thread (tmp);
        aCU.start();
    }
    else
    {
    ServerRequest newReq = new ServerRequest(list.get(0), list.get(1), Integer.parseInt(list.get(2)),list.get(3),list.get(4));
    addRequest(newReq);
    }
}

これは AdminClientUpdate クラスです

public class AdminClientUpdate implements Runnable {

static boolean listening = true;    
public BufferedReader in;
public String _ip;

public AdminClientUpdate(String ip)
{
    _ip = ip;
}

public void run() {

        try {
            Socket socket = new Socket(_ip,   RequestReciever._communicationPort);
            InputStream in = new FileInputStream("Capture/tmp.jpg");
            java.io.OutputStream out = socket.getOutputStream();

            copy(in, out);
            System.out.println("Sent Image !");
            socket.close();
            out.close();
            in.close();

        } catch (UnknownHostException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            System.out.println("Cant find tmp.jpg");
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }   
    }


    static void copy(InputStream in, java.io.OutputStream out) throws IOException {
            byte[] buf = new byte[8192];
            int len = 0;
            while ((len = in.read(buf)) != -1) {
                out.write(buf, 0, len);
            }
            in.close();
            out.close();
     }
}
4

2 に答える 2

0

いくつかの頭のメルトダウンの後、クライアント側にサーバーソケットを配置して、サーバーからの更新をリッスンすることが最善の方法であると判断しました。いくつかの点を修正しました: * サーバーは、受け入れスレッドでインラインで各接続を処理するのではなく、受け入れられたすべての接続を処理するために新しいスレッドを開始する必要があります。* ログインの初期化ではなく、サーバー ソケット経由で最初の更新を取得しようとしました。ログイン中に最初の更新を取得した後、クライアント側にサーバーソケットを追加して、サーバーからのさらなる更新をリッスンし続けるようにしました。

于 2014-02-16T09:59:06.597 に答える
0

これをなくす

echoServer.close();

この行はソケットを閉じます。そのため、接続は中止されます。

于 2014-02-07T05:05:41.690 に答える