我正在兩個站(A,B)之間建立標準TCP連接 A正在發送消息,B收到併發迴響應,然後關閉連接。爲DataInputStream設置手動超時
- 工作站B是「黑匣子」,我無法訪問更改或在那裏做任何事情。
有時B有沒有發送迴應的情況,然後我需要重新嘗試整個過程。
我想在站A的等待時間(等待答案B)的收回時間上設置超時。 所以基本上當等待時間過期時,我會派遣一個重試。
我沒有找到一種方法來設置DataInputStream的超時。 (僅適用於全套接字連接 - 我不想)
一些代碼:
/**
* Method receives the Server Response
*/
public byte[] receive(DataInputStream is) throws Exception
{
logger.debug(TAG + " Client Recieving...");
try
{
byte[] inputData = new byte[1024];
// here I want to set timeout for the "receiving mode"
is.read(inputData);
return inputData;
} catch (Exception e)
{
throw new Exception(TAG + " Couldnt receive data from modem: " + e.getMessage());
}
}
感謝, 射線。