java serversocket 客户端 断开_如何中断ServerSocket accept()方法?

好的,我的工作方式更直接地解决了OP的问题 .

Keep reading past the short answer for a Thread example of how I use this.

简短回答:

ServerSocket myServer;

Socket clientSocket;

try {

myServer = new ServerSocket(port)

myServer.setSoTimeout(2000);

//YOU MUST DO THIS ANYTIME TO ASSIGN new ServerSocket() to myServer‼!

clientSocket = myServer.accept();

//In this case, after 2 seconds the below interruption will be thrown

}

catch (java.io.InterruptedIOException e) {

/* This is where you handle the timeout. THIS WILL NOT stop

the running of your code unless you issue a break; so you

can do whatever you need to do here to handle whatever you

want to happen when the timeout occurs.

*/

}

现实世界的例子:

在这个例子中,我有一个ServerSocket等待Thread内部的连接 . 当我关闭应用程序时,我想在我关闭应用程序之前以干净的方式关闭线程(更具体地说,套接字),所以我在ServerSocket上使用.setSoTimeout()然后我使用抛出的中断在超时后检查并查看父进程是否正在尝试关闭该线程 . 如果是这样,那么我设置关闭套接字,然后设置一个标志,指示线程已完成,然后我打破了返回null的Threads循环 .

package MyServer;

import javafx.concurrent.Task;

import java.io.IOException;

import java.net.ServerSocket;

import java.net.Socket;

import java.net.SocketException;

import javafx.concurrent.Task;

import java.io.IOException;

import java.net.ServerSocket;

import java.net.Socket;

import java.net.SocketException;

public class Server {

public Server (int port) {this.port = port;}

private boolean threadDone = false;

private boolean threadInterrupted = false;

private boolean threadRunning = false;

private ServerSocket myServer = null;

private Socket clientSocket = null;

private Thread serverThread = null;;

private int port;

private static final int SO_TIMEOUT = 5000; //5 seconds

public void startServer() {

if (!threadRunning) {

serverThread = new Thread(thisServerTask);

serverThread.setDaemon(true);

serverThread.start();

}

}

public void stopServer() {

if (threadRunning) {

threadInterrupted = true;

while (!threadDone) {

//We are just waiting for the timeout to exception happen

}

if (threadDone) {threadRunning = false;}

}

}

public boolean isRunning() {return threadRunning;}

private Task thisServerTask = new Task () {

@Override public Void call() throws InterruptedException {

threadRunning = true;

try {

myServer = new ServerSocket(port);

myServer.setSoTimeout(SO_TIMEOUT);

clientSocket = new Socket();

} catch (IOException e) {

e.printStackTrace();

}

while(true) {

try {

clientSocket = myServer.accept();

}

catch (java.io.InterruptedIOException e) {

if (threadInterrupted) {

try { clientSocket.close(); } //This is the clean exit I'm after.

catch (IOException e1) { e1.printStackTrace(); }

threadDone = true;

break;

}

} catch (SocketException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

}

return null;

}

};

}

然后,在我的Controller类中...(我只会显示相关代码,根据需要将其按入您自己的代码中)

public class Controller {

Server server = null;

private static final int port = 10000;

private void stopTheServer() {

server.stopServer();

while (server.isRunning() {

//We just wait for the server service to stop.

}

}

@FXML private void initialize() {

Platform.runLater(()-> {

server = new Server(port);

server.startServer();

Stage stage = (Stage) serverStatusLabel.getScene().getWindow();

stage.setOnCloseRequest(event->stopTheServer());

});

}

}

我希望这可以帮助有人在路上 .

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值