socket读不到流的结尾,直到报Read timed out

sysmaid 2013-06-04 03:17:08
初学iso8583,有做这方面的朋友,请多指点下。
代码:

try {
s.setSoTimeout(6000 * 5);//设定超时时间
input = new DataInputStream(new BufferedInputStream(s.getInputStream()));

// 头两个字节表示报文总(报文头+数据内容)长度
byte[] header = new byte[2];//2字节长的报头
int len = 0;
while((len = input.read(header)) != -1){
break;
}
int msgLen = Integer.parseInt(IsoUtil.hexString(header), 16);
System.out.println("megLen=" + msgLen);

//按每次两个字节读取
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int start = 0;
while((len = input.read(header, 0, 2)) != -1){
System.out.println("in while, len=" + len);
start += len;
baos.write(header);
}
System.out.println("start=" + start);

//读取全部报文
byte[] msg = new byte[start];

msg = baos.toByteArray();
System.out.println("hex=" + IsoUtil.hexString(msg));//报文内容
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


输出和异常:

~~~~~~~接受报文开始~~~~~~
from ip:/192.168.1.111:2059
Len=2
hex=003C
megLen=60
in while, len=2
in while, len=2
in while, len=2
in while, len=2
in while, len=2
in while, len=2
in while, len=2
in while, len=2
in while, len=2
in while, len=2
in while, len=2
in while, len=2
in while, len=2
in while, len=2
in while, len=2
in while, len=2
in while, len=2
in while, len=2
in while, len=2
in while, len=2
in while, len=2
in while, len=2
in while, len=2
in while, len=2
in while, len=2
in while, len=2
in while, len=2
in while, len=2
in while, len=2
in while, len=2
java.net.SocketTimeoutException: Read timed out
at java.net.SocketInputStream.socketRead0(Native Method)
at java.net.SocketInputStream.read(Unknown Source)
at java.io.BufferedInputStream.fill(Unknown Source)
at java.io.BufferedInputStream.read1(Unknown Source)
at java.io.BufferedInputStream.read(Unknown Source)
at java.io.DataInputStream.read(Unknown Source)
at com.hyt.pos.PosReceiver.run(PosReceiver.java:52)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
...全文
4029 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
sysmaid 2013-06-05
  • 打赏
  • 举报
回复
这样就明白了,所以8583报文的开头要先定义长度,就是让用这个长度来读取数据的,不能用-1来判断。
l_9style 2013-06-04
  • 打赏
  • 举报
回复
即client的socket.close后,才会发送给server端结尾符,从而才会read到结尾信息而返回-1
l_9style 2013-06-04
  • 打赏
  • 举报
回复
引用 3 楼 sysmaid 的回复:
[quote=引用 2 楼 l_9style 的回复:] 你这个input的流在第一个while循环里面已经读到末尾了,所以返回-1后退出第一个循环。 而你的第二个while循环任然再去使用input读取数据,这个时候的input流里面肯定没有东西让你再继续读下去了,又因为input.read()是个阻塞的方法,会一直等待下一个字节的出现,从而导致timeout。
我觉得不是这样的,我一次只读了2个字节,而且总长度是60,第二个while里也读了29次,然后应该结束的,但他没有,一直到报time out退出。 我在想是不是因为socket是保持连接的,所以read方法没有退出,一直等到time out才退出。[/quote] 额,我刚看错了,你的第一个while循环只读取了2个字节,就break掉了,我囧啊。第二个循环读取58个字节,打印出结果都是对的。之所以没有退出,的确是因为你的socket是保持连接的,这个是正常的,因为你的连接不是只发送一次数据就断开了,而是保持一个长连接,服务器等待你的client发送下一次的消息。 你如果想要让input.read()返回-1而退出的话,需要你的client端关闭连接,即socket.close()后,就是正常退出了。
sysmaid 2013-06-04
  • 打赏
  • 举报
回复
引用 2 楼 l_9style 的回复:
你这个input的流在第一个while循环里面已经读到末尾了,所以返回-1后退出第一个循环。 而你的第二个while循环任然再去使用input读取数据,这个时候的input流里面肯定没有东西让你再继续读下去了,又因为input.read()是个阻塞的方法,会一直等待下一个字节的出现,从而导致timeout。
我觉得不是这样的,我一次只读了2个字节,而且总长度是60,第二个while里也读了29次,然后应该结束的,但他没有,一直到报time out退出。 我在想是不是因为socket是保持连接的,所以read方法没有退出,一直等到time out才退出。
l_9style 2013-06-04
  • 打赏
  • 举报
回复
你这个input的流在第一个while循环里面已经读到末尾了,所以返回-1后退出第一个循环。 而你的第二个while循环任然再去使用input读取数据,这个时候的input流里面肯定没有东西让你再继续读下去了,又因为input.read()是个阻塞的方法,会一直等待下一个字节的出现,从而导致timeout。 /** * Reads some number of bytes from the input stream and stores them into * the buffer array <code>b</code>. The number of bytes actually read is * returned as an integer. This method blocks until input data is * available, end of file is detected, or an exception is thrown. * * <p> If the length of <code>b</code> is zero, then no bytes are read and * <code>0</code> is returned; otherwise, there is an attempt to read at * least one byte. If no byte is available because the stream is at the * end of the file, the value <code>-1</code> is returned; otherwise, at * least one byte is read and stored into <code>b</code>. *
sysmaid 2013-06-04
  • 打赏
  • 举报
回复
字迹先顶一下。 报错是在第二个while中,这一句: while((len = input.read(header, 0, 2)) != -1){ 我让他每次读两个字节,就是想看看读到的和报文头写的长度是否一致。 但为何读到最后,没有结束循环,而是一直等到了time out呢?

62,616

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧