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

  • SEARCH
  • Home
  • 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 6734997
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T10:57:21+00:00 2026-05-26T10:57:21+00:00

I am writing a server like below public class Server<T extends RequestHandler> { public

  • 0

I am writing a server like below

public class Server<T extends RequestHandler> {

    public void start() {

        try{
            this.serverSocket = new ServerSocket(this.port, this.backLog);
        } catch (IOException e) {
            LOGGER.error("Could not listen on port " + this.port, e);
            System.exit(-1);
        }

        while (!stopTheServer) {
            socket = null;
            try {
                socket = serverSocket.accept();
                handleNewConnectionRequest(socket);
            } catch (IOException e) {
                LOGGER.warn("Accept failed at: " + this.port, e);
                e.printStackTrace();
            }
        }

    }

    protected void handleNewConnectionRequest(Socket socket) {
        try {
            executorService.submit(new T(socket));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

But in the handleNewConnectionRequest(...) method, I can’t create an instance of T as it is actually not a class. Also I can’t use the method mentioned here as I want to pass the socket instance so that the request handler can get OutputStream and InputStream on the socket.


Can’t I make a generic server like above and have different protocol handlers e.g

public class HttpRequestHandler extends RequestHandler {
    ...
}

public class FtpRequestHandler extends RequestHandler {
    ...
}

public class SmtpRequestHandler extends RequestHandler {
    ...
}

and then use them like below

Server<HttpRequestHandler> httpServer = new Server<HttpRequestHandler>();
Server<FtpRequestHandler> ftpServer = new Server<FtpRequestHandler >();
Server<SmtpRequestHandler> smtpServer = new Server<SmtpRequestHandler >();
  • 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-05-26T10:57:21+00:00Added an answer on May 26, 2026 at 10:57 am

    Maybe make different Server subclasses befitting various handler types. One example:

    public class HttpServer extends Server<HttpRequestHandler> {
    
        protected HttpRequestHandler wrapSocket(Socket socket) {
            return new HttpRequestHandler(socket);
        }
    
    }
    

    And adapt Server like so:

    public abstract class Server<T extends RequestHandler> {
    
        protected abstract T wrapSocket(Socket socket);
    
        protected void handleNewConnectionRequest(Socket socket) {
            try {
                executorService.submit(wrapSocket(socket));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
    }
    

    Just a thought…

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

Sidebar

Related Questions

I'm writing script in remote.ini The script looks like on 1:start:{ server some.irc.server server
I would like to use libev for a streaming server I am writing. This
I'm writing multilingual website. I have several files on server like: /index.php /files.php /funny.php
I'm writing some software that modifies a Windows Server's configuration (things like MS-DNS, IIS,
I'm writing myself a small server daemon in C, and the basic parts like
I'm writing a database application that connects to SQL server. I'd like to implement
I`m writing client-server app for windows using WinSock and I have class for server.
if writing below codes: Error returns.i do like advise : http://stackoverflow.com/questions/794364/how-do-i-use-transactionscope-in-c But only error
When writing server-side code you need to explicitly stop execution after sending a Location:
I'm writing server-side programs in PHP for an iPhone app. And I have no

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.