开发者

What does -1 from DataInputStream's read() mean?

According to Java documentation, the read() method returns:

  • -1 if there is no more data because the end the stream has been reached

I don't quite understand what they mean by "end of stream".

Currently I close/re-open the socket when this occurs (which I understood from much Googling to be an acceptable way to deal with this). Is -1 possibly "normal" behavior? Should I not react so dramatically? Perhaps it would be better to only get upset on an IOException?

Socket creation:

clientSocket = new Socket(InetAddress.getByName(remoteHost), remotePort, InetAddress.getByName(sourceIpAddress), 0);

Some background information:

  • i am using the 'localAddr' attribute when creating the socket (project requirement)
  • i am running under Windows XP SP2
  • i have no idea how the remote server is handling sockets (or even what language, not 开发者_Python百科that that should matter)
  • this occurs very rarely (perhaps once a day)
  • only happens with one specific remote client running Linux
  • the trouble-some client and myself are both behind numerous firewalls
  • this never occurs with clients running various flavors of XP (perhaps it's time to setup a VMware image running Linux!)


The other end of the socket has closed the stream, so there is no more data to read - you've reached the end. I'd diagnose further with a network monitor such as Wireshark as to why the socket gets closed.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜