我正在读取来自交换机的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