首页 > 代码库 > Socket在iOS客户端上的简单实现 - 利用GCAsyncSocket框架
Socket在iOS客户端上的简单实现 - 利用GCAsyncSocket框架
GCAsyncSocket 这是一个2003的开发出来的一个开源框架
首先把GCDAsyncSocket的.h和.m文件拖入到工程中
试图控制器遵守GCDAsyncSocketDelegate协议
实例:在页面上有一个简单的textView和textField 在textField中输入文字后,点击发送即可把文字发送到服务器
,服务器端返回的文字数据会显示到textView上
// 服务器主机ip地址
#define kHost @"172.16.3.101"
// 服务器主机通信端口
#define kPort 10024
@interface LYViewController ()<GCDAsyncSocketDelegate]]]]>
// 创建socket对象,进行通信
@property (nonatomic, strong) GCDAsyncSocket *socket;
@property (nonatomic, strong) GCDAsyncSocket *socketServer;
// 创建安装socket对象
- (void)_setupSocket;
- (void)_setupServerSocket;
//
- (void)_keyboardFrameChange:(NSNotification *)notification;
@end
#define kHost @"172.16.3.101"
// 服务器主机通信端口
#define kPort 10024
@interface LYViewController ()<GCDAsyncSocketDelegate]]]]>
// 创建socket对象,进行通信
@property (nonatomic, strong) GCDAsyncSocket *socket;
@property (nonatomic, strong) GCDAsyncSocket *socketServer;
// 创建安装socket对象
- (void)_setupSocket;
- (void)_setupServerSocket;
//
- (void)_keyboardFrameChange:(NSNotification *)notification;
@end
@implementation LYViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.textField.delegate = self;
[self _setupSocket];
[self _setupServerSocket];
}
- (BOOL)prefersStatusBarHidden
{
return YES;
}
#pragma mark - 键盘弹起 改变输入框的位置
#pragma mark 注册通知 改变输入框的位置
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:YES];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_keyboardFrameChange:) name:UIKeyboardWillChangeFrameNotification object:nil];
}
#pragma mark 移除通知
- (void)viewWillDisappear:(BOOL)animated
{
[super viewDidDisappear:YES];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillChangeFrameNotification object:nil];
}
#pragma mark 接收到键盘弹起通知相应的方法
- (void)_keyboardFrameChange:(NSNotification *)notification
{
CGRect textFieldRect = self.textField.frame;
NSDictionary *userInfo = notification.userInfo;
CGRect beginRect = [userInfo[UIKeyboardFrameBeginUserInfoKey] CGRectValue];
CGRect endRect = [userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
CGFloat tempY = CGRectGetMinY(endRect) - CGRectGetMinY(beginRect);
textFieldRect.origin.y += tempY;
[UIView animateWithDuration:.25 animations:^{
self.textField.frame = textFieldRect;
}];
}
#pragma mark 点击return 键盘收回 发送消息(写入数据)
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
NSString *dataString = [self.textField.text stringByAppendingString:@"\r\n"];
NSData *data = http://www.mamicode.com/[dataString dataUsingEncoding:NSUTF8StringEncoding];
[self.socket writeData:data withTimeout:-1 tag:0];
[textField resignFirstResponder];
return YES;
}
#pragma amrk - private methods
// 创建安装socket对象
-(void)_setupSocket
{
self.socket = [[GCDAsyncSocket alloc] initWithDelegate:self delegateQueue:dispatch_get_main_queue()];
// 连接到服务器
[self.socket connectToHost:kHost onPort:kPort error:nil];
}
// 创建服务器对象
- (void)_setupServerSocket
{
self.socketServer = [[GCDAsyncSocket alloc] initWithDelegate:self delegateQueue:dispatch_get_main_queue()];
[self.socket acceptOnPort:10024 error:nil];
}
#pragma mark - socket delegate
// 连接服务器成功
-(void)socket:(GCDAsyncSocket *)sock didConnectToHost:(NSString *)host port:(uint16_t)port
{
NSLog(@"%s", __FUNCTION__);
// 等待读取数据
[self.socket readDataToData:[GCDAsyncSocket CRLFData] withTimeout:-1 maxLength:NSIntegerMax tag:0];
}
// 读到数据的回调方法
- (void)socket:(GCDAsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag
{
NSString *content = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
self.textView.text = content;
}
- (void)socket:(GCDAsyncSocket *)sock didWriteDataWithTag:(long)tag
{
[self.socket readDataToData:[GCDAsyncSocket CRLFData] withTimeout:-1 maxLength:NSIntegerMax tag:0];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
- (void)viewDidLoad
{
[super viewDidLoad];
self.textField.delegate = self;
[self _setupSocket];
[self _setupServerSocket];
}
- (BOOL)prefersStatusBarHidden
{
return YES;
}
#pragma mark - 键盘弹起 改变输入框的位置
#pragma mark 注册通知 改变输入框的位置
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:YES];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_keyboardFrameChange:) name:UIKeyboardWillChangeFrameNotification object:nil];
}
#pragma mark 移除通知
- (void)viewWillDisappear:(BOOL)animated
{
[super viewDidDisappear:YES];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillChangeFrameNotification object:nil];
}
#pragma mark 接收到键盘弹起通知相应的方法
- (void)_keyboardFrameChange:(NSNotification *)notification
{
CGRect textFieldRect = self.textField.frame;
NSDictionary *userInfo = notification.userInfo;
CGRect beginRect = [userInfo[UIKeyboardFrameBeginUserInfoKey] CGRectValue];
CGRect endRect = [userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
CGFloat tempY = CGRectGetMinY(endRect) - CGRectGetMinY(beginRect);
textFieldRect.origin.y += tempY;
[UIView animateWithDuration:.25 animations:^{
self.textField.frame = textFieldRect;
}];
}
#pragma mark 点击return 键盘收回 发送消息(写入数据)
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
NSString *dataString = [self.textField.text stringByAppendingString:@"\r\n"];
NSData *data = http://www.mamicode.com/[dataString dataUsingEncoding:NSUTF8StringEncoding];
[self.socket writeData:data withTimeout:-1 tag:0];
[textField resignFirstResponder];
return YES;
}
#pragma amrk - private methods
// 创建安装socket对象
-(void)_setupSocket
{
self.socket = [[GCDAsyncSocket alloc] initWithDelegate:self delegateQueue:dispatch_get_main_queue()];
// 连接到服务器
[self.socket connectToHost:kHost onPort:kPort error:nil];
}
// 创建服务器对象
- (void)_setupServerSocket
{
self.socketServer = [[GCDAsyncSocket alloc] initWithDelegate:self delegateQueue:dispatch_get_main_queue()];
[self.socket acceptOnPort:10024 error:nil];
}
#pragma mark - socket delegate
// 连接服务器成功
-(void)socket:(GCDAsyncSocket *)sock didConnectToHost:(NSString *)host port:(uint16_t)port
{
NSLog(@"%s", __FUNCTION__);
// 等待读取数据
[self.socket readDataToData:[GCDAsyncSocket CRLFData] withTimeout:-1 maxLength:NSIntegerMax tag:0];
}
// 读到数据的回调方法
- (void)socket:(GCDAsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag
{
NSString *content = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
self.textView.text = content;
}
- (void)socket:(GCDAsyncSocket *)sock didWriteDataWithTag:(long)tag
{
[self.socket readDataToData:[GCDAsyncSocket CRLFData] withTimeout:-1 maxLength:NSIntegerMax tag:0];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。