服务器
public void run () {
Socket serversocket = new ServerSocket(port);
while(true) {
new Thread(new ServerThread(serverSocket.accept())).start();
}
}
//serverSocket.close(); etc
服务器线程
public void run() {
BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
String input;
while(true) {
input = in.readLine();
new Thread(new RequestThread(clientSocket, input)).start();
}
}
//close sockets etc in.close() clientSocket.close();
请求线程
public void run() {
//input was passed from constructor
String output = new SomeProtocol(input);
if(output == null)
break;
//true for auto flush
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
out.println(output);
}//closing out seems to also close the socket, so opted to not to close it, maybe this is the one giving me trouble?
班长
public class ServerMonitor implements Runnable{
private ServerThread server;
private LoggingClass log = LoggingClass .getInstance();
private int heartbeat = 0;
public ServerMonitor(ServerThread server) {
this.server = server;
}
public boolean checkFile() {
File file = new File("cmd//in//shutdown.txt");
return file.exists();
}
public void run() {
log.logToFile("Server monitor running");
while (true) {
incHeartbeat();
if (checkFile()) {
log.logToFile("Shutting down server");
break;
}
writeStatus();
this.delay(5000);
}
log.logToFile("Server monitor stopped");
}
public void delay(long delay) {
try {
wait(delay);
} catch (InterruptedException e) {
log.logToFile("Monitor sleep error");
e.printStackTrace();
}
}
public void writeStatus() {
try {
BufferedWriter out = new BufferedWriter(new FileWriter(
"sys//status//status.txt"));
out.write("Start date:" + log.getStartDate());
out.newLine();
out.write("Current date:" + log.getTimestamp("yyyy-MMM-dd k:mm:ss"));
out.newLine();
out.write("Heartbeat:" + getHeartbeat());
out.newLine();
out.write("Cimd in:" + log.getCimdIn());
out.newLine();
out.write("Cimd out:" + log.getCimdOut());
out.newLine();
out.write("Keep alive:" + log.getCimdKeepAlive());
out.newLine();
out.write("HTTP in:" + log.getNumHTTPIn());
out.newLine();
out.write("HTTP out:" + log.getNumHTTPOut());
out.newLine();
out.close();
} catch (Exception e) {
log.logToFile("Write status error");
e.printStackTrace();
}
}
public int getHeartbeat() {
return heartbeat;
}
public synchronized void incHeartbeat() {
heartbeat++;
}
}
这是我的应用程序的大致框架。我有麻烦,因为有时它只是停止没有任何错误。我想可能是因为插座的缘故,但我不太确定,所以你们有什么想法吗?谢谢。
添加了监视服务器线程的类
>How do I know that it doesn't work
心跳不再增加
3条答案
按热度按时间az31mfrm1#
内存/线程可能不足
您是否有这样的高级异常处理:
如果是这样,你至少要记录你忽略的错误。
这正是我想到的,因为我手头没有你的源代码。
s1ag04yj2#
你能更好地定义“停止工作”吗?它是否停止接受连接;它会崩溃吗;它是否在处理过程中停止了某些事情;等?
有一点猜测是,当一个套接字关闭时,我不确定读卡器中会发生什么。我希望发生的是,尝试使用关闭的套接字调用read line会导致异常,从而停止该线程,并且/或者可能杀死整个应用程序。这将产生堆栈跟踪和其他问题。
基本上,除了oscar所说的在main中异常登录之外;您可能需要登录任何run()方法。try/catch在main中只捕获由主线程抛出的异常;不是其他线程中抛出的那些。
ff29svar3#
从您发布的伪代码很难判断,但如果应用程序挂起(而不是完全崩溃),我最好的猜测是serverthread的in.readline()阻塞了。如果应用程序刚刚关闭,我会查看套接字闭包,并检查bufferedreader中的isready()。
一般评论:您可能正在生成比您需要的更多的线程。它看起来像是从客户机套接字读取数据,然后对其进行写入。如果是这种情况,serverthread runnable(我假设它是runnable,因为您正在将它传递给线程的构造函数)不一定需要生成requestthread。