客户端套接字中的SocketTimeoutException(在java中)
我正在开发一个ServerSocket,它在处理请求时会延迟很多时间,这会导致客户端出现异常(SocketTimeoutException)。 我试图增加超时时间,但客户端没有从服务器接收到任何东西(这两种情况都没有例外) 服务器客户端套接字中的SocketTimeoutException(在java中),java,sockets,Java,Sockets,我正在开发一个ServerSocket,它在处理请求时会延迟很多时间,这会导致客户端出现异常(SocketTimeoutException)。 我试图增加超时时间,但客户端没有从服务器接收到任何东西(这两种情况都没有例外) 服务器 FileOutputStream parameters = new FileOutputStream(zipOutputPath); int len; byte[] buffer = new byte[1024]; System.ou
FileOutputStream parameters = new FileOutputStream(zipOutputPath);
int len;
byte[] buffer = new byte[1024];
System.out.print("Receiving data...");
while ((len = input.read(buffer)) != -1) {
parameters.write(buffer, 0, len);
}
parameters.close();
//This take a console output and send it to the client (sequentially).
//But some times the console can take several minutes to return a line (this is the problem)
BufferedReader d = new BufferedReader(new InputStreamReader(new BufferedInputStream(Runtime.getRuntime().exec(command).getInputStream())));
String lineOutput;
while ((lineOutput = d.readLine()) != null) {
if (!lineOutput.equals("")) {
outPut.println(lineOutput);// outPut (PrintWriter) is the ServerSocket output
outPut.flush();
}
}
客户
System.out.print("Sending arguments...");
int len;
byte[] buffer = new byte[1024];
while ((len = zipFile.read(buffer)) != -1) {
output.write(buffer, 0, len);
}
connection.shutdownOutput();// I do this because the server stays reading indefinitely, and I don't understand why, HELP ME WITH THIS TOO
BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
PrintWriter pw = new PrintWriter(fileOutputPath);
String line;
//Here is where a SocketTimeOutException occur
while ((line = br.readLine()) != null) {
pw.println(line);
pw.flush();
}
pw.close();
connection.close();
}
很抱歉,从您提供的代码来看,我的英语不清楚服务器上的数据来自何处。它是否从客户端接收数据,然后以压缩格式返回?这同样适用于您的客户端-什么是
zipFile
,output
?如果控制台需要很长时间才能返回输入(如果用户出去吃午饭,可能会发生这种情况),您需要相应地选择套接字超时。太短了,