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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T09:18:09+00:00 2026-06-12T09:18:09+00:00

I’ve got a program I’m working on where the main thread initializes a listener

  • 0

I’ve got a program I’m working on where the main thread initializes a listener thread to listen for incoming connections to a serverSocket

    /*
     * run - this will set up the server so that it is ready to listen.
     */
    public void run( serverVariables servVar, storageManager writer, storageVariables write, CoreVariables coreVar, serverFunctions serv, SQLFunctions SQL, SQLvariables SQLvar, netFunctions conn, netVariables netVar) {
        ControlFunctions cntrl = new ControlFunctions();
        util.doCommand("ifconfig eth0 " + servVar.ip);
        ListenerThread listen = new ListenerThread(servVar, coreVar, writer, serv, write, SQL, SQLvar, util);
        try {
            servVar.servSocket = new ServerSocket(servVar.port);
            listen.start();
            cntrl.getInput(util, clr.YELLOW, SQL, SQLvar, listen, conn, netVar);
        } catch (IOException e) {
            System.out.println("Could not read input stream");
            e.printStackTrace();
        }
    }

then waits for input from the server admin (in cntrl.getInput()). That’s all well and good, but the main function I’m trying to implement from the getInput is “halt” which should check that there are no connections, and then stop the listener and itself.

Here’s the listener thread:

   package server;

import java.io.IOException;

import net.netFunctions;
import net.netVariables;
import core.CoreVariables;
import utl.utilFunctions;
import utl.color;
import server.serverVariables;
import store.storageManager;
import store.storageVariables;
import conn.SQLFunctions;
import conn.SQLvariables;

public class ListenerThread extends Thread {
    color clr = new color();
    CoreVariables coreVar = new CoreVariables();
    utilFunctions util = new utilFunctions();
    netVariables netVar = new netVariables();
    storageManager writer = new storageManager();
    netFunctions conn = new netFunctions();
    storageVariables write = new storageVariables();
    serverFunctions serv = new serverFunctions();
    serverVariables servVar = new serverVariables();
    SQLFunctions SQL = new SQLFunctions();
    SQLvariables SQLvar = new SQLvariables();
    message msg = new message();

    public ListenerThread(serverVariables servVar, CoreVariables coreVar,
            storageManager writer, serverFunctions serv,
            storageVariables write, SQLFunctions SQL, SQLvariables SQLvar,
            utilFunctions util) {
        super("ListenerThread");
        this.coreVar = coreVar;
        this.util = util;
        this.writer = writer;
        this.write = write;
        this.serv = serv;
        this.servVar = servVar;
        this.SQL = SQL;
        this.SQLvar = SQLvar;
    }

    public void run() {
        util.outColored("Listener thread started", clr.CYAN);
        while (true) {
            try {
                new ConnectionThread(clr.GREEN, servVar.servSocket.accept(),
                        coreVar, util, writer, write, serv, servVar, SQL,
                        SQLvar).start();
            } catch (IOException e) {
                System.out.println("Failed to accept");
            }
        }
    }
}

and the haltServer function:

private boolean haltServer(SQLFunctions SQL, SQLvariables SQLvar, utilFunctions util, String color, ListenerThread listen, netFunctions conn, netVariables netVar) {
    Socket s = null;
    boolean success = false;
    String result = null;
    int count = 0;
    //check for open connections
    SQL.doQuery("SELECT * FROM Clients WHERE Status != 0", SQLvar);
    try {
        while(SQLvar.resultSet.next()) {
            result = SQLvar.resultSet.getString("ClientID");
            util.outColored("Client " + result + " is still connected", color);
            count++;
        }
        if(count == 0) {
            listen.stop();
            success = true;
        }
        } catch (UnknownHostException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return success;
}

I’m aware that stop is depreciated, and looked in to using interupted() as a flag, but I don’t think that will work due to the serverSocket.accept() call.

Ideas?

  • 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-12T09:18:09+00:00Added an answer on June 12, 2026 at 9:18 am

    Use a boolean flag to represent whether it should break or not:

    private volatile boolean stop = false;
    

    Then make a method called something like kill (can’t be stop) which sets the flag to true, then closes your ServerSocket:

    public voic kill() {
        stop = true;
        serverSocket.close();
    }
    

    Then, almost done here, in the line of code which accepts your ServerSocket into a Socket, in the catch block, have it like this:

    try {
       //accept server socket
    } catch(IOException e) {
        if(stop)
            return;
        e.printStackTrace();
    }
    

    So it won’t print an error when you close the ServerSocket, but rather simply stop trying to accept connections. One last thing, change your while loop to be:

    while(!stop) { ...
    

    Just to make sure it exits properly.

    Actually I’ve got a server running on my computer off Java, which uses the following code:

        threadHandler = new ThreadPoolExecutor( maxThreads, maxThreads, 1, TimeUnit.MINUTES,
                new ArrayBlockingQueue<Runnable>( maxThreads, true ) );
        while (!serverTerminated) {
            try {
                final Socket connectionSocket = serversocket.accept();
                SocketHandler handler = new SocketHandler( this, connectionSocket );
                threadHandler.submit( handler );
            } catch (IOException e) {
                if (!serverTerminated)
                    e.printStackTrace();
            }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I would like to run a str_replace or preg_replace which looks for certain words
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
This could be a duplicate question, but I have no idea what search terms

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.