首先,關(guān)于含義的基本含義的介紹我就不多做介紹,基本原理就是三次握手,如果看不懂的可以加我扣扣向我咨詢:1123231279
先將AsyncSocket文件夾下的類(lèi)目導(dǎo)入工程中
然后開(kāi)始自定義封裝Socket流程:
1、創(chuàng)建一個(gè)單例類(lèi)
(1)在.h文件中導(dǎo)入AsyncSocket.h和AsyncUdpSocket.h兩個(gè)頭文件
(2)創(chuàng)建單例類(lèi)
(3)創(chuàng)建自定義協(xié)議,主要目的就是為了在tcp接收數(shù)據(jù)成功后返回接收到 數(shù)據(jù),
(4)在.h文件中創(chuàng)建兩個(gè)方法,一個(gè)是斷開(kāi)socket鏈接,一個(gè)是發(fā)送消息的內(nèi)容。
代碼示例:
#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;
//創(chuàng)建單例對(duì)象
+ (TcpAsyncSocketManger *)sharedInstance;
/*!
@brief 發(fā)送消息
@param code 發(fā)送的內(nèi)容
*/
- (void)socketDidSendMessage:(NSString *)code;
/*!
@brief 斷開(kāi)連接
*/
- (void)socketDisconnect;
@end
在.m文件中,需要實(shí)現(xiàn)的代碼:
首先,定義接口地址:
#pragma mark 接口地址
//socket 地址
#define SERVER_IP? ? @"app.baidu.com"
#define SERVER_PORT? 7799
二、實(shí)現(xiàn)單例類(lèi)
- (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;
}
三、鏈接到服務(wù)器
//SERVER_IP IP地址
//SERVER_PORT 端口地址
#pragma mark - 連接到服務(wù)器
- (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]];
}
四、發(fā)送消息
#pragma mark - 發(fā)送消息
- (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];
}
五、斷開(kāi)鏈接
#pragma mark - 斷開(kāi)連接
- (void)socketDisconnect{
//斷開(kāi)連接時(shí)候一定要清空socket
[self.sendSocket disconnect];
self.sendSocket = nil;
}
六、TCP Delegate
#pragma mark - tcp delegate
// 與服務(wù)器建立連接時(shí)調(diào)用(連接成功)
- (void)onSocket:(AsyncSocket *)sock didConnectToHost:(NSString *)host port:(UInt16)port
{
//? ? NSLog(@"? :%s %d", __FUNCTION__, __LINE__);
//? ? //這里的host就是當(dāng)前進(jìn)行服務(wù)器連接的客戶端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];
}
// 讀取客戶端發(fā)送來(lái)的信息(收到socket信息時(shí)調(diào)用)
- (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出錯(cuò)時(shí)調(diào)用
//- (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];
}
調(diào)用的時(shí)候可以直接將方法以單例方式調(diào)用就好,基本的Socket的封裝已完成,還望大神多多指教。