!!!求救---serversocket.accept()如何中断?!!!

uruly 2004-11-27 11:37:47
我写了一个应用程序,它在执行serversocket.accept()时,想中断程序的
执行,点关闭按钮,可是不起作用,应该是因为accept()一直在运行的原因,
请问有什么方法可以让程序可以关闭?
谢谢
...全文
458 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
Avampire 2004-11-29
  • 打赏
  • 举报
回复
public class ServerMain{
Server svr = new Server();
public static void main(String args[]){
waitCommand();
}
/**
* 命令等待与采集循环
*/
public void waitCommand() {
svr.start();
BufferedReader commandReader = new BufferedReader(new InputStreamReader(System.in));
while (true) {
try {
String command = commandReader.readLine();
if (command.equals("q")) {
Server .stopServer();
System.exit(0);
}else
System.out.println("you input:"+command);
} catch (Exception e) {
System.out.println("faile in ServerMain.waitCommand:" + e);
}
}
}
//监听线程
public class Server extends Thread {
private ServerSocket listenSocket;
boolean stop = fals;
public void stopServer() {
this.stop = true; // Set the stop flag
this.interrupt(); // Stop blocking in accept()
}
public void run(){
.......
while(!stop){
....
Socket client = listenSocket.accept();
...
}

}
}
临时写的,小错就自己改改了
Avampire 2004-11-29
  • 打赏
  • 举报
回复
public class ClientMain{
Server svr = new Server();
public static void main(String args[]){
waitCommand();
}
/**
* 命令等待与采集循环
*/
public void waitCommand() {
svr.start();
BufferedReader commandReader = new BufferedReader(new InputStreamReader(System.in));
while (true) {
try {
String command = commandReader.readLine();
if (command.equals("q")) {
Server .stopServer();
System.exit(0);
}else
System.out.println("you input:"+command);
} catch (Exception e) {
System.out.println("faile in ClientMain.waitCommand:" + e);
}
}
}
//监听线程
public class Server extends Thread {
private ServerSocket listenSocket;
boolean stop = fals;
public void stopServer() {
this.stop = true; // Set the stop flag
this.interrupt(); // Stop blocking in accept()
}
public void run(){
.......
while(!stop){
....
Socket client = listenSocket.accept();
...
}

}
}
fjw3699 2004-11-29
  • 打赏
  • 举报
回复
一般做成线程,同时在线程的循环中加一个FLAG进行统一控制
uruly 2004-11-29
  • 打赏
  • 举报
回复
还有没有别的好方法
uruly 2004-11-28
  • 打赏
  • 举报
回复
我急用啊
uruly 2004-11-28
  • 打赏
  • 举报
回复
那要怎么办啊
javafaq2004 2004-11-28
  • 打赏
  • 举报
回复
这个好像没有肯定的方法做到。
funcreal 2004-11-28
  • 打赏
  • 举报
回复
把它放在一个线程里面就行了。
allen830826 2004-11-28
  • 打赏
  • 举报
回复
close不行吗?

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧