Socket - 10086 사례


#import "Serverlistener.h"
#import "GCDAsyncSocket.h"



@interface Serverlistener ()

@property (nonatomic, strong) GCDAsyncSocket *server;

//           Socket   --
@property (nonatomic, strong) NSMutableArray *clientSockets;

@end

@implementation Serverlistener


- (NSMutableArray *)clientSockets{
    
    if (!_clientSockets) {
        _clientSockets = [NSMutableArray array];
    }
    return _clientSockets;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
}

/**
 
 1.       
 2.        
 3.         
 4.          
 5.          
 6.          
 
 */


// 1.       socket          
- (void)start{
    
    // 1.         
    //1.1     Socket  __  __     (             )
    self.server = [[GCDAsyncSocket alloc] initWithDelegate:self delegateQueue:dispatch_get_global_queue(0, 0)];
    
    // 1.2    _         __5288          1024 (  1024      )
    NSError *error = nil;
    [self.server acceptOnPort:5288 error:&error];
    
    if (error) {
        
        NSLog(@"       ");
        
    }else{
        
        NSLog(@"       ");
    }
    
    //      __  main           
        [[NSRunLoop mainRunLoop] run]; --  main   

    
}

// 2.         _                newSocket          
- (void)socket:(GCDAsyncSocket *)sock didAcceptNewSocket:(GCDAsyncSocket *)newSocket{
    
    NSLog(@"        ");
    
    // 3.       
    // 3.1_                                   
    [self.clientSockets addObject:newSocket];
    
    // 3.2                 __                
    NSMutableString *dataStr = [NSMutableString string];
    [dataStr appendString:@"    10086    ,            
"]; [dataStr appendString:@"[0]
"]; [dataStr appendString:@"[1]
"]; [dataStr appendString:@"[2]
"]; [dataStr appendString:@"[3]
"]; [dataStr appendString:@"[4]
"]; [newSocket writeData:[dataStr dataUsingEncoding:NSUTF8StringEncoding] withTimeout:-1 tag:0]; // 3.3 __ // -1 : 0 : [newSocket readDataWithTimeout:-1 tag:0]; } // 4. __ #warning [newSocket readDataWithTimeout:-1 tag:0]; - (void)socket:(GCDAsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag{ NSString *readStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; NSLog(@"%@",readStr); #warning [newSocket readDataWithTimeout:-1 tag:0]; [sock readDataWithTimeout:-1 tag:0]; NSString *reulitStr = nil; switch ([readStr integerValue]) { case 0: reulitStr = @"
"; break; case 1: reulitStr = @"
"; break; case 2: reulitStr = @" 5
"; break; case 3: reulitStr = @"
"; break; case 4: reulitStr = @"
"; break; default: break; } // __ [sock writeData:[reulitStr dataUsingEncoding:NSUTF8StringEncoding] withTimeout:-1 tag:0]; // if ([readStr integerValue] == 4) { [self.clientSockets removeObject:sock]; } }

터미널 사용:
 $ telnet +IP  

호출 가능 (테스트)

좋은 웹페이지 즐겨찾기