@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];
};
}
這是
「不工作」 是一個有點含糊。你期望發生什麼?什麼沒有發生?他們的錯誤信息? –
看一看[這裏](http://stackoverflow.com/help/how-to-ask)瞭解更多關於提出好問題的信息。 –
我想檢查很多連接,如果與ip連接存在與否。 –