简体   繁体   English

使用NIO时如何确保SocketChannel已真正连接?

[英]How to make sure the SocketChannel is really connected when using NIO?

I'm working on a NIO client program on Android, Basically it functions well, but when the network is not avaliable(WIFI is disabled, etc), I have no idea how to distinguish the socket is 'really' connected, SocketChannel.finishConnect() always returns true even there is no avaliable network.If I try to write to a unconnected SocketChannel, an Broken pipe exception will happen. 我正在使用Android上的NIO客户端程序工作,基本上它运行良好,但是当网络不可用(禁用WIFI等)时,我不知道如何区分套接字是否已“真正”连接,即SocketChannel.finishConnect即使没有可用的网络,()始终返回true。如果我尝试写入未连接的SocketChannel,则会发生管道中断的情况。

So I wonder how to make sure the SocketChannel is really connected? 因此,我想知道如何确保SocketChannel确实已连接?

If I try to write to a unconnected SocketChannel, an Broken pipe exception will happen. 如果我尝试写入未连接的SocketChannel,则会发生管道中断的情况。

Correct. 正确。

So I wonder how to make sure the SocketChannel is really connected? 因此,我想知道如何确保SocketChannel确实已连接?

That's how. 这就是如何。 There is no API that can tell you. 没有API可以告诉您。 TCP doesn't work like that. TCP不能那样工作。 The only way to detect a broken TCP connection is to try to use it. 检测断开的TCP连接的唯一方法是尝试使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM