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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T22:14:31+00:00 2026-05-19T22:14:31+00:00

I am engaging the following problems: I need to write a server program, will

  • 0

I am engaging the following problems:

  1. I need to write a server program, will accept multiple clients
  2. all clients are subscribing the same data from server, for example the stock price update.
  3. each clients can send simple commands to server like “logon”, “stop”

So here is my solution, Since I am not very experianced in multithread/tcp, I want to know is it a good solution? if not, is there any better solution? is it necessary to have a thread for each client socket? Thanks
BTW: sorry for confusing every one, it is a small project that only involve 5-10 classes.

class AcceptThread {
    ......
    public void run () {
        ControlThread controlThread = new ControlThread();
        controlThread.start();

        Socket socket = new Socket(port);
        while (!stop) {
            Socket s = socket.accept();
            controlThread.addClient (s);
        }
    }
}

class ControlThread {
    Set<Scoket> clients;
    SendDataThread sendDataThread;  

    public ControlThread () {
        sendDataThread = new SendDataThread();
        sendDataThread.start();     
    }

    public void addClient (Socket socket) {
        clients.add(socket);
        sendDataThread.addListener(socket);
    }

    public void run () {
        ......
        for (Socket s : clients) {
            if (s.getInputStream().available()) {
                //read command from s
            }
        }
        ......              
    }
}

class SendDataThread () {
    Set<Scoket> listeners;

    public void addListener (Socket s) {
        listeners.add(s);
    }

    public void run () {
        for (Socket s: listeners) {
            // send data to each listener
        }
    }
}
  • 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-19T22:14:32+00:00Added an answer on May 19, 2026 at 10:14 pm

    is it necessary to have a thread for each client socket?

    No, as a matter of fact, I wouldn’t even not recommend it. If it’s a small project and you don’t want to use any existing library, I would suggest you use the java.nio package and the SelectableChannels. With a so called selector you can easily monitor clients for incoming data in a non-blocking way.

    Here are a few useful links:

    • NIO Examples (from the official tutorial)
    • The Rox Java NIO Tutorial
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In the next few weeks, my company will be engaging multiple vendors to establish
I'm currently re-engaging with Python after a long absence and loving it. However, I
JavaFX is now out, and there are promises that Swing will improve along with
Is there a way to assign a binary value to a VB variable? All
the company I work for has a web-based user interface which paying clients log
I know this isn't a good thing to do, but its also a temporary
One of the best classes I took in college was Programming Languages where the
I have an asmx web service that should only be allowed to respond to
I have for the last several years been struggling to understand why the Internet
I'm developing a web app. This is more of a line-of-business app rather than

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.