title: Sending images from client to server with sockets
body: <p>I want to send images from a client to a server in java. I found one way to do this is with sockets. My code is as follows:</p>
  
  <p>Server side: thread waiting for client sockets and receiving the image when a client connects:</p>
  
  <pre><code>@Override
  public void run() {
      while (running) {   
          try(Socket clientSocket = serverSocket.accept();
              PrintWriter outText = new PrintWriter(clientSocket.getOutputStream(), true);
              BufferedReader intText = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
          ) {
  
              try {
                  // Get image info
                  String imageInfo = inText.readLine();
                  BufferedImage image = ImageIO.read(ImageIO.createImageInputStream(clientSocket.getInputStream()));
  
                  // Handle input
                  String[] split = imageInfo.split(";");
                  String typeStr = split[0];
                  String name = split[1];
                  ServerImageType type = ServerImageType.fromInt(Integer.valueOf(typeStr));
  
                  // Save image
                  SaveImageThread saveImageThread = new SaveImageThread(image, name, type);
                  saveImageThread.start();
  
                  // Respond
                  outText.println(name + " OK");
  
              } catch (IOException e) {
                  System.err.out("The server is in pain: " + e);
              } 
          }
      }
  
      try {
          serverSocket.close();
      } catch (Exception e) {
          e.printStackTrace();
      }
  }
  </code></pre>
  
  <p>Client side:</p>
  
  <pre><code>public void sendImage(BufferedImage image, String name, ServerImageType imageType) throws IOException {
      try(Socket clientSocket = new Socket(serverName, txPort);
             PrintWriter outText = new PrintWriter(clientSocket.getOutputStream(), true);
             BufferedReader inText = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()))
      ) {
          // Send image info
          String imageInfo = imageType.getId() + ";" + name;
          outText.println(imageInfo);
  
          // Send image
          ImageIO.write(image, "JPG", clientSocket.getOutputStream());
  
          // Receive acknowledgement
          String input = inText.readLine();
          System.out.println("Server answered: " + input);
      }
  }
  </code></pre>
  
  <p>Problem: the server gets stuck on the <em>ImageIO.read(...</em> line.. Any ideas?</p>