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

消除这个

echoServer.close();

此行关闭套接字。由于该连接被中止。

于 2014-02-07T05:05:41.690 回答
0

经过几次大脑崩溃后,我决定在客户端放置一个服务器套接字来监听来自服务器的更新是最好的方法。我修复了一些问题: * 服务器应该启动一个新线程来处理每个接受的连接,而不是在接受线程中处理每个内联连接。* 我试图通过服务器套接字而不是登录初始化来获取第一次更新。现在,在登录时获得第一次更新后,我在客户端添加了一个服务器套接字,因此它将继续侦听来自服务器的进一步更新。

于 2014-02-16T09:59:06.597 回答