1

当我在 Ubuntu 中使用 Java JDK 在终端中运行该程序时,如何使该程序退出?

我想在这个程序运行时输入“结束”,但我无法让它工作。它监听客户端并调用一个线程来为新客户端创建一个套接字。我不知道如何使这个程序结束。我什么都试过了。

我昨天开始学习java。请帮我....

import java.net.*;          //jave lib
import java.io.*;           //io lib

public class MultiServerConnections {       //initiate class
public static void main(String[] args) throws IOException {

    int portNum = 5342;                 //set server port number
    boolean listen = true;

    System.out.println("Listening for Connections");    //print message

    ServerSocket server_Socket = null;          //set server_Socket to null
    try {



        server_Socket = new ServerSocket(portNum);      //set server port
    } catch (IOException e) {
        System.err.println("Port " + portNum + " is unavailable"); //port is taken error
        System.exit(1);
    }

BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in)); //set reader
String input;

    //while (TCPglobals.checkRequests){
    while (listen == true){
    new MultiServer(server_Socket.accept()).start();

    input = stdIn.readLine();
    //if(TCPglobals.checkRequests == false) //|| (input = stdIn.readLine()) == "end")
    if(input == "end") {
        System.out.println("end connection?");
        System.exit(1);
    }

}//while

    server_Socket.close(); //close server socket

}

}

4

0 回答 0