通过套接字发送图像 [英] Sending image over Socket

查看:171
本文介绍了通过套接字发送图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过Socket(客户端 - 服务器)发送图像有一个小问题,我只接收UTF文本而不是图像对象,代码是否有问题?,

i have a small problem sending image over Socket(client-server), i receive only "UTF" text but not the image object, is there something wrong with code?,

此代码只发送UTF文本并在服务器端接收,我使用UTF文本识别服务器端的图像(名称),并在识别后将图像对象发送到客户端

This code just sends the UTF txt and it's received on server side, i'm using the UTF text to identify an image(name) on server side and after identified it can send the image Object to client

    /*
     * ServerSide
     * 
     */
    package Interface_class;

    import configuraciones.procesador;
    import java.awt.image.BufferedImage;
    import java.io.DataInputStream;
    import java.io.DataOutputStream;
    import java.io.File;
    import java.io.IOException;
    import java.net.ServerSocket;
    import java.net.Socket;
    import java.net.SocketTimeoutException;
    import javax.imageio.ImageIO;

    /**
     *
     * @author TheCoder
     */
    public class img_monitor extends Thread{
            ServerSocket serverSocket;
           Socket server;
           BufferedImage bimg;
            byte[] bytes;
           public img_monitor() 
           {
               try{
//Opening server socket
              serverSocket = new ServerSocket(6066);
              System.out.println("Conectado al Socket!");
    }
               catch(IOException ex){
                   System.out.println("Error en: "+ex);

              }
           }

           public void run()
           {
//This class gets the path from a property file (works well)
            procesador obj = new procesador();
            obj.UBARCHIVO_CONFIG();

               while(true)
              {
                   try
                   {

                      server = serverSocket.accept();
                      System.out.println("Nuevo cliente conectado!");
                      DataInputStream din=new DataInputStream(server.getInputStream());
                      DataOutputStream dout=new DataOutputStream(server.getOutputStream());                 
    //Receiving image "name" from client as txt
                      String nombre = din.readUTF();
                      System.out.println(nombre);
//Using path+name of image to identify and send over socket
                      bimg = ImageIO.read(new File(obj.getRuta_arch_img()+nombre));
                      System.out.println(obj.getRuta_arch_img()+nombre);
                      ImageIO.write(bimg,"JPG",dout);
                      System.out.println("Image sent!!!");
    //                  server.close();

                      //lblimg.setIcon(img);
                  }
                 catch(IOException e)
                 {
                      e.printStackTrace();
                      break;
                 }
                 catch(Exception ex)
                {
                      System.out.println(ex);
                }
              }
           }

    }

客户端

    /*
     * Client Side 
     * 
     */
    package Interface_class;

    import java.awt.image.BufferedImage;
    import java.io.DataInputStream;
    import java.io.DataOutputStream;
    import java.io.IOException;
    import java.net.ServerSocket;
    import java.net.Socket;
    import java.net.SocketTimeoutException;
    import javax.imageio.ImageIO;

    /**
     *
     * @author TheCoder
     */
    public class Obtener_imagen extends Thread {

        public Obtener_imagen() {
            System.out.println("Loading  Socket!");

        }

        public void run() {
//The class below gets info from database
            CLIENTINFO_CLASS obj = new CLIENTINFO_CLASS();
            obj.consulta();
            try {
                Socket socket = new Socket("localhost", 6066);
    //              DataInputStream din = new DataInputStream(socket.getInputStream());
                DataOutputStream dout = new DataOutputStream(socket.getOutputStream());
               //Using this to send image "name" to server, so it can identify and send image to client
 dout.writeUTF(obj.getImg_name());
                BufferedImage img = ImageIO.read(ImageIO.createImageInputStream(socket.getInputStream()));
                System.out.println("Image received!!!!");
    //                socket.close();
            } catch (IOException ex) {
                System.out.println("Error al abrir el socket" + ex);
            }
        }
    }


推荐答案

发送图像后没有关闭套接字,因此客户端永远不会停止读取。服务器应该关闭套接字输出流,并在finally块中关闭套接字本身。

You aren't closing the socket after you send the image, so the client never stops reading. The server should close the socket output stream, and close the socket itself in a finally block.

客户端也应该在finally块中关闭他的套接字。

The client should also close his socket, in a finally block.

编辑:您不需要使用ImageIO在服务器上读取和写入图像文件,除非您更改格式。只需复制字节。

You don't need to use ImageIO to read and write the image files at the server, unless you are, say, changing the format. Just copy the bytes.

这篇关于通过套接字发送图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆