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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T13:13:15+00:00 2026-05-31T13:13:15+00:00

Here is my programme, which have a two threads, one is listening user input,

  • 0

Here is my programme, which have a two threads, one is listening user input, another is a socket:

        bio = new BasicConsoleIO();
        bio.assignObject(worker);

        Thread b = new Thread(bio);
        b.start();

        Thread a = new Thread(worker);
        a.start();

The worker is a socket, and the BasicConsoleIO is responsible for listening the user input
The BasicConsoleIO is something like that:

private Worker worker;
static BufferedReader reader;

@Override
public void run() {
    //......Code Skip......//
    if (inputString.equalsIgnoreCase("q")) {
        this.applicationQuit();
    }
}

public void applicationQuit(){
    this.getWorker().stopWorking();
    System.exit(0);
}

When it press ‘q’, the application will call the worker to close the socket, and quit the program, and the Worker works this way:

private ServerSocket providerSocket;
private Socket socket = null;

int port = 1234;

Worker() {
}

public void stopWorking() {
    System.out.println("worker stop working");

    try {
        if (providerSocket != null) {
            providerSocket.close();
        }
        if (socket != null) {
            socket.close();
        }

    } catch (IOException ioException) {
        ioException.printStackTrace();
    }
}

@Override
public void run() {

    try {
        providerSocket = new ServerSocket(this.port);

        while (true) {
            if (!providerSocket.isClosed()) {

                socket = providerSocket.accept();

                WorkTask wt = new WorkTask();
                wt.setSocket(socket);

                Thread a = new Thread(wt);
                a.start();
            }
        }

    } catch (IOException e) {
        e.printStackTrace();
    }
}

The worker will keep listening the request, and assign the new work task in a separate thread, the details of worktask like this:

Socket socket;
ObjectOutputStream out;
ObjectInputStream in;
Object receivedObj;
String message;

@Override
public void run() {
    try {

        do {
            out.flush();
            receivedObj = in.readObject();

                              //......Code Skip......//

        } while (receivedObj != null
                && !receivedObj.equals(SharedConstant.SOCKET_EOF_STRING));

        if (in != null) {
            in.close();
        }
        if (out != null) {
            out.close();
        }

    } catch (IOException e1) {
        e1.printStackTrace();
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }
}

But When I launch the programme, and press ‘q’ to exit, it warns me with this error:

Please assign a port number 2333 Press ‘q’ to kill to programme
Waiting for connection: 2333 q worker stop working Run me anyway!
java.net.SocketException: Socket closed at
java.net.PlainSocketImpl.socketAccept(Native Method) at
java.net.PlainSocketImpl.accept(PlainSocketImpl.java:408) at
java.net.ServerSocket.implAccept(ServerSocket.java:462) at
java.net.ServerSocket.accept(ServerSocket.java:430) at
com.mydefault.package.Worker.run(Worker.java:61) at
java.lang.Thread.run(Thread.java:680)

  • 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-31T13:13:15+00:00Added an answer on May 31, 2026 at 1:13 pm

    You can see the

    socket = providerSocket.accept();
    

    is throwing an exception because in

    public void stopWorking() {
        // Socket won't close unless the user make it to close
        // 4: Closing connection
        System.out.println("worker stop working");
    
        try {
            if (providerSocket != null) {
                providerSocket.close();
    

    you closed it.

    If you want to avoid this error, I have a volatile boolean closed field which I set to true and check before I report an error. i.e. ignore errors when I am closing down.

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

Sidebar

Related Questions

Just a quick question here. I have a program in which I need to
Newbie here...can I write one program which incorporates .NET LINQ and also various Java
I have written a program a.exe which launches another program I wrote, b.exe ,
I have a pagination script, which can be seen here: http://www.automotori6282.tk/phpsandbox/ This is the
I have a ASP.NET MVC 3 view with esentially two forms, but which reside
I have two code files, one contains my WinMain() function, and all functions related
Here is a special Haskell program which outputs a Python program that outputs a
hobbyist/non-profession programmer here. I don't have a strong grasp on network addressing. I have
I have a program here: #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/ipc.h> #include
I have developed a Java Swing application, which uses the SwingWorker class to perform

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.