title: How can I transfer files from Server-Side to Client using Sockets?
body: <p>I'm having trouble on where to begin performing this task, I'd like some examples or input on how I should set up my server/client components to receive and send data including letting the client download images. </p>
  
  <p>Here's my client-side code:</p>
  
  <pre><code>package V3;
  
  import java.io.*;
  import java.net.*;
  
  public class Version3Client {
      public static void main(String[] args) throws IOException {
  
          Socket kkSocket = null;
          PrintWriter out = null;
          BufferedReader in = null;
  
          try {
              kkSocket = new Socket("localhost", 4444);
              out = new PrintWriter(kkSocket.getOutputStream(), true);
              in = new BufferedReader(new InputStreamReader(kkSocket.getInputStream()));
          } catch (UnknownHostException e) {
              System.err.println("Don't know about host: taranis.");
              System.exit(1);
          } catch (IOException e) {
              System.err.println("Couldn't get I/O for the connection to: taranis.");
              System.exit(1);
          }
  
          BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
          String fromServer;
          String fromUser;
  
          while ((fromServer = in.readLine()) != null) {
              System.out.println("Server: " + fromServer);
              if (fromServer.equals("Bye."))
                  break;
  
              fromUser = stdIn.readLine();
          if (fromUser != null) {
                  System.out.println("Client: " + fromUser);
                  out.println(fromUser);
          }
          }
  
          out.close();
          in.close();
          stdIn.close();
          kkSocket.close();
      }
  }
  </code></pre>
  
  <p>And here's my server-side code:</p>
  
  <pre><code>package V3;
  
  import java.net.*;
  import java.io.*;
  
  public class Version3Server {
      public static void main(String[] args) throws IOException {
  
          ServerSocket serverSocket = null;
          boolean listening = true;
  
          try {
              serverSocket = new ServerSocket(4444);
          } catch (IOException e) {
              System.err.println("Could not listen on port: 4444.");
              System.exit(-1);
          }
  
          while (listening)
          new Version3ServerThread(serverSocket.accept()).start();
  
          serverSocket.close();
  
          Socket clientSocket = null;
          try {
              clientSocket = serverSocket.accept();
          } catch (IOException e) {
              System.err.println("Accept failed.");
              System.exit(1);
          }
  
          PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
          BufferedReader in = new BufferedReader(
                  new InputStreamReader(
                  clientSocket.getInputStream()));
          String inputLine, outputLine;
          Version3Protocol kkp = new Version3Protocol();
  
          outputLine = kkp.processInput(null);
          out.println(outputLine);
  
          while ((inputLine = in.readLine()) != null) {
               outputLine = kkp.processInput(inputLine);
               out.println(outputLine);
               if (outputLine.equals("Bye."))
                  break;
          }
          out.close();
          in.close();
          clientSocket.close();
          serverSocket.close();
      }
  }
  </code></pre>
  
  <p>Thread class added for further specification:</p>
  
  <pre><code>package V3;
      import java.net.*;
      import java.io.*;
      import java.io.FileWriter;
  
      public class Version3ServerThread extends Thread {
          private Socket socket = null;
  
          public Version3ServerThread(Socket socket) {
          super("Version3ServerThread");
          this.socket = socket;
          }
  
          public void run() {
  
          try {
              PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
              BufferedReader in = new BufferedReader(
                          new InputStreamReader(
                          socket.getInputStream()));
  
              String inputLine, outputLine;
              Version3Protocol kkp = new Version3Protocol();
              outputLine = kkp.processInput(null);
              out.println(outputLine);
  
              while ((inputLine = in.readLine()) != null) {
              outputLine = kkp.processInput(inputLine);
              out.println(outputLine);
              if (outputLine.equals("Bye"))
                  break;
                      if(kkp.getInteraction()){
                        Logging.writeToFile(socket.getInetAddress());
                      }
              }
              out.close();
              in.close();
              socket.close();
  
          } catch (IOException e) {
              e.printStackTrace();
          }
          }
      }
  </code></pre>