#import <UIKit/UIKit.h>#import "GCDAsyncSocket.h"@interface ViewController : UIViewController <GCDAsyncSocketDelegate> { GCDAsyncSocket *serverSocket; NSMutableArray *allClientArray;}@end
#import "ViewController.h"#import "SendViewController.h"@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; self.navigationItem.title = @"接收消息"; [self showNavItem]; [self createTcpSocket];}- (void) createTcpSocket { allClientArray = [NSMutableArray array]; dispatch_queue_t dQueue = dispatch_queue_create("My socket queue", NULL); serverSocket = [[GCDAsyncSocket alloc] initWithDelegate:self delegateQueue:dQueue socketQueue:nil]; [serverSocket acceptOnPort:12345 error:nil];}#pragma mark - 代理方法 接收到一个请求- (void)socket:(GCDAsyncSocket *)sock didAcceptNewSocket:(GCDAsyncSocket *)newSocket { NSString *ip = [newSocket connectedHost]; uint16_t port = [newSocket connectedPort]; NSLog(@"new socket [%@:%d] is %@", ip, port, newSocket); [allClientArray addObject:newSocket]; [newSocket readDataWithTimeout:-1 tag:200]; NSString *s = @"Welcome"; NSData *data = [s dataUsingEncoding:NSUTF8StringEncoding]; [newSocket writeData:data withTimeout:60 tag:300];}#pragma mark - 接收到数据代理函数- (void)socket:(GCDAsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag { NSString *ip = [sock connectedHost]; uint16_t port = [sock connectedPort]; NSString *s = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; NSLog(@"接收到tcp [%@:%d] %@", ip, port, s); NSString *s2 = [NSString stringWithFormat:@"你发的数据是:%@", s]; NSData *databack = [s2 dataUsingEncoding:NSUTF8StringEncoding]; [sock writeData:databack withTimeout:60 tag:400]; [sock readDataWithTimeout:-1 tag:200];}- (void)socketDidDisconnect:(GCDAsyncSocket *)sock withError:(NSError *)err { NSLog(@"失去连接 %@", err); [allClientArray removeObject:sock];}- (void) showNavItem { UIBarButtonItem *sendMyself = [[UIBarButtonItem alloc] initWithTitle:@"发送自己" style:UIBarButtonItemStylePlain target:self action:@selector(sendMyself)]; self.navigationItem.rightBarButtonItem = sendMyself;}- (void) sendMyself { SendViewController *svc = [[SendViewController alloc] init]; [self.navigationController pushViewController:svc animated:YES];}
#import <UIKit/UIKit.h>#import "GCDAsyncSocket.h"@interface SendViewController : UIViewController <GCDAsyncSocketDelegate> { GCDAsyncSocket *sendTcpSocket;}@end
#import "SendViewController.h"#import "MBProgressHUD.h"@interface SendViewController ()@end@implementation SendViewController- (void)viewDidLoad { [super viewDidLoad]; self.navigationItem.title = @"发送"; [self showNavItem]; [self createClientTcpSocket];}- (void) showNavItem { UIBarButtonItem *sendMsg = [[UIBarButtonItem alloc] initWithTitle:@"发送消息" style:UIBarButtonItemStylePlain target:self action:@selector(sendMsg)]; self.navigationItem.rightBarButtonItem = sendMsg;}- (void) createClientTcpSocket { dispatch_queue_t dQueue = dispatch_queue_create("client tdp socket", NULL); sendTcpSocket = [[GCDAsyncSocket alloc] initWithDelegate:self delegateQueue:dQueue socketQueue:nil]; NSString *host = @"10.0.161.119"; uint16_t port = 12345; [sendTcpSocket connectToHost:host onPort:port withTimeout:60 error:nil]; }#pragma mark - 代理方法表示连接成功/失败 回调函数- (void)socket:(GCDAsyncSocket *)sock didConnectToHost:(NSString *)host port:(uint16_t)port { NSLog(@"连接成功"); [sock readDataWithTimeout:-1 tag:200];}- (void)socketDidDisconnect:(GCDAsyncSocket *)sock withError:(NSError *)err { NSLog(@"连接失败 %@", err); NSString *host = @"10.0.161.119"; uint16_t port = 12345; [sendTcpSocket connectToHost:host onPort:port withTimeout:60 error:nil];}#pragma mark - 消息发送成功 代理函数- (void)socket:(GCDAsyncSocket *)sock didWriteDataWithTag:(long)tag { [MBProgressHUD hideHUDForView:self.view animated:YES]; NSLog(@"消息发送成功");}- (void)socket:(GCDAsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag { NSString *ip = [sock connectedHost]; uint16_t port = [sock connectedPort]; NSString *s = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; NSLog(@"接收到服务器返回的数据 tcp [%@:%d] %@", ip, port, s);}- (void) sendMsg { NSString *s = @"hello from yang"; NSData *data = [s dataUsingEncoding:NSUTF8StringEncoding]; [MBProgressHUD showHUDAddedTo:self.view animated:YES]; [sendTcpSocket writeData:data withTimeout:60 tag:100];}- (void)dealloc { NSLog(@"dealloc"); [sendTcpSocket disconnect]; sendTcpSocket = nil;}@end