简体   繁体   English

Java循环退出条件

[英]Java loop exit condition

I am writing a network server in java, and I have a tiny problem with it. 我正在用Java编写网络服务器,但它有一个小问题。 Let the code do the talking: 让代码进行讨论:

import java.io.IOException;
import java.net.ServerSocket;
/**
* Another flavor of the server module.
* According to GOD (also known as Joshua Bloch) this is the proper
* implementation of a singleton (Note: serialization).
* 
* @author estol
*/
public enum EnumSingletonServer implements ServerInterface, Runnable
{
    SERVER;
    private ServerSocket serverSocket = null;
    private boolean Listening         = true;

    @Override
    public void bind() {
        this.bind(DEFAULTPORT);
    }

    @Override
    public void bind(int Port) {
        try {
            this.serverSocket = new ServerSocket(Port);
        } catch (IOException ioe) {
            System.err.printf("Cannot bind to port %d\nAdditional information:\n%s\nExiting\n", Port, ioe.getMessage());
            System.exit(1);
        }
    }

    /**
    * Not that elegant, but does not work with flipping the switch, because
    * the loop (in public void run()) is only running when there is an incoming connection(?).
    * 
    * 
    * FIXME
    */
    @Override
    public void shutdown() {
        // this.Listening = !this.Listening; // FIXME
        System.exit(0);

    }
    /**
    * Accepting connections on the port, we are bound to.
    * The main loop of the server is a bit broken. Does not exit,
    * if we flip the value of this.Listening, but exit on the next incoming
    * connection. This is a problem.
    * 
    * FIXME
    */
    @Override
    public void run() {
        try {
            System.out.printf("Listening on %d\n", this.serverSocket.getLocalPort());
            Thread.currentThread().setName("ServerThread");
            // FIXME
            do {
                new Thread(new ServerWorkerThreads(this.serverSocket.accept(), 3)).start();
            } while (this.Listening);

            this.serverSocket.close();
            System.exit(0);
        } catch (IOException ioe) {
            System.err.printf("Cannot accept on, or close port %d\nAdditional information:\n%s\nExiting\n", this.serverSocket.getLocalPort(), ioe.getMessage());
            System.exit(1);
        }
    }
}

Now, as said in the comments, if I negate/flip/change the value of the variable this.Listening, the loop does not stop, but exits on the next connection. 现在,如评论中所述,如果我否定/翻转/更改变量this.Listening的值,则循环不会停止,而是在下一个连接上退出。

I did not implement the server in an enumerator at first, I did it with a class, which failed to be a singleton after being serialized, but that executed as I expected it to do so. 首先,我没有在枚举器中实现服务器,而是通过一个类来实现的,该类在序列化后无法成为单例,但可以按预期执行。 I tried a while (condition) done, and also the do while(condition) loop. 我尝试了一段时间(条件)完成,还尝试了while(条件)循环。

All help would be appreciated. 所有帮助将不胜感激。

In your shutdown method just close the socket instead of setting the variable: 在关闭方法中,只需关闭套接字即可,而不是设置变量:

this.serverSocket.close()

This will raise a SocketException on the accept loop and the loop is going to stop. 这将在accept循环上引发SocketException ,并且循环将停止。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM