title: Read Image from Socket
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/8679112/read-image-file-through-java-socket">Read Image File Through Java Socket</a>  </p>
  </blockquote>
  
  
  
  <pre><code>void readImage() throws IOException
  {
      socket = new Socket("upload.wikimedia.org", 80);
  
  
      DataOutputStream bw = new DataOutputStream(new DataOutputStream(socket.getOutputStream()));
      bw.writeBytes("GET /wikipedia/commons/8/80/Knut_IMG_8095.jpg HTTP/1.1\n");
      bw.writeBytes("Host: wlab.cs.bilkent.edu.tr:80\n\n");
  
      DataInputStream in = new DataInputStream(socket.getInputStream());
  
      File file = new File("imgg.jpg");
      file.createNewFile();
      DataOutputStream dos = new DataOutputStream(new FileOutputStream(file));
      int count;
      byte[] buffer = new byte[8192];
      while ((count = in.read(buffer)) &gt; 0)
      {
        dos.write(buffer, 0, count);
        dos.flush();
      }
      dos.close();
      System.out.println("image transfer done");
  
      socket.close();     
  }
  </code></pre>
  
  <p>-Create a socket
  -Create output stream
  -Request the page that includes image
  -Read socket to an input stream
  -Write to file</p>
  
  <p>I am trying to read an image from socket.
  But it is not working.</p>
  
  <p>It seems to read and the image is opened but can not be seen</p>
  
  <p>Where is the problem?</p>