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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T21:56:26+00:00 2026-06-03T21:56:26+00:00

I first asked this question, which I figured out how the EDT works and

  • 0

I first asked this question, which I figured out how the EDT works and started reading more on swing and worker threads by reading this. I started to get an understanding of how they work and got my code fixed to where it would run. Now I’m trying to get the information from my worker thread (the server) to update my GUI. I run into a problem though I can’t seem to work my way around. The problem is I need to keep listening for new client (As the server is suppose to handle multiple clients) but because that is in a while loop I never hit the return of my worker thread. I can’t see any other way to set it up either. Could someone take a look at my code and suggest a way I might be able to get this to work?

Main.class

package com.sever.core;

import java.io.IOException;
import java.net.Socket;
import java.util.Scanner;
import java.util.concurrent.ExecutionException;

import javax.swing.SwingUtilities;
import javax.swing.SwingWorker;

public class Main { 

private SocketManager network;
private Window window;

public static void main(String[] args){         
    Main main = new Main();
    main.runGUI();
    main.runServer();
}

private void runGUI(){

    //Runs the swing components in the EDT.
    SwingUtilities.invokeLater(new Runnable(){
        @Override
        public void run() {
            window = new Window();
            window.setVisible(true);
        }       
    });
}

private void runServer(){

    //Runs the Server process on a worker thread.
    SwingWorker<String, String> server = new SwingWorker(){
        @Override
        protected Object doInBackground() throws Exception {    
            network = new SocketManager(25595);
            /*
             * Here is the problem. I need to keep running this code so,
             * that I can let multiple clients connect. However,
             * it then never reaches the return.
             */
            while(true){
                try {   
                    network.setSocket(network.getServerSocket().accept());
                    addUser(network.getSocket());
                } catch (Exception e) {
                    System.out.println("Failed to connect.");
                }
            }
            return network.getMessage(); 
        }

        @Override
        protected void done(){
            try {
                window.updateChat(get().toString());
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (ExecutionException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

    };
    server.run();
}

private void addUser(Socket s){
    try {
        Scanner input = new Scanner(s.getInputStream());
        network.addUser(input.nextLine());
    } catch (Exception e) {

    }
}
}
  • 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-03T21:56:28+00:00Added an answer on June 3, 2026 at 9:56 pm

    From Java Tutorials

    Server

    public class Server {
    
        public static void main(String[] args) {
            ServerSocket server = new ServerSocket(4444);
    
            while(true) {
                new ServerThread(server.accept()).start();
            }
        }
    }
    

    Server Thread

    public class ServerThread implements Runnable {
    
        private InputStream in;
        private OutputStream out;
    
        public ServerThread(Socket client) {
            in = client.getInputStream();
            out = client.getOutputStream();
        }
    
        public void run() {
            // do your socket things 
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Note: A have asked this question at the Superuser community first , but since
I asked this question before, but had a hard time showing which edits I
First, this question may have been asked before, but I'm not sure what phrase
I know this question has been asked countless times before, but the other threads
I originally asked this question on programmers.stackexchange.com, because I figured the answers would be
i think this question was asked few times, which I have went through and
First of all, I know I asked a similar question before but this one
First of all, sorry if this has been asked before. I've done a pretty
First off, I apologize if this has been asked before. I can't seem to
First up, my question is very similar to questions asked in Stackoverflow (and the

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.