Redian新闻
>
how to read from a non-return socket stream?
avatar
how to read from a non-return socket stream?# Java - 爪哇娇娃
c*n
1
after connection,
the server side just spits out a short string , which I know is less than
100 chars long.
so supposedly I could just wait 1 second, and read from the stream
whatever is available.
but the problem is that the stream does not terminate by a "\n", so I
can't do
readline
read(byte[] , int, int) doesn't work either, since I don't know the actual
length of the string.
apparently I can use NIO for this, but is there a simpler method?
Thanks
avatar
m*r
2
can yuou read one byte at a time until there is no more to be read?

【在 c******n 的大作中提到】
: after connection,
: the server side just spits out a short string , which I know is less than
: 100 chars long.
: so supposedly I could just wait 1 second, and read from the stream
: whatever is available.
: but the problem is that the stream does not terminate by a "\n", so I
: can't do
: readline
: read(byte[] , int, int) doesn't work either, since I don't know the actual
: length of the string.

avatar
c*n
3
define "no more to be read" ? :)
InputStream.isready() ??
looks a bit clumsy....

【在 m****r 的大作中提到】
: can yuou read one byte at a time until there is no more to be read?
avatar
c*n
4
define "no more to be read" ? :)
InputStream.isready() ??
looks a bit clumsy....

【在 m****r 的大作中提到】
: can yuou read one byte at a time until there is no more to be read?
avatar
h*0
5
服务器端发送完后会close吧?如果服务器竟然不close,把socket吊在那里,那神仙也
没办法啊……
如果会关,那简单,read(byte[])就成,用循环,看返回值是否为-1,为-1时表明已经
读完了。具体请参考j2se doc

【在 c******n 的大作中提到】
: after connection,
: the server side just spits out a short string , which I know is less than
: 100 chars long.
: so supposedly I could just wait 1 second, and read from the stream
: whatever is available.
: but the problem is that the stream does not terminate by a "\n", so I
: can't do
: readline
: read(byte[] , int, int) doesn't work either, since I don't know the actual
: length of the string.

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。