GCDAsyncSocket 实现通信
github URL: https://github.com/LiuRuian/ReferenceCode
需要三个文件
SocketServerWindow 服务端代码文件
GCDAsyncSocket_Code 客户端1代码文件
OtherGCDAsyncSocket_Code 客户端2代码文件
1. 实现通信需要启动
SocketServerWindow 项目的服务器
GCDAsyncSocket_Code 代码
#import "ViewController.h"
@interface ViewController ()
{
GCDAsyncSocket *socket;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (IBAction)coonetButAction:(UIButton *)sender {
// 1. 初始化scoket
socket = [[GCDAsyncSocket alloc] initWithDelegate:self delegateQueue:dispatch_get_main_queue()];
// 2. 配置scoket的服务器地址和端口
NSError *error = nil;
if (![socket connectToHost:@"127.0.0.1" onPort:4000 error:&error]) {
NSLog(@"error = %@",error);
}else
{
NSLog(@"存在IP和端口");
}
}
- (void)socket:(GCDAsyncSocket *)sock didConnectToHost:(NSString *)host port:(uint16_t)port
{
NSLog(@"连接成功");
NSLog(@"host = %@,port = %d",host,port);
// 3. 连接成功后开始读取,读取后有数据会调用read的代理方法
// - (void)socket:(GCDAsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag
[socket readDataWithTimeout:-1 tag:0];
// [socket readDataToLength:1024 withTimeout:-1 tag:2];
/**
* Reads the given number of bytes. 第一个参数:给读取的字节
*
* If the timeout value is negative, the read operation will not use a timeout.
* 第二个参数: 如果超时的值一个负数,读取操作将不使用超时
* If the length is 0, this method does nothing and the delegate is not called.
第三个参数: 如果为0,这个方法什么都不做和代理方法也不会被回掉
**/
/*
这样读请求会自动进入了队列,当socket 连接上后,请求会自动出队被执行。(这里假设先读一个包头,再根据长度接收包体,这个读操作完成后 socket:didReadData:withTag: 会被调用)
*/
/*当然我们可以投递多个读写操作而不必等待上一个完成.*/
}
// 4.读取另一端发送的数据
- (void)socket:(GCDAsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag
{
NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"str = %@",str);
[socket readDataWithTimeout:-1 tag:0];
}
// 5. 写入操作
- (IBAction)writeBtnAction:(UIButton *)sender {
NSData *data = [@"clientMessage" dataUsingEncoding:NSUTF8StringEncoding];
// 写入到服务端
[socket writeData:data withTimeout:-1 tag:0];
[socket readDataWithTimeout:-1 tag:0];
}
@interface ViewController ()
{
GCDAsyncSocket *listensocket;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
- (IBAction)connetBtnAction:(UIButton *)sender {
// 1. 创建 GCDAsyncSocket
listensocket = [[GCDAsyncSocket alloc] initWithDelegate:self delegateQueue:dispatch_get_main_queue()];
NSError *error = nil;
// 2. GCDAsyncSocket 创建一个服务器,并接受传入的连接
if (![listensocket acceptOnPort:4000 error:&error])
{
NSLog(@"error: %@", error);
}else
{
NSLog(@"开始监听端口");
}
}
- (void)socket:(GCDAsyncSocket *)sock didAcceptNewSocket:(GCDAsyncSocket *)newSocket
{
// The "sock" parameter is the listenSocket we created.
// 这个sock 的参数是一个我们创建的 listensocket
// The "newSocket" is a new instance of GCDAsyncSocket.
// 这个newSocket是一个新的Socket
// It represents the accepted incoming client connection.
// 它代表着接受传入的客户端连接
listensocket = newSocket;
[listensocket readDataWithTimeout:-1 tag:0];
}
// 读取出入的数据
- (void)socket:(GCDAsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag
{
NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"str = %@",str);
[listensocket readDataWithTimeout:-1 tag:0];
}
- (IBAction)sendMessageBtnAction:(UIButton *)sender {
NSData *data = [@"severMessage" dataUsingEncoding:NSUTF8StringEncoding];
// 写入到服务端
[listensocket writeData:data withTimeout:-1 tag:0];
[listensocket readDataWithTimeout:-1 tag:0];
}
@end