[英]Socket taking too long to read data (BufferedReader)
我正在從交換機讀取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
您發布的輸出不包含行,因此readLine()
不適合,並且您希望一次發送一條消息,因此IOUtils.toByteArray()
也不適合。 嘗試,錯誤, read(byte[])
。
我猜您應該使用read(byte[])
並以某種方式檢測消息的結尾。
我對ISO8583不熟悉,因此您必須弄清楚這一點。 可能是固定長度的消息協議,或者您可以檢測到消息終止符。
一個典型的例子是:
private static final int BUFFER_SIZE = 1024; // Or other reasonable value
// ...
byte[] buffer = new byte[BUFFER_SIZE];
int bytesRead = 0;
// assuming you got the InputStream as "input"
while ( (bytesRead = input.read(buffer)) > 0 ){ // -1 indicates EOF
// read bytes are now in buffer[0..bytesRead-1]
// analyse bytes to maybe add up multiple reads to a complete message.
}
為了簡潔起見,我省略了異常處理。
猜猜:message = in.readLine(); 等待到endofLine \\ n。 也許您不發送一些消息,或者錯過了發送套接字/流的沖洗過程。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.