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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T15:39:14+00:00 2026-05-13T15:39:14+00:00

I’ve followed a tutorial to create the socket server below which allows multiple users

  • 0

I’ve followed a tutorial to create the socket server below which allows multiple users to connect and if they send some data to it then it returns it back to them.

   import java.awt.Color;
    import java.awt.BorderLayout;
    import java.awt.event.*;
    import javax.swing.*;

    import java.io.*;
    import java.net.*;

    class ClientWorker implements Runnable {
      private Socket client;
      private JTextArea textArea;

      ClientWorker(Socket client, JTextArea textArea) {
       this.client = client;
       this.textArea = textArea;   
      }

      public void run(){
        String line;
        BufferedReader in = null;
        PrintWriter out = null;
        try{
          in = new BufferedReader(new InputStreamReader(client.getInputStream()));
          out = new PrintWriter(client.getOutputStream(), true);
        } catch (IOException e) {
          System.out.println("in or out failed");
          System.exit(-1);
        }

        while(true){
          try{
            line = in.readLine();
    //Send data back to client
             out.println(line);
             textArea.append(line);
           } catch (IOException e) {
             System.out.println("Read failed");
             System.exit(-1);
           }
        }
      }
    }



----------


    class SocketThrdServer extends JFrame{

       JLabel label = new JLabel("Text received over socket:");
       JPanel panel;
       JTextArea textArea = new JTextArea();
       ServerSocket server = null;

       SocketThrdServer(){ //Begin Constructor
         panel = new JPanel();
         panel.setLayout(new BorderLayout());
         panel.setBackground(Color.white);
         getContentPane().add(panel);
         panel.add("North", label);
         panel.add("Center", textArea);
       } //End Constructor

      public void listenSocket(){
        try{
          server = new ServerSocket(4444); 
        } catch (IOException e) {
          System.out.println("Could not listen on port 4444");
          System.exit(-1);
        }
        while(true){
          ClientWorker w;
          try{
            w = new ClientWorker(server.accept(), textArea);
            Thread t = new Thread(w);
            t.start();
          } catch (IOException e) {
            System.out.println("Accept failed: 4444");
            System.exit(-1);
          }
        }
      }

      protected void finalize(){
    //Objects created in run method are finalized when 
    //program terminates and thread exits
         try{
            server.close();
        } catch (IOException e) {
            System.out.println("Could not close socket");
            System.exit(-1);
        }
      }

      public static void main(String[] args){
            SocketThrdServer frame = new SocketThrdServer();
        frame.setTitle("Server Program");
            WindowListener l = new WindowAdapter() {
                    public void windowClosing(WindowEvent e) {
                            System.exit(0);
                    }
            };
            frame.addWindowListener(l);
            frame.pack();
            frame.setVisible(true);
            frame.listenSocket();
      }
    }

However I need it so if data is sent to the server rather than simply returning it, it sends it to everyone who is connected to the server. If someone helps me sort this out then ill be forever thankful! It means ill be able to finish my School project!

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-13T15:39:14+00:00Added an answer on May 13, 2026 at 3:39 pm

    This is your problem:

    //Send data back to client
    out.println(line);
    

    As the others have said, use a List to keep track of all your clients.
    Specifically, you want a List of ClientWorkers.

    I use a simple Singleton class that keeps the List. This is so that when a new client connects it will add itself to the list and the other clients don’t need to do anything to see that there is a new client. And add a method like writeString(String) to the ClientWorker class to make it easier to write something out to each client.

    You should have a really basic loop like this in ClientWorker:

    String line = in.readLine();
    LinkedList<ClientWorkers> clients = SingletonClients.getClients();
    for(int = 0; i < clients.size(); i++) {
        ClientWorker c = clients.get(i);
        //The client doesn't need to get it's own data back.
        if(c == this)
            continue;
    
        c.writeString(line);
    }
    

    writeString will just be:

    public void writeString(String s) {
        try {
            out.println(s);
        } catch(IOException ex) {
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 384k
  • Answers 384k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Look at the ZF ldap API, might help you. May 14, 2026 at 11:13 pm
  • Editorial Team
    Editorial Team added an answer Doesn't seem that way: MDC Reference Introduced in Gecko 1.9.2… May 14, 2026 at 11:13 pm
  • Editorial Team
    Editorial Team added an answer Your WAR is also including either org.xml.sax or org.w3c.dom classes,… May 14, 2026 at 11:13 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.