Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7965033
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T06:03:04+00:00 2026-06-04T06:03:04+00:00

I am trying to Write two Java programs implementing the FTP server with responses

  • 0

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?

FTPServer.java

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): <Port>");

    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 < 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();
    }
}
}

FTPProtocol.java

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;
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T06:03:05+00:00Added an answer on June 4, 2026 at 6:03 am

    Am I missing something here? You are creating an on object whose type implements Runnable (for no apparent reason):

    FTPProtocol client = new FTPProtocol(sock);
    

    and then you are not doing anything with it.

    Did you mean to start a thread with that Runnable? Or pass it to a thread pool:

    ExecutorService exec = Executors.newFixedThreadPool(threadPoolSize);
    
    while (true) {
       try {
          Socket sock = server.accept();
          exec.submit(new FTPProtocol(sock));
       } catch (IOException e) {
          System.err.println(e.getMessage());
          return;
       }
    }
    

    This will ensure that you are not starting more threads than threadPoolSize and that the appropriate run method of FTPProtocol is called for each object.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a simple server in c that plays a two player
I am trying to write a Java applet that will read from a user's
I'm trying to write a program which prompts the user to enter two 3×3
I'm trying to write a method to compare two arrays in Java without using
I'm trying to write two batch files that will allow me to switch the
I'm trying to write two applications (iphone and desktop) to achieve what's been described
I'm trying to write unit tests in MSTest and I've created two TestClasses. When
I'm trying to write an application (winforms) that can demonstrate how two oscillating colors
Trying to write a chat, like on facebook, I wondered if two clients can
I'm trying to write a single query which will include one of the two

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.