0

我有点问题。我正在尝试使用菜单制作服务器。Evrythink 还可以,但是当我启动服务器时它就卡住了。我的想法是有菜单,并用它来启动服务器:命令:“启动服务器”并启动。但是当我启动它时,我可以摆脱这个循环。这是我的代码。如何在“启动服务器”后返回主菜单?

package meetingsserver;

import java.util.Scanner;
import server.DB.DataBase;
import server.LoadSettings;


public class MeetingsServer {

    private static server.ServerCore serverCore = null;

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);
        LoadSettings loadSettings = LoadSettings.Init();

        System.out.println("Use 'help' command to list all commands");

        while (input.hasNext()) {
            String command = input.nextLine();

            switch (command) {
                case "start server":
                    System.out.println("Starting server ....");
                    serverCore = new server.ServerCore();
                    serverCore.start();
                    break;
                case "status server":
                    serverCore.statusServer();
                    break;
                case "set config":
                    loadSettings.writeDatacondig();
                    break;
                case "reload config":
                    loadSettings.loadDataConfig();
                    System.out.println("Configuration was reseted");
                    break;
                case "view config":
                    loadSettings.viewConfig();
                    break;
                case "view config file":
                    System.out.println("Configuration from file");
                    loadSettings.viewConfigFile();
                    break;
                case "help":
                    helpText();
                    break;
                case "stop server":
                    System.exit(1);
                    break;
                case "cls":
                    clsTerminal();
                    break;
                case "database tables":
                    DataBase.Init().showTables();
                    break;
                default:
                    helpText();
                    break;

            }//switch

        }//while

    }//main

    private static void helpText() {
        System.out.println("\t Help commands:\n\r");
        System.out.println("SERVER FUNCTIONS :");
        System.out.println("\tstart server\t start the server");
        System.out.println("\tstop server\t stop the server");
        System.out.println("\tstatus server\t return status server");
        System.out.println("CONFIGURATION FUNCTIONS:");
        System.out.println("\tset config\t adding new confing  keys with values");
        System.out.println("\t\t\t exiting from this mode write ;edited;");
        System.out.println("\treload config\t reload confinguration from config file");
        System.out.println("\tview config\t returns loaded configuration");
        System.out.println("\tview config file\t returns configuration of file");
        System.out.println("OTHER FUNCTIONS :");
        System.out.println("\thelp\t\t listing help commands");
        System.out.println("\tcls\t\t clear   console");
        System.out.println("DATABASE  FUNCTIONS: ");
        System.out.println("\t database tables\t  return all tables in mysql");
    }

    private static void clsTerminal() {
        for (int i = 0; i < LoadSettings.Init().getConfigInt("clsTerminal"); i++) {
            System.out.println("");
        }
    }
}

和 :

package server;

import java.io.IOException;
import java.net.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import server.DB.DataBase;

public class ServerCore extends Thread {

    private LoadSettings loadSettings = LoadSettings.Init();
    private int port = loadSettings.getConfigInt("port");
    private int max_connections = loadSettings.getConfigInt("max_connections");
    private ServerSocket socket;
    private Socket connection;
    private boolean serverRuning = false;
    private int connectedUsers = 0;

    @Override
    public void run() {
        this.start();
    }

    @Override
    public void start() {

        DataBase.Init();
        try {

            socket = new ServerSocket(port);
            serverRuning = true;

            while (serverRuning) {
                if (connectedUsers <= max_connections) {
                    connection = socket.accept();
                    connectedUsers++;
                }
                System.out.println(connection.toString());
            }

        } catch (IOException ex) {
            Logger.getLogger(ServerCore.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    public void stopServer() {
        if (serverRuning) {
            statusServer();
        }
        serverRuning = false;
        System.out.println("+++++\t  SERVER WAS STOPED !");

    }

    public void statusServer() {
        if (serverRuning) {
            System.out.println("Server running at port:" + port + "  with  users :" + connectedUsers);
        } else {
            System.out.println("Server IS NOT RUNNING!");
        }
    }
}
4

1 回答 1

1

你很困惑Thread#run()并且Thread#start()

start()神奇地启动一个新线程并立即返回给调用者(通常不需要覆盖start())。

run()执行线程的工作并由 Java 线程框架自动调用;你绝不run()能从你的代码中调用。

因此,将您的代码移入run()(可能存在一些同步问题,但您稍后会解决...)

于 2013-04-03T22:52:28.770 回答