客户端Socket中的SocketTimeoutException(在java中)

时间:2013-11-22 23:07:55

标签: java sockets

我正在开发一个ServerSocket,它会延迟处理请求的大量时间,这会导致客户端出现异常(SocketTimeoutException)。 我试图增加超时并且它可以工作,但是客户端没有从服务器接收任何内容(两者都没有例外)。

服务器

    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();
}

为我的英语而烦恼

0 个答案:

没有答案