iOS SDK:如何检查端口是否打开? [英] iOS SDK: How can I check if a port is open?

查看:333
本文介绍了iOS SDK:如何检查端口是否打开?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于如何检查端口是否打开,我还没有找到任何东西.我尝试使用AsyncSocket类实现它,但是尽管我拒绝了到服务器上该端口的所有连接,但它始终返回TRUE. 此外,我尝试使用AsyncSocket的isConnected方法,但始终返回FALSE.

I have not found anything yet on how to check if a port is open or not. I tried to realize it with the AsyncSocket class but it returns always TRUE although I reject all connections to that port on my server. Additionally, I tried to use the isConnected method of AsyncSocket but that always returns FALSE.

到目前为止,我的代码:

My code so far:

//Init socket
socket=[[AsyncSocket alloc] initWithDelegate:self];

//results on TRUE always!
NSLog(@"Ready");

NSError *err = nil;
if(![socket connectToHost:@"10.1.2.40" onPort:25 error:&err])
{
    NSLog(@"Error: %@", err);
}
else
{
    NSLog(@"Connected");
}
//addition - results in FALSE always!

if([socket isConnected])
{
    NSLog(@"yes, its connected");
}
else
{
    NSLog(@"not connected...");
}
[socket disconnect];

推荐答案

您需要使自己成为委托并处理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.

这篇关于iOS SDK:如何检查端口是否打开?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆