【发布时间】:2016-11-10 19:31:14
【问题描述】:
我正在从交换机读取 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 的情况下读取字节。 sn-p 中是否存在复制和粘贴错误?声明了两个
ins。不应该编译。 -
仍然无法正常工作。我已经尝试使用此代码,但延迟仍然存在 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 引用来自哪里?
-
@EJP 抱歉,应该已经发布了源代码。 commons.apache.org/proper/commons-io/javadocs/api-release/org/…