[英]Objective-C: Check IP connection
我在檢查本地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];
}
知道為什么這行不通嗎?
@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];
};
}
這是
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.