多个BufferedImage传输套接字

时间:2014-03-21 06:52:34

标签: java sockets bufferedimage

我正在尝试将多个gif图像作为BufferedImages从客户端传输到服务器。但是一旦单个图像开始传输,客户端套接字就会关闭。我试过的代码如下

服务器

    private void connect() {
        int noOfFiles, w, h;
        BufferedImage bi;
        try {
            ServerSocket ss = new ServerSocket(5223);
            Socket s = ss.accept();
            InputStream is = s.getInputStream();
            DataInputStream dis = new DataInputStream(is);
            noOfFiles = dis.readInt();
            System.out.println("Total Files: " + noOfFiles);
            dis.close();
            s.close();
            ServerSocket ssa = new ServerSocket(5225);
            Socket sa = ssa.accept();
            InputStream isa = sa.getInputStream();
            for (int x = 0; x < noOfFiles; x++) {
                File imgFile = new File("D:\\aa\\" + x);
                bi = ImageIO.read(isa);
                w = bi.getWidth();
                System.out.println(w);
                h = bi.getHeight();
                if (bi.getType() != BufferedImage.TYPE_INT_RGB) {
                    BufferedImage bi1 = new BufferedImage(w, h,
 BufferedImage.TYPE_INT_RGB);
                    Graphics g = bi1.getGraphics();
                    g.drawImage(bi, 0, 0, null);
                }
                ImageIO.write(bi, "GIF", imgFile);                
            }
            sa.close();
            ssa.close();            
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

客户端

 private void connect() {
        Image i;
        BufferedImage bi = null;
        try {
            Socket s = new Socket("localhost", 5223);
            DataOutputStream dos = new DataOutputStream(s.getOutputStream());
            File fol = new File("D:\\images\\red");
            File[] files = fol.listFiles();
            dos.writeInt(files.length);
            dos.flush();
            dos.close();          
            Socket ss = new Socket("localhost", 5225);
            for (File f : files) {                
                i = ImageIO.read(f);
                bi = (BufferedImage) i;
                ImageIO.write(bi, "GIF", ss.getOutputStream());                                
            }
            ss.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

我得到的例外情况如下:

服务器

Exception in thread "main" java.lang.NullPointerException
    at PortScanNew.ImgTrnsfrSvr.connect(ImgTrnsfrSvr.java:54)
    at PortScanNew.ImgTrnsfrSvr.main(ImgTrnsfrSvr.java:33)
Java Result: 1

客户端

java.net.SocketException: Connection reset by peer: socket write error
    at java.net.SocketOutputStream.socketWrite0(Native Method)
    at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:113)
    at java.net.SocketOutputStream.write(SocketOutputStream.java:159)
    at javax.imageio.stream.FileCacheImageOutputStream.flushBefore(FileCacheImageOutputStream.java:255)
    at javax.imageio.stream.ImageInputStreamImpl.flush(ImageInputStreamImpl.java:825)
    at javax.imageio.ImageIO.doWrite(ImageIO.java:1615)
    at javax.imageio.ImageIO.write(ImageIO.java:1578)
    at PortScanNew.ImgTrnClnt.connect(ImgTrnClnt.java:44)
    at PortScanNew.ImgTrnClnt.main(ImgTrnClnt.java:26)

我查看了许多例子,但我从他们那里得不到任何想法。有什么建议吗?

0 个答案:

没有答案