gpt4 book ai didi

java - 我怎样才能安全地停止这个Java服务器程序?

转载 作者:行者123 更新时间:2023-12-02 03:32:52 25 4
gpt4 key购买 nike

我想通过终端在我的ubuntu计算机中运行一个java服务器程序,但问题是一旦我启动该程序,我就无法停止它(该程序正在终端中运行并等待客户端)。

这是我的代码:

import java.net.*; 
import java.io.*;

public class EchoServer2 extends Thread {
protected Socket clientSocket;

public static void main(String[] args) throws IOException {
ServerSocket serverSocket = null;

try {
serverSocket = new ServerSocket(2000);
System.out.println("Connection Socket Created");
try {
while (true) {
System.out.println("Waiting for Connection");
new EchoServer2(serverSocket.accept());
}
} catch (IOException e) {
System.err.println("Accept failed.");
System.exit(1);
}
} catch (IOException e) {
System.err.println("Could not listen on port: 2000.");
System.exit(1);
} finally {
try {
serverSocket.close();
} catch (IOException e) {
System.err.println("Could not close port: 2000.");
System.exit(1);
}
}
}

private EchoServer2(Socket clientSoc) {
clientSocket = clientSoc;
start();
}

public void run() {
System.out.println("New Communication Thread Started");

try {
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(),
true);
BufferedReader in = new BufferedReader(
new InputStreamReader(clientSocket.getInputStream()));

String inputLine;

while ((inputLine = in.readLine()) != null) {
System.out.println("Server: " + inputLine);
out.println(inputLine);

if (inputLine.equals("Bye."))
break;
}

out.close();
in.close();
clientSocket.close();
} catch (IOException e) {
System.err.println("Problem with Communication Server");
System.exit(1);
}
}
}

我知道我可以终止该进程,但我不想强行停止该程序。我想知道:如何安全地停止该程序?我如何在我的代码中实现这一点?

最佳答案

让我们说清楚,您在客户端接受时阻塞主线程。这并不是彻底关闭程序的真正方法。

我的解决方案是运行一个单独的线程,它将完成接受工作。

为了说明这一点,这是我的代码:

<小时/>

这是接受线程:

private static Thread acception = new Thread("Acception Thread") {

public void run() {

try {
while (true) {
System.out.println("Waiting for Connection");
new EchoServer2(serverSocket.accept());
}
-> } catch (SocketException e) {
-> if(serverSocket.isClosed())
-> System.out.println("Connection Closed.");
-> }
} catch (IOException e) {
System.err.println("Accept failed.");
System.exit(1);
}

}
};
<小时/>

这是修改后的 main 方法:

public static void main(String[] args) throws IOException {
ServerSocket serverSocket = null;


try {
serverSocket = new ServerSocket(2000);
System.out.println("Connection Socket Created");
-> acception.start();
} catch (IOException e) {
System.err.println("Could not listen on port: 2000.");
System.exit(1);
}


//support to close, using the command line.

Scanner scn = new Scanner(System.in);
String s = scn.next();

while(true) {
if("quit".equals.(s)) {
try {
serverSocket.close();
} catch (IOException e) {
System.err.println("Could not close port: 2000.");
System.exit(1);
} finally {
break;
}
}
s = scn.next();
}
}

关于java - 我怎样才能安全地停止这个Java服务器程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37843506/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com