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

发布于 2024-11-15 01:46:39 字数 611 浏览 4 评论 0原文

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

到目前为止我的代码:

//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];

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];

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你不是我要的菜∠ 2024-11-22 01:46:39

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

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

我不确定为什么,但 AsyncSocket 类认为 kCFStreamStatusError 流状态为“已连接”,所以我怀疑这就是它显示为已连接的原因。您可以在 AsyncSocket 的源代码中了解所有这些内容。

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.

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文