AsyncSocket的简单封装使用

首先,关于含义的基本含义的介绍我就不多做介绍,基本原理就是三次握手,如果看不懂的可以加我扣扣向我咨询:1123231279

先将AsyncSocket文件夹下的类目导入工程中

然后开始自定义封装Socket流程:

1、创建一个单例类

(1)在.h文件中导入AsyncSocket.h和AsyncUdpSocket.h两个头文件

(2)创建单例类

(3)创建自定义协议,主要目的就是为了在tcp接收数据成功后返回接收到 数据,

(4)在.h文件中创建两个方法,一个是断开socket链接,一个是发送消息的内容。

代码示例:

#import

#import "AsyncUdpSocket.h"

#import "AsyncSocket.h"

@protocol TcpAsyncSocketDelegate

@optional

- (void)tcpAsySocket_Receive_Success:(NSString *)success;

@end

@interface TcpAsyncSocketManger : NSObject

@property(nonatomic,weak)id delegate;

@property(nonatomic,strong)AsyncSocket  *sendSocket;

//创建单例对象

+ (TcpAsyncSocketManger *)sharedInstance;

/*!

@brief 发送消息

@param code 发送的内容

*/

- (void)socketDidSendMessage:(NSString *)code;

/*!

@brief 断开连接

*/

- (void)socketDisconnect;

@end

在.m文件中,需要实现的代码:

首先,定义接口地址:

#pragma mark 接口地址

//socket 地址

#define SERVER_IP    @"app.baidu.com"

#define SERVER_PORT  7799

二、实现单例类

- (void)dealloc{

self.sendSocket = nil;

}

-(id)init{

if (self = [super init]){

[self initSocket];

}

return self;

}

+ (TcpAsyncSocketManger *)sharedInstance{

static TcpAsyncSocketManger *_tcpAsySocket = nil;

static dispatch_once_t onceToken;

dispatch_once(&onceToken, ^{

_tcpAsySocket = [[TcpAsyncSocketManger alloc]init];

});

return _tcpAsySocket;

}

三、链接到服务器

//SERVER_IP IP地址

//SERVER_PORT 端口地址

#pragma mark - 连接到服务器

- (void)initSocket{

if (self.sendSocket==nil){

self.sendSocket = [[AsyncSocket alloc] initWithDelegate: self];

}

static BOOL connectOK = NO;

NSError *error = nil;

connectOK = [self.sendSocket connectToHost: SERVER_IP onPort: SERVER_PORT error: &error];

if (!connectOK){

NSLog(@"connect error: %@", error);

}

[self.sendSocket setRunLoopModes:[NSArray arrayWithObject:NSRunLoopCommonModes]];

}

四、发送消息

#pragma mark - 发送消息

- (void)socketDidSendMessage:(NSString *)code{

if (self.sendSocket==nil){

self.sendSocket = [[AsyncSocket alloc] initWithDelegate: self];

static BOOL connectOK = NO;

NSError *error = nil;

connectOK = [self.sendSocket connectToHost: SERVER_IP onPort: SERVER_PORT error: &error];

if (!connectOK){

NSLog(@"connect error: %@", error);

}

[self.sendSocket setRunLoopModes:[NSArray arrayWithObject:NSRunLoopCommonModes]];

}

NSData *data = [code dataUsingEncoding: NSUTF8StringEncoding];

[self.sendSocket writeData: data withTimeout: -1 tag: 0];

}

五、断开链接

#pragma mark - 断开连接

- (void)socketDisconnect{

//断开连接时候一定要清空socket

[self.sendSocket disconnect];

self.sendSocket = nil;

}

六、TCP Delegate

#pragma mark - tcp delegate

// 与服务器建立连接时调用(连接成功)

- (void)onSocket:(AsyncSocket *)sock didConnectToHost:(NSString *)host port:(UInt16)port

{

//    NSLog(@"  :%s %d", __FUNCTION__, __LINE__);

//    //这里的host就是当前进行服务器连接的客户端ip

NSLog(@"host:%@",host);

[self.sendSocket readDataWithTimeout: -1 tag: 0];

}

- (void)onSocket:(AsyncSocket *)sock didWriteDataWithTag:(long)tag

{

//    NSLog(@"%s %d, tag = %ld", __FUNCTION__, __LINE__, tag);

[self.sendSocket readDataWithTimeout: -1 tag: 0];

}

// 读取客户端发送来的信息(收到socket信息时调用)

- (void)onSocket:(AsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag

{

NSString *msg = [[NSString alloc] initWithData: data encoding:NSUTF8StringEncoding];

NSLog(@"\n************\tmsg = %@ \n**************", msg);

[self.delegate tcpAsySocket_Receive_Success:msg];

[self.sendSocket readDataWithTimeout: -1 tag: 0];

}

//连接socket出错时调用

//- (void)onSocket:(AsyncSocket *)sock willDisconnectWithError:(NSError *)err

//{

//    NSLog(@"%s %d, err = %@", __FUNCTION__, __LINE__, err);

//}

//

- (void)onSocketDidDisconnect:(AsyncSocket *)sock

{

NSLog(@"%s %d", __FUNCTION__, __LINE__);

self.sendSocket = nil;

//    [self initSocket];

}

调用的时候可以直接将方法以单例方式调用就好,基本的Socket的封装已完成,还望大神多多指教。

你可能感兴趣的:(AsyncSocket的简单封装使用)