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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T11:14:44+00:00 2026-05-30T11:14:44+00:00

For educational purposes I tried to make a server and client where the server

  • 0

For educational purposes I tried to make a server and client where the server receives data from multiple clients and echoes each message. The problem is when I try to make the server send the echo to all of the clients at once.

public class SocketServer {
  ArrayList<MyRunnable> ts = new ArrayList<MyRunnable>();
  ServerSocket serv;
  static MainServerThread mst = new MainServerThread();
//                                  ^ IDE(eclipse) underlines this as the problem
  SocketServer() {
    EventQueue.invokeLater(mst);
  }

  public static void main(String[] args) { 
    Thread tr = new Thread(mst);
    tr.start();
  }

  void send(String s) {
    for (int i = 0; i < ts.size(); i++) {
      MyRunnable tmp = ts.get(i);
      tmp.sendToClient(s);
    }
  }

  class MainServerThread implements Runnable {
    public void run() {
      try {
        serv = new ServerSocket(13131);
        boolean done = false;
        while (!done) {
          Socket s = serv.accept();
          MyRunnable r = new MyRunnable(s);
          Thread t = new Thread(r);
          ts.add(r);
          t.start();
        }
      } catch(Exception e) {
        e.printStackTrace();
      }
    }
  }

  class MyRunnable implements Runnable {
    Socket sock;
    PrintWriter out;
    Scanner in;
    MyRunnable(Socket soc) {
      sock = soc;
    }

    public void run() {
      try {
        try {
          out = new PrintWriter(sock.getOutputStream(), true);
          in = new Scanner(sock.getInputStream());
          boolean done = false;
          while (!done) {
            String line = in.nextLine();
            send("Echo: " + line);
            System.out.println("Echo: " + line);
            if (line.trim().equals("BYE")) done = true;
          }
        } finally {
          sock.close();
        }
      } catch (Exception e) {
        e.printStackTrace();
      }
    }

    public void sendToClient(String s) {
      out.println(s);
    }
  }
}    

I have searched for and answer and saw many similar questions, but none of them helped me.
Hope you can point out my mistake. Thanks in advance.

  • 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-30T11:14:46+00:00Added an answer on May 30, 2026 at 11:14 am

    Your nested class requires an instance of the outer class, because it’s not static – but you don’t have an instance of the outer class.

    Try making both of your nested classes static. It doesn’t look like they need a reference to the outer class anyway.

    In fact, I’d be tempted to avoid using nested classes at all for this – while nested classes can certainly be useful sometimes, they have various corner cases, and it’s typically cleaner to create separate top-level types.

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

Sidebar

Related Questions

For educational purposes I tried to convert the following Linq expression from the book
In educational purposes I'm writing a HTTP server in C++. When receiving a request,
I am writing a super tiny web server for educational purposes. For the following
My app is phonebook(educational purposes). When user opens application, QTableWidged is filled with data
I've been trying to implement a web server gateway (for fun and educational purposes)
I'm writing some wxWidgets samples for educational purposes. My problem is simple: I'm using
Any ideas where can I find a free (trial or for educational purposes) WebSphere
I am asking this question from an educational/hacking point of view, (I wouldn't really
I'm trying to write my own C++ String class for educational and need purposes.
For educational purposes I have set up a project layout like so (flat in

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.