title: Transfer image file through Socket Programming in Java
body: <p>In my code, client sent an image to server and server should be able to receive it and show the image name (<code>HelloNewPic.jpg</code> in here) and also save image in desktop.</p>
  
  <p>Server:</p>
  
  <pre><code>public class Server1 {
  
  ServerSocket serverSocket = null;
  Socket socket = null;
  DataInputStream dataInputStream = null;
  
  public Server1() {
      try {
          serverSocket = new ServerSocket(6002);
          System.out.println("Server is Waiting for request...");
          socket = serverSocket.accept();
          System.out.println("Connected with: " + socket.getInetAddress());
          dataInputStream = new DataInputStream(socket.getInputStream());
          System.out.println("Server Read from client: " + dataInputStream.readUTF());
          BufferedImage image = ImageIO.read(socket.getInputStream());
          System.out.println("Server: Image received from client.");
  
      } catch (Exception e) {
          e.printStackTrace();
      }
  }
  
  public static void main(String[] args) {
      new Server1();
  }
  }
  </code></pre>
  
  <p>Client:</p>
  
  <pre><code>public class Client1 {
  
  Socket clientSocket = null;
  OutputStream outputStream = null;
  DataOutputStream dataOutputStream = null;
  InputStream inputStream = null;
  
  public Client1() {
      try {
          clientSocket = new Socket("localhost", 6002);
          outputStream = clientSocket.getOutputStream();
          dataOutputStream = new DataOutputStream(outputStream);
          inputStream = clientSocket.getInputStream();
  
          dataOutputStream.writeUTF("Hello");
          System.out.println("Client to server says: Hello");
          BufferedImage image = ImageIO.read(new File("HelloNewPic.jpg"));
          ImageIO.write(image, "JPG", clientSocket.getOutputStream());
          System.out.println("Client: Image sent to server");
  
      } catch (Exception e) {
          e.printStackTrace();
      }
  }
  
  public static void main(String[] args) {
      new Client1();
  }
  }
  </code></pre>
  
  <p>In client side, There is no exception:</p>
  
  <pre><code>Client to server says: Hello
  Client: Image sent to server
  </code></pre>
  
  <p>But exception occur on server side:</p>
  
  <pre><code>Server is Waiting for request...
  Connected with: /127.0.0.1
  Server Read from client: Hello
  java.net.SocketException: Connection reset
      at java.net.SocketInputStream.read(SocketInputStream.java:189)
      at java.net.SocketInputStream.read(SocketInputStream.java:121)
      at javax.imageio.stream.FileCacheImageInputStream.readUntil(FileCacheImageInputStream.java:141)
      at javax.imageio.stream.FileCacheImageInputStream.read(FileCacheImageInputStream.java:187)
      at com.sun.imageio.plugins.jpeg.JPEGImageReader.readImage(Native Method)
      at com.sun.imageio.plugins.jpeg.JPEGImageReader.readInternal(JPEGImageReader.java:1158)
      at com.sun.imageio.plugins.jpeg.JPEGImageReader.read(JPEGImageReader.java:966)
      at javax.imageio.ImageIO.read(ImageIO.java:1448)
      at javax.imageio.ImageIO.read(ImageIO.java:1352)
      at networking.Server1.&lt;init&gt;(Server1.java:23)
      at networking.Server1.main(Server1.java:32)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:601)
      at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
  </code></pre>