本文是根据iOS使用CocoaAsyncSocket框架socket通信的demo,自己做些笔记,方便自己阅读;如有冒犯,烦请和本人沟通,我将立即撤下文章.
客户端
//
// ViewController.m
// SocketClient
//
// Created by Edward on 16/6/24.
// Copyright © 2016年 Edward. All rights reserved.
//
#import "ViewController.h"
#import "GCDAsyncSocket.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UITextField *addressTF;
@property (weak, nonatomic) IBOutlet UITextField *portTF;
@property (weak, nonatomic) IBOutlet UITextField *messageTF;
@property (weak, nonatomic) IBOutlet UITextView *showMessageTF;
//客户端socket
@property (nonatomic) GCDAsyncSocket *clinetSocket;
@end
@implementation ViewController
#pragma mark - GCDAsynSocket Delegate
/**
链接成功
*/
- (void)socket:(GCDAsyncSocket *)sock didConnectToHost:(NSString *)host port:(uint16_t)port{
[self showMessageWithStr:@"链接成功"];
[self showMessageWithStr:[NSString stringWithFormat:@"服务器IP : %@", host]];
[self.clinetSocket readDataWithTimeout:-1 tag:0];
}
/**
收到消息
*/
- (void)socket:(GCDAsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag{
NSString *text = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
[self showMessageWithStr:text];
[self.clinetSocket readDataWithTimeout:-1 tag:0];
}
#pragma - 连接,发送,接收消息
//开始连接
- (IBAction)connectAction:(id)sender {
//2、连接服务器
[self.clinetSocket connectToHost:self.addressTF.text onPort:self.portTF.text.integerValue withTimeout:-1 error:nil];
}
//发送消息
- (IBAction)sendMessageAction:(id)sender {
NSData *data = [self.messageTF.text dataUsingEncoding:NSUTF8StringEncoding];
//withTimeout -1 :无穷大
//tag: 消息标记
[self.clinetSocket writeData:data withTimeout:-1 tag:0];
}
//接收消息
- (IBAction)receiveMessageAction:(id)sender {
[self.clinetSocket readDataWithTimeout:11 tag:0];
}
- (void)showMessageWithStr:(NSString *)str{
self.showMessageTF.text = [self.showMessageTF.text stringByAppendingFormat:@"%@\n", str];
}
#pragma - 其他的一些附加操作
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
[self.view endEditing:YES];
}
#pragma - lazy
-(GCDAsyncSocket *)clinetSocket{
if (_clinetSocket == nil) {
_clinetSocket = [[GCDAsyncSocket alloc]initWithDelegate:self delegateQueue:dispatch_get_main_queue()];
}
return _clinetSocket;
}
@end
服务器
//
// ViewController.m
// SocketServer
//
// Created by Edward on 16/6/24.
// Copyright © 2016年 Edward. All rights reserved.
//
#import "ViewController.h"
#import "GCDAsyncSocket.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UITextField *portF;
@property (weak, nonatomic) IBOutlet UITextField *messageTF;
@property (weak, nonatomic) IBOutlet UITextView *showContentMessageTV;
//服务器socket(开放端口,监听客户端socket的链接)
@property (nonatomic) GCDAsyncSocket *serverSocket;
//保护客户端socket
@property (nonatomic) GCDAsyncSocket *clientSocket;
@end
@implementation ViewController
#pragma -按钮的一些操作
//发送消息
- (IBAction)sendMessage:(id)sender {
NSData *data = [self.messageTF.text dataUsingEncoding:NSUTF8StringEncoding];
//withTimeout -1:无穷大,一直等
//tag:消息标记
[self.clientSocket writeData:data withTimeout:-1 tag:0];
}
//开始监听
- (IBAction)startReceive:(id)sender {
//2、开放哪一个端口
NSError *error = nil;
BOOL result = [self.serverSocket acceptOnPort:self.portF.text.integerValue error:&error];
if (result && error == nil) {
//开放成功
[self showMessageWithStr:@"开放成功"];
}
}
//接受消息,socket是客户端socket,表示从哪一个客户端读取消息
- (IBAction)ReceiveMessage:(id)sender {
[self.clientSocket readDataWithTimeout:11 tag:0];
}
- (void)showMessageWithStr:(NSString *)str{
self.showContentMessageTV.text = [self.showContentMessageTV.text stringByAppendingFormat:@"%@\n",str];
}
#pragma mark - 服务器socket Delegate
/**
连接成功
*/
- (void)socket:(GCDAsyncSocket *)sock didAcceptNewSocket:(GCDAsyncSocket *)newSocket{
//保存客户端的socket
self.clientSocket = newSocket;
[self showMessageWithStr:@"链接成功"];
[self showMessageWithStr:[NSString stringWithFormat:@"服务器地址:%@ -端口: %d", newSocket.connectedHost, newSocket.connectedPort]];
[self.clientSocket readDataWithTimeout:-1 tag:0];
}
/**
接收到消息
*/
- (void)socket:(GCDAsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag{
NSString *text = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
[self showMessageWithStr:text];
[self.clientSocket readDataWithTimeout:-1 tag:0];
}
#pragma - lazy
-(GCDAsyncSocket *)serverSocket{
if (_serverSocket == nil) {
_serverSocket = [[GCDAsyncSocket alloc] initWithDelegate:self delegateQueue:dispatch_get_main_queue()];
}
return _serverSocket;
}
@end
使用步骤
- 分别运行两个项目
- 在服务器段点击开始监听,出现开发成功
- 获取本机的IP地址,客户端输入地址,点击开始连接,显示链接成功
- 可以互相发消息了
服务器端
客户端
demo
SocketServer-master
SocketClient
参考文献
iOS使用CocoaAsyncSocket框架socket通信的demo