TCP UDP 套接字

TCP是全双工的,安全的,不过效率低,内存消耗大

UDP是半双工的,不安全,不过效率高,内存消耗小

在工程中导入三方库AsyncSocket

TCP是需要三次握手的

 1 - (void)viewDidLoad
 2 {
 3     [super viewDidLoad];
 4     _mArray = [[NSMutableArray alloc] init];
 5     //客户端
 6     _clientSocket = [[AsyncSocket alloc] initWithDelegate:self];
 7     //服务端
 8     _serverSocket = [[AsyncSocket alloc] initWithDelegate:self];
 9     
10     //监听有没有客户端连接
11     [_serverSocket acceptOnPort:5678 error:nil];
12 }
13 
14 
15 //监听到客户端连接
16 - (void)onSocket:(AsyncSocket *)sock didAcceptNewSocket:(AsyncSocket *)newSocket
17 {
18     [_mArray addObject:newSocket];
19     //监听客户端发送消息
20     [newSocket readDataWithTimeout:-1 tag:0];//Timeout连接超时,-1指的是永远不超时,永远等待
21     
22     
23 }
24 
25 //当监听到克华短发送了消息
26 - (void)onSocket:(AsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag
27 {
28     NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
29     if(_textView.text.length >= 100 ){
30         _textView.text = @"";
31     }
32     //消息显示到textView上面 sock.connectedHost客户端地址
33     _textView.text = [NSString stringWithFormat:@"%@%@:%@\n",_textView.text,sock.connectedHost,str];
34     //继续监听客户端发送消息
35     [sock readDataWithTimeout:-1 tag:0];//一直监听,递归
36 }
37 
38 //连接
39 - (void)conToHost:(id)sender
40 {
41     //如果已经连接,先断开
42     if(_clientSocket.isConnected){
43         [_clientSocket disconnect];
44     }
45     //连接服务端
46     [_clientSocket connectToHost:_ipField.text onPort:5678 withTimeout:20 error:nil];
47 }
48 
49 
50 //连接成功
51 - (void)onSocket:(AsyncSocket *)sock didConnectToHost:(NSString *)host port:(UInt16)port
52 {
53     NSLog(@"连接成功");
54 }
55 
56 
57 //发送
58 - (void)sendText:(id)sender
59 {
60     NSData *data = [_sendField.text dataUsingEncoding:NSUTF8StringEncoding];
61     _sendField.text = @"";
62     [_clientSocket writeData:data withTimeout:20 tag:0];
63     
64 }
65 
66 //发送成功
67 - (void)onSocket:(AsyncSocket *)sock didWriteDataWithTag:(long)tag
68 {
69     NSLog(@"发送成功");
70 }
71 
72 @end

 

UDP相对于TCP是简单的,不管服务器端在不在,直接发送信息

 1 - (void)viewDidLoad
 2 {
 3     [super viewDidLoad];
 4     //发送端
 5     _sendSocket = [[AsyncUdpSocket alloc] initWithDelegate:self];
 6     //绑定端口
 7     [_sendSocket bindToPort:5555 error:nil];
 8     
 9     //接收端
10     _recvSocket = [[AsyncUdpSocket alloc] initWithDelegate:self];
11     [_recvSocket bindToPort:6666 error:nil];
12     
13     //监听有没有人发送消息
14     [_recvSocket receiveWithTimeout:-1 tag:0];
15 }
16 
17 
18 
19 //接收端,监听到有人给它发消息
20 - (BOOL)onUdpSocket:(AsyncUdpSocket *)sock didReceiveData:(NSData *)data withTag:(long)tag fromHost:(NSString *)host port:(UInt16)port
21 {
22     //解析xml
23     GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithData:data options:0 error:nil];
24     //解析message
25     GDataXMLElement *messageEle = doc.rootElement;
26     //解析name
27     GDataXMLElement *nameEle = [[messageEle elementsForName:@"name"] lastObject];
28     //解析text
29     GDataXMLElement *textEle = [[messageEle elementsForName:@"text"] lastObject];
30     //显示出来
31     if(_textView.text.length >=100){
32         _textView.text = @"";
33     }
34     _textView.text = [NSString stringWithFormat:@"%@%@:%@\n",_textView.text,nameEle.stringValue,textEle.stringValue];
35     //继续监听
36     [_recvSocket receiveWithTimeout:-1 tag:0];
37     return YES;
38 }
39 
40 
41 //发送端
42 - (void)sendText:(id)sender
43 {
44     /*
45      <?xml>
46      <message>
47          <name>...</name>
48          <text>.....</text>
49      </message>
50      */
51     GDataXMLElement *messageEle = [GDataXMLElement elementWithName:@"message"];
52     GDataXMLElement *nameEle = [GDataXMLElement elementWithName:@"name" stringValue:@"iOS"];
53     GDataXMLElement *textEle = [GDataXMLElement elementWithName:@"text" stringValue:_sendField.text];
54     [messageEle addChild:nameEle];
55     [messageEle addChild:textEle];
56     
57     //加声明
58     GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithRootElement:messageEle];
59     [doc setCharacterEncoding:@"utf-8"];//编码格式
60     
61     //发送消息
62     [_sendSocket sendData:doc.XMLData toHost:_ipField.text port:6666 withTimeout:20 tag:0];
63 }
64 
65 //发送消息后
66 - (void)onUdpSocket:(AsyncUdpSocket *)sock didSendDataWithTag:(long)tag
67 {
68     
69 }
70 
71 
72 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
73 {
74     [self.view endEditing:YES];
75 }
76 
77 
78 @end

 

posted @ 2015-04-07 18:53  Angelone  阅读(154)  评论(0编辑  收藏  举报