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