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 7754105
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T12:13:28+00:00 2026-06-01T12:13:28+00:00

I have developed a java swing client-server application. The server has many services like

  • 0

I have developed a java swing client-server application. The server has many services like database service, cache service and client service talks to the clients.

The client service opens a socket on a port and listens to incoming connections. It spawns a new thread for every client connection, creates a session and reads the incoming serialized object. It maintains this session (keeps the thread alive) till the client issues a ‘CLOSE_SESSION’ command.

What i would like to know is if its correct to spawn a new thread for every new client-socket session. Thanks.

My client service code is as below.

Code to create server socket:

            try {
                ServerSocket socket = new ServerSocket(serverPort);
                Socket listener = socket.accept();

                Thread client = new Thread(new ClientHandler(listener));
                client.start();

            } catch (IOException ex) {
                log.error(new Throwable(ex));
            }

Code to spawn new thread for every client

class ClientHandler implements Runnable {

private static Logger log = Logger.getLogger(ClientHandler.class);

private Socket listener;

public ClientHandler(Socket listener) {
    this.listener = listener;
}

public void run() {
    try {
        ObjectInputStream inStream = new ObjectInputStream(
                listener.getInputStream());
        try {
            ServiceRequestResponse request = (ServiceRequestResponse) inStream
                    .readObject();
            if (request != null && request.getServiceCommand() != null) {

                ServiceCommand command = request.getServiceCommand();
                log.debug("command : " + command.getCommand());
                log.debug("is session alive? " + request.isAlive());
                log.debug("ServiceCommand.CREATE_SESSION : "
                        + ServiceCommand.CREATE_SESSION.getCommand());
                if (!request.isAlive()
                        && command.getCommand().equals(
                                ServiceCommand.CREATE_SESSION.getCommand())) {
                    // No session yet, and service command issued is login.
                    // Call login service, check credentials and create
                    // session.
                    request.setSessionId(UUID.randomUUID());
                    log.debug("Created user session with id : "
                            + request.getSessionId());
                } else {
                    if (command.getCommand().equals(
                            ServiceCommand.CLOSE_SESSION)) {
                        // Close session and do clean up here
                    }
                    // Here session is alive.
                    while (!ServiceCommand.CLOSE_SESSION.equals(command
                            .getCommand())) {
                        // Read the service command from the request
                        // response and
                        // Hand it over to the appropriate handler.
                    }

                }

            }

        } catch (ClassNotFoundException ex) {
            log.error(new Throwable(ex));
        }

    } catch (IOException ex) {

    }

}

}

  • 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-01T12:13:29+00:00Added an answer on June 1, 2026 at 12:13 pm

    If your client session request can last long then thread-per-connection is a good solution.

    Alternatives are:

    • Using NIO;
    • Using thread pool if client requests are short.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Java Swing application, developed on Mac OS X 10.5 using Java
I have developed a basic Chat application in Java. It consists of a server
I have developed a Java Swing application, which uses the SwingWorker class to perform
I have to design a distributed application composed by one server (developed in Java)
I have developed a Java Swing application, and it will be installed on multiple
I have a stand-alone Java windows application developed based on Swing. It connects to
I have developed a Java swing application, then I have deployed it through a
I have already developed a JAVA SWING Application . I want to install this
We have developed SOA architecure for our intranet application, so our web-start java applications
I've been doing quite large application recently with Java - Swing. Now I'd like

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.