Java:客户端-服务器套接字编程 - 服务器 returns,但未在客户端接收

Java: client-server socket programming - server returns, but not receiving at client

现在问题:

代码片段: 客户:

Socket server = null;
OutputStream outToServer = null;
DataOutputStream out = null;
InputStream inFromServer = null;
DataInputStream in = null;

if(msg != null){ 
try
{
    server = new Socket(serverIp, serverPort);
    server.setSoTimeout(1000 * 60 * timeoutInMins); //set to 30 mins
    outToServer = server.getOutputStream();
    out = new DataOutputStream(outToServer);
    out.writeUTF(msg);
    out.flush();

    // now wait for Server reply
    inFromServer = server.getInputStream();
    in = new DataInputStream(inFromServer);
    responseString = in.readUTF();
    // do something with response
} 

服务器:

ServerSocket serverSocket = null;
Socket client = null;
try{
serverSocket = new ServerSocket(port);
} catch (Exception e) {//log this}
try
{
  while(true)
  {
    client = serverSocket.accept();

    if(client.getRemoteSocketAddress() != null){
        try{
            ReqObject request = getRequest(client);
            // do processing. this may take upto 10-15 mins at max
            sendBackResponse(client, request);
        }

        // do remaining


private void sendBackResponse(Socket client, ReqObject result) throws IOException {
    DataOutputStream out = null;
    try{
        out = new DataOutputStream(client.getOutputStream());
        String outToClient = result.toString();
        out.writeUTF(outToClient);
        out.flush();
    } finally {
        try{out.close();}catch(IOException e){}
    }
}

private ReqObject getRequest(Socket client) throws IOException {
    DataInputStream in = null;
    in = new DataInputStream(client.getInputStream());
    String incoming = in.readUTF();
    return convertMessageToRequest(incoming);
}

在 5 分钟不活动后,连接 disconnected/blocked 通过介于两者之间的防火墙。

为了克服这个问题,我开始每 2 分钟发送一次心跳消息(带有垃圾数据)。这使连接保持活动状态,直到操作完成。

注意:java 提供的 keepalive() 方法没有帮助..