在 Java 中通过套接字以字节数组的形式发送图像



我知道有人以几种不同的方式问过这个问题,但我已经为此工作了 2 天,但无济于事。 我的代码失败了,因为接收方不断抛出 EOF 异常。 有人可以指出我正确的方向吗?

接收方:

    class ReceiveThread extends Thread {
    ReceiveThread() {
    }
    @Override
    public void run() {
        System.out.println("Receive Thread Start");
        DataInputStream in;
        try {
        } catch (Exception e) {
            return;
        }
        try {
            in = new DataInputStream(connection.getInputStream());
            while (true) {
                if (!connection.isConnected()) {
                    System.out.println("Connection not connected");
                    break;
                }
                try {
                    int len = in.readInt();
                    byte[] data = new byte[len];
                    System.out.println("Image size: " + len);
                    if (len > 0) {
                        in.readFully(data, 0, len);
                        BufferedImage bi = ImageIO.read(new ByteArrayInputStream(data));
                        panel.updateImage(bi);
                        panel.repaint();
                    }
                    in.close();
                } catch (Exception e) {
                }
                pause(100);
            }
        } catch (Exception e) {
        }
    }
    public void pause(long time) {
        try {
            Thread.sleep(time);
        } catch (Exception e) {
        }
    }
}

发送方:

    class UpdateScreenThread extends Thread {
    Robot robot;
    public UpdateScreenThread() {
        try {
            robot = new Robot();
            System.out.println("Update Thread Created");
        } catch (Exception e) {
        }
    }
    @Override
    public void run() {
        System.out.println("Update Thread Running");
        Settings.isSharing = true;
        Dimension screenSize;
        screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        Rectangle screenRectangle = new Rectangle(screenSize);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        DataOutputStream out;
        try {
            out = new DataOutputStream(s.getOutputStream());
        } catch (Exception e) {
            return;
        }
        while (s.isConnected()) {
            //System.out.println("test");
            BufferedImage bi = robot.createScreenCapture(screenRectangle);
            try {
                ImageIO.write(bi, "PNG", baos);
                baos.flush();
                byte[] bytes = baos.toByteArray();
                out.flush();
                out.writeInt(bytes.length);
                out.flush();
                out.write(bytes);
                System.out.println("Image sent");
            } catch (Exception e) {
            }
            pause(500);
        }
        try {
            out.close();
        } catch (Exception e) {
        }
        Settings.isSharing = false;
    }
}

感谢任何可以提供帮助的人。 这让我发疯了。

简化为基本要素,这是您的读取循环:

public void run() {
//...
    try {
        in = new DataInputStream(connection.getInputStream());
        while (true) {
            //...
            try {
                int len = in.readInt();
                byte[] data = new byte[len];
                in.readFully(data, 0, len);
                //...
                in.close();
            } catch (Exception e) {
            }
            pause(100);
        }
    } catch (Exception e) {
    }
}

请注意,while (true) {...}包括in.close() 。将关闭移出循环。

相关内容

  • 没有找到相关文章

最新更新