title: Java, FTP server and client
body: <p>I am trying to "Write two Java programs implementing the FTP server with responses to USER and PASS, with thread-per-client and thread pool, respectively." I am just trying to make sure I have everything in order before turning it in. Here is my source code. The only trouble I am having is figuring out what to do with "FTPProtocol client;" Should I just destroy it after I have the thread.start?</p>
  
  <p>FTPServer.java</p>
  
  <pre><code>import java.io.IOException;
  import java.net.ServerSocket;
  import java.net.Socket;
  
  
   public class FTPserver
  {   
  public static void main(String [] args)
  {
      if (args.length != 1) 
          throw new IllegalArgumentException( "Parameter(s): &lt;Port&gt;");
  
      int threadPoolSize = 10;
      int port = Integer.parseInt(args[0]); 
  
      final ServerSocket server;
      try 
      {
          server = new ServerSocket(port);
      } 
      catch (IOException e1) 
      {
          return;
      }
  
      for (int i = 0; i &lt; threadPoolSize; i++) 
      {
          Thread thread = new Thread() 
          {
              public void run() 
              {
                  while (true) 
                  {
                      try 
                      {
                          Socket sock = server.accept();
                          FTPProtocol client = new FTPProtocol(sock);
                      } 
                      catch (IOException e)
                      {
                          System.err.println(e.getMessage());
                          return;
                      }
                  }
              }
          };
          thread.start();
      }
  }
  }
  </code></pre>
  
  <p>FTPProtocol.java</p>
  
  <pre><code>import java.io.IOException;
  import java.io.InputStream;
  import java.io.OutputStream;
  import java.net.Socket;
  
  class FTPProtocol implements Runnable
  {
  static String greeting = "220 Service Ready.\r\n";
  static String needPassword = "331 User name ok, need password.\r\n";
  static String closing = "421 Service not available, closing control connection.\r\n";
  static byte[] reply220 = null;
  static byte[] reply331 = null;
  static byte[] reply421 = null;
  
      Socket sock = null;
      public FTPProtocol(Socket so)
      {
          sock = so; 
          reply220 = greeting.getBytes();
          reply331 = needPassword.getBytes();
          reply421 = closing.getBytes();
      }
  
      public void run()
      { 
          handleFTPClient(sock); 
      }
  
      void handleFTPClient(Socket sock)
      {
          InputStream is = null;
          OutputStream os = null;
          byte[] inBuffer = new byte[1024];
  
          try 
          {
              is = sock.getInputStream();
              os = sock.getOutputStream();
              os.write(reply220);
              int len = is.read(inBuffer);
              System.out.write(inBuffer, 0, len);
              os.write(reply331);
              len = is.read(inBuffer);
              System.out.write(inBuffer, 0, len);
              os.write(reply421);
              sock.close();
          } 
          catch (IOException e)
          {
              System.err.println(e.getMessage());
              return;
          }
      }
  }
  </code></pre>