我正在讀取來自交換機的ISO消息,並且其讀取時間過長。如果在8秒內沒有收到回覆,它甚至可以花費兩分鐘時間讀取整個流並切換超時。有沒有使用BufferedReader從套接字獲得輸入流的另一種方法?套接字花費太長的時間來讀取數據(緩衝讀取器)
s = new ServerSocket(8777);
echo("Server socket created.Waiting for connection...");
//get the connection socket
conn = s.accept();
echo("Connection received from " + conn.getInetAddress().getHostName() + " : " + conn.getPort());
//3. get Input and Output streams
out = new PrintStream(conn.getOutputStream());
//out.flush();
System.out.println(new Date());
//in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
System.out.println(new Date());
InputStream in = conn.getInputStream();
message = in.readLine();
echo("client>" + message);
System.out.println(new Date());
這裏你可以看到差異日誌從它開始讀,直到它輸出的消息
Server socket created.Waiting for connection...
Connection received from 11.xx.xx.xx : 51639
Fri Jul 08 11:53:48 EAT 2016
Fri Jul 08 11:53:48 EAT 2016
client>ISO8583-9300004918040387042160708122130801ISO8583- 9300004918040387049160708122230802
Fri Jul 08 11:55:51 EAT 2016
我想交換機不會以newLine結束傳輸 - 因此是延遲。嘗試讀取沒有BufferedReader的字節。片段中是否存在複製和粘貼錯誤?有兩個聲明。不應該編譯。 – Fildor
仍然無法正常工作。我嘗試過使用這段代碼,但延遲仍然存在 InputStream in = conn.getInputStream(); byte [] bytes = IOUtils.toByteArray(in); // message = in.readLine(); echo(「client>」+ bytes.toString()); System.out.println(new Date()); –
「此方法在內部緩衝輸入,因此不需要使用BufferedInputStream。」只需從InputStream中讀取一些合理大小的'byte []緩衝區'即可。 – Fildor