如何终止监听客户端创建套接字请求的java程序?

时间:2013-03-08 23:09:21

标签: sockets terminate

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

我希望在程序运行时输入“end”,但我无法让它工作。 它侦听客户端并调用线程,以便为新客户端创建套接字。 我无法弄清楚如何使这个程序结束。我尝试了一切。

我昨天开始学习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

}

}

0 个答案:

没有答案