通过套接字将图像作为Java中的字节数组发送

时间:2014-12-21 17:04:00

标签: java image sockets stream byte

我知道这有几种不同的方式被问到,但我已经做了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;
    }

}

感谢任何可以提供帮助的人。这让我想起了INSANE。

1 个答案:

答案 0 :(得分:0)

简化为必需品,这是你的阅读循环:

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()。将闭合移出循环。