为什么当我退出程序时会警告我socket是关闭的

为什么当我退出程序时会警告我socket是关闭的

本文介绍了为什么当我退出程序时会警告我socket是关闭的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的程序,它有两个线程,一个是听用户输入,另一个是套接字:

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();

工作者是一个套接字,BasicConsoleIO负责监听用户输入
BasicConsoleIO是这样的:

The worker is a socket, and the BasicConsoleIO is responsible for listening the user inputThe 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);
}

当按q时,应用程序将呼叫工作人员关闭socket,并退出程序,而Worker的工作方式如下:

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();
    }
}

但是当我启动程序,然后按'q '退出,它警告我这个错误:

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


推荐答案

你可以看到

socket = providerSocket.accept();

抛出异常,因为在

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();

关闭它。

如果你想要避免这个错误,我有一个 volatile布尔关闭字段,我设置为 true 并在我报告之前检查错误。即在关闭时忽略错误。

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.

这篇关于为什么当我退出程序时会警告我socket是关闭的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-02 01:19