iOS/Objective-C中的TCP客户端-检查开放端口

iOS/Objective-C中的TCP客户端-检查开放端口,ios,iphone,objective-c,tcp,cocoaasyncsocket,Ios,Iphone,Objective C,Tcp,Cocoaasyncsocket,0关于AsyncSocket,我已经仔细研究了一些问题,但没有单击任何内容。我觉得我要做的是相当简单的,所以它将是脸掌。。。我尝试过非GCD版本,但也没有乐趣 我需要能够扫描一系列主机的特定开放TCP端口。不幸的是,我无法控制服务器代码,无法让它使用Bonjour声明自己。所以,我要进行暴力扫描,并尝试在一个端口上连接——如果我得到“连接被拒绝”,那么我知道要继续 所有这些让我想到了AsyncSocket,我认为它是适合这项工作的工具。通过作者的例子,我得到了一个基本的工作示例,但不是很好。和他

0关于AsyncSocket,我已经仔细研究了一些问题,但没有单击任何内容。我觉得我要做的是相当简单的,所以它将是脸掌。。。我尝试过非GCD版本,但也没有乐趣

我需要能够扫描一系列主机的特定开放TCP端口。不幸的是,我无法控制服务器代码,无法让它使用Bonjour声明自己。所以,我要进行暴力扫描,并尝试在一个端口上连接——如果我得到“连接被拒绝”,那么我知道要继续

所有这些让我想到了AsyncSocket,我认为它是适合这项工作的工具。通过作者的例子,我得到了一个基本的工作示例,但不是很好。和他的示例一样,为了简单起见,我只使用了主队列。问题是,对于委托方法是否被调用,这似乎是一个废话。有时插座断开:着火,有时不着火。在这种情况下,我知道端口5002在192.168.1.7上是打开的。但是,当它到达数组中的该元素时,didConnectToHost不会触发。但是,如果我从IP列表中删除除.7之外的所有内容,那么didConnectToHost就会启动。我怀疑在如此紧密的循环中调用connectToHost是一个问题,但我无法证明这一点。任何帮助都将不胜感激。如果有人知道一种更简单的方法来实现这一点,我愿意接受。令人惊讶的是,关于这个话题的内容并不多。提前感谢您的帮助

抢劫

#导入“GCDAsyncSocket.h”
#定义端口5002
@实现视图控制器
{
GCDAsyncSocket*异步套接字;
NSMutableArray*可用主机;
}
-(IBAction)startScan
{
NSArray*ipAddressList=@[@“192.168.1.1”、“192.168.1.2”、“192.168.1.3”、“192.168.1.4”、“192.168.1.5”、“192.168.1.6”、“192.168.1.7”];
dispatch_queue_t mainQueue=dispatch_get_main_queue();
asyncSocket=[[GCDAsyncSocket alloc]initWithDelegate:self-delegateQueue:mainQueue];
n错误*错误=nil;
对于(int i=1;i
我尝试了类似的方法,首先找到了自己的IP,然后修改它以显示整个子网范围。我得到了类似于“192.168.10”的东西,然后我得到了

- (void)startScan:(NSString *)ipRange
{
    dispatch_queue_t mainQueue = dispatch_get_main_queue();

    asyncSocket = [[GCDAsyncSocket alloc] initWithDelegate:self delegateQueue:mainQueue];
    NSError *error = nil;

    for (int i = 1; i < 255; i++) {
        NSString *scanHostIP = [NSString stringWithFormat:@"%@%d", ipRange, i];
        [asyncSocket connectToHost:scanHostIP onPort:PORT error:&error];
    }
    //connect to an IP that listens on the certain port
    [asyncSocket connectToHost:@"192.168.10.162" onPort:PORT error:&error]; 
}
-(void)开始扫描:(NSString*)ipRange
{
dispatch_queue_t mainQueue=dispatch_get_main_queue();
asyncSocket=[[GCDAsyncSocket alloc]initWithDelegate:self-delegateQueue:mainQueue];
n错误*错误=nil;
对于(int i=1;i<255;i++){
NSString*scanHostIP=[NSString stringWithFormat:@“%@%d”,ipRange,i];
[asyncSocket connectToHost:scanHostIP onPort:PORT错误:&错误];
}
//连接到侦听特定端口的IP
[asyncSocket connectToHost:@“192.168.10.162”on端口:端口错误:&错误];
}
当我尝试连接到循环中的162时,委托方法从未被调用。但当我只连接到特定的IP时,它会突然被调用。 有趣的是,当我第一次运行循环然后尝试连接到特定主机时,委托方法没有被调用

我试着在有超时和没有超时的情况下连接,但这两种方法都没有

我检查了connectToHost方法,它总是返回一些东西。即使我尝试连接到2个IP,也不会触发委托方法

总结一下我的经验:循环似乎以某种方式阻止了委托方法。我怀疑它只意味着连接到一个地址?那么它将是扫描子网的错误工具

编辑: 我找到了一个很好的解决办法。为了扫描整个网络,我开始使用以下代码:

-(void)startScan
{
    dispatch_queue_t mainQueue = dispatch_get_main_queue();

    asyncSocket = [[GCDAsyncSocket alloc] initWithDelegate:self delegateQueue:mainQueue];
    NSError *error = nil;

    NSString *scanHostIP = [NSString stringWithFormat:@"%@%d", ipRange, count];
    [asyncSocket connectToHost:scanHostIP onPort:PORT withTimeout:0.01 error:&error];
}

- (void)socketDidDisconnect:(GCDAsyncSocket *)sock withError:(NSError *)err
{
    NSError *error;
    if(count < 254)
    {
        count++;
        NSString *scanHostIP = [NSString stringWithFormat:@"%@%d", ipRange, count];
        [asyncSocket connectToHost:scanHostIP onPort:PORT withTimeout:0.01 error:&error];
    }
}
-(无效)开始扫描
{
dispatch_queue_t mainQueue=dispatch_get_main_queue();
asyncSocket=[[GCDAsyncSocket alloc]initWithDelegate:self-delegateQueue:mainQueue];
n错误*错误=nil;
NSString*scanHostIP=[NSString stringWithFormat:@“%@%d”,ipRange,count];
[asyncSocket connectToHost:scanHostIP onPort:端口超时:0.01错误:&错误];
}
-(void)socketDidDisconnect:(GCDAsyncSocket*)sock错误:(NSError*)错误
{
n错误*错误;
如果(计数<254)
{
计数++;
NSString*scanHostIP=[NSString stringWithFormat:@“%@%d”,ipRange,count];
[asyncSocket connectToHost:scanHostIP onPort:端口超时:0.01错误:&错误];
}
}
诀窍是等待套接字超时,然后启动新连接。通过这种方式,您可以在可接受的时间间隔内扫描整个网络


希望这对您或其他人有所帮助。

我不这么认为,获取端口比获取ip地址更难。你有解决办法吗?
-(void)startScan
{
    dispatch_queue_t mainQueue = dispatch_get_main_queue();

    asyncSocket = [[GCDAsyncSocket alloc] initWithDelegate:self delegateQueue:mainQueue];
    NSError *error = nil;

    NSString *scanHostIP = [NSString stringWithFormat:@"%@%d", ipRange, count];
    [asyncSocket connectToHost:scanHostIP onPort:PORT withTimeout:0.01 error:&error];
}

- (void)socketDidDisconnect:(GCDAsyncSocket *)sock withError:(NSError *)err
{
    NSError *error;
    if(count < 254)
    {
        count++;
        NSString *scanHostIP = [NSString stringWithFormat:@"%@%d", ipRange, count];
        [asyncSocket connectToHost:scanHostIP onPort:PORT withTimeout:0.01 error:&error];
    }
}