且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

iOS SDK:如何检查端口是否打开?

更新时间:2021-07-10 22:47:42

您需要使自己成为委托并处理onSocket:willDisconnectWithError:方法.该连接是完全异步的,因此,除非存在根本的系统问题(禁用了套接字,您传递了无效的地址等),否则该调用将始终成功,而套接字连接尝试将在后台进行.

You need to make yourself the delegate and handle the onSocket:willDisconnectWithError: method. The connection is entirely async so unless there is a fundamental system problem (sockets are disabled, you passed in an invalid address, etc) then that call will always succeed while the socket connection attempt happens in the background.

如果该尝试失败,则将调用onSocket:willDisconnectWithError:委托方法,以便您可以知道连接尝试失败.

If that attempt fails, the onSocket:willDisconnectWithError: delegate method will get called so you can know the connection attempt failed.

我不确定为什么,但是AsyncSocket类认为kCFStreamStatusError流状态为已连接",因此我怀疑这就是为什么它显示为已连接.您可以在AsyncSocket的源代码中关注所有这一切.

I'm not sure why but the AsyncSocket class considers the kCFStreamStatusError stream status to be "connected" so I suspect that is why it shows up as connected. You can follow all this in the source of AsyncSocket.