2016-07-20 57 views
0

我面臨檢查本地IP連接的問題。Objective-C:檢查IP連接

我正在使用此項目https://github.com/pbkhrv/SimpleSocketConnection檢查帶有端口的IP。

,當我要連接一個IP這項工作很好:

[[NetworkController sharedInstance] connect:@"192.168.1.100"]; 

但是,當我要檢查一樣,許多連接:

for(int i=1; i<255; i++) { 
    NSString *ip = [NSString stringWithFormat:@"192.168.1.%d", i]; 
    [[NetworkController sharedInstance] connect:ip]; 
} 

知道爲什麼這不起作用?

+0

「不工作」 是一個有點含糊。你期望發生什麼?什麼沒有發生?他們的錯誤信息? –

+1

看一看[這裏](http://stackoverflow.com/help/how-to-ask)瞭解更多關於提出好問題的信息。 –

+0

我想檢查很多連接,如果與ip連接存在與否。 –

回答

0
@interface ViewController() 
- (void)displayMessage:(NSString*)message; 
@end 


@implementation ViewController 

#pragma mark - Private methods 

- (void)displayMessage:(NSString*)message { 
    // These two came from UITextView+Utils.h 
    [textViewOutput appendTextAfterLinebreak:message]; 
    [textViewOutput scrollToBottom]; 
} 


#pragma mark - Public methods 

- (IBAction)connect:(id)sender { 

    for(int i=1; i<255; i++) { 
     NSString *ip = [NSString stringWithFormat:@"192.168.1.%d", i]; 
     [[NetworkController sharedInstance] connect:ip]; 
    } 


} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    // Enable input and show keyboard as soon as connection is established. 
    [NetworkController sharedInstance].connectionOpenedBlock = ^(NetworkController* connection){ 
    [textInput setUserInteractionEnabled:YES]; 
    [textInput becomeFirstResponder]; 

     NSLog(@"Connection opened"); 

    [self displayMessage:@">>> Connection opened <<<"]; 
    }; 

    // Disable input and hide keyboard when connection is closed. 
    [NetworkController sharedInstance].connectionClosedBlock = ^(NetworkController* connection){ 
    [textInput resignFirstResponder]; 
    [textInput setUserInteractionEnabled:NO]; 
    [self displayMessage:@">>> Connection closed <<<"]; 
    }; 

    // Display error message and do nothing if connection fails. 
    [NetworkController sharedInstance].connectionFailedBlock = ^(NetworkController* connection){ 
    [self displayMessage:@">>> Connection FAILED <<<"]; 
     NSLog(@"Connection failed"); 
    }; 

    // Append incoming message to the output text view. 
    [NetworkController sharedInstance].messageReceivedBlock = ^(NetworkController* connection, NSString* message){ 
    [self displayMessage:message]; 
    }; 
} 

這是