日期:2014-05-20  浏览次数:20837 次

利用socket发送数据的问题
这几天在写PC端和手机端的远程监控,我现在写到截获PC端摄像头的数据并得到BufferedImage,然后利用ImageIO写到socket的输出流里面,然后在客户端(我先在PC端写的客户端,没有在安卓手机端)里从socket里得到输入流,在用ImageIO写到硬盘里,可是只能写入成功一张图片,然后就会出
Java code

java.net.SocketException: Connection reset by peer: socket write error
    at java.net.SocketOutputStream.socketWrite0(Native Method)
    at java.net.SocketOutputStream.socketWrite(Unknown Source)
    at java.net.SocketOutputStream.write(Unknown Source)
    at java.io.BufferedOutputStream.flushBuffer(Unknown Source)
    at java.io.BufferedOutputStream.write(Unknown Source)
    at java.io.DataOutputStream.writeInt(Unknown Source)
    at ss.snow.service.ImageSend$Worker.run(ImageSend.java:91)


这个问题,如果不发送,只是在服务端直接保存到硬盘里可以连续保存。求大神帮忙,现贴代码如下:
服务端:
Java code

private class Worker extends Thread {
        private final int[] data = new int[320 * 240];
        private FrameGrabbingControl sendFrame;
        private Buffer buffer;
        private BufferToImage image;
        private BufferedImage sendImage;
        private ServerSocket ss;
        private Socket socket;
        private OutputStream outStream;
        private boolean isRunning = false;
        private DataOutputStream dos;

        @Override
        public void run() {
            try {
                ss = new ServerSocket(8000);
            } catch (IOException e1) {
                e1.printStackTrace();
            }

            try {
                isRunning = true;
                while (isRunning) {
                    boolean isConnected = false;
                    socket = ss.accept();
                    outStream = socket.getOutputStream();
                    
                    System.out.println("有客户端连接");
                    isConnected = true;
                    while (isConnected) {
                        sendFrame = control;
                        buffer = sendFrame.grabFrame();
                        image = new BufferToImage((VideoFormat) buffer.getFormat());
                        
                        
                        sendImage = (BufferedImage) image.createImage(buffer);
                        /*
                        sendImage.getWritableTile(0, 0).getDataElements(0, 0,
                                320, 240, data);
                        sendImage.releaseWritableTile(0, 0);
                        dos = new DataOutputStream(new BufferedOutputStream(outStream));
                        for(int i = 0; i < data.length; i++) {
                            dos.writeInt(data[i]);
                        }
                        dos.flush();
                        */
                        ImageIO.write(sendImage, "JPEG", outStream);
                    }
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (outStream != null) {
                    try {
                        outStream.close();
                        outStream = null;
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if (socket != null) {
                    try {
                        socket.close();
                        socket = null;
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if (ss != null) {
                    try {
                        ss.close();
                        ss = null;
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }


这个服务端里注释掉的代码我想用缓冲区像外发送data,可是这个在本地都不行,这个也不知道为什么。求解答。