客户端套接字在一个循环后关闭

时间:2017-05-02 21:44:12

标签: java serversocket

我正在使用java中的屏幕共享应用程序,但它没有工作

所以基本上我试图通过套接字发送屏幕截图,但客户端套接字在发送一个屏幕截图后关闭

服务器

   try {
        server = serverSocket.accept();
        in = new BufferedReader(new InputStreamReader(server.getInputStream()));
        server.setKeepAlive(true);
    } catch (IOException e1) {
        e1.printStackTrace();
    }
  while(true)
  { 

          BufferedImage screenshot = null;
        try {
            screenshot = ImageIO.read(server.getInputStream());
        } catch (NullPointerException | IOException e) {
            System.out.println("exception");
        }
        if(screenshot != null)
            panel.image = resize(screenshot, frame.getWidth(), frame.getHeight());
        else
            System.out.println("null");
        frame.revalidate();
        panel.repaint();
  }

客户端

String serverName = "127.0.0.1";
int port = 27005;

client = new Socket(serverName, port);
client.setKeepAlive(true);
DataOutputStream out = new DataOutputStream(client.getOutputStream());

while(client.isConnected())
{

        Robot bot = new Robot();
        bimg = bot.createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));
        ImageIO.write((RenderedImage)bimg, "jpg", out);
        out.flush();
}

0 个答案:

没有答案