//
// ViewController.m
// TestCocoaAsyncSocket
//
// Created by dj on 2017/6/15.
// Copyright © 2017年 djlovettt. All rights reserved.
//
#import "ViewController.h"
#import "GCDAsyncSocket.h"
@interface ViewController ()<GCDAsyncSocketDelegate>
@property (nonatomic, strong) GCDAsyncSocket *clientSocket;
@property (nonatomic, strong) NSTimer *connectTimer;
@property (nonatomic, assign) BOOL connected;
@property (nonatomic, assign) NSInteger port;
@property (nonatomic, copy) NSString *hostAddress;
@property (nonatomic, copy) NSString *sendMessage;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
_port = 8080;
_hostAddress = @"192.***.1.***";
_sendMessage = @"这是一条测试数据";
}
/**
开始进行 socket 连接
*/
- (void)startConnect {
if (!self.connected) {
self.clientSocket = [[GCDAsyncSocket alloc] initWithDelegate:self delegateQueue:dispatch_get_main_queue()];
NSLog(@"开始进行 socket 连接");
NSError *error = nil;
self.connected = [self.clientSocket connectToHost:_hostAddress onPort:_port viaInterface:nil withTimeout:-1 error:&error];
if (self.connected) {
NSLog(@"正在尝试连接");
} else {
NSLog(@"客户端未创建连接");
}
} else {
NSLog(@"socket 连接成功");
}
}
/**
向服务器端发送消息
*/
- (void)sendMessageAction {
NSData *data = [_sendMessage dataUsingEncoding:NSUTF8StringEncoding];
//withTimeout:-1 表示一直等待
//tag 消息标记
[self.clientSocket writeData:data withTimeout:-1 tag:0];
}
/**
连接服务器对应的端口号
@param sock 客户端的 socket
@param host 主机 IP
@param port 端口号
*/
- (void)socket:(GCDAsyncSocket *)sock didConnectToHost:(NSString *)host port:(uint16_t)port {
//连接成功后开启定时器
[self addTimer];
[self.clientSocket readDataWithTimeout:-1 tag:0];
self.connected = YES;
}
- (void)addTimer {
self.connectTimer = [NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector(longConnectToSocket) userInfo:nil repeats:YES];
//将定时器添加到 runloop 里
[[NSRunLoop currentRunLoop] addTimer:self.connectTimer forMode:NSRunLoopCommonModes];
}
- (void)longConnectToSocket {
//发送固定内容至服务器
float version = [[UIDevice currentDevice] systemVersion].floatValue;
NSString *versionStr = [NSString stringWithFormat:@"djlovett_%.2f",version];
NSData *data = [versionStr dataUsingEncoding:NSUTF8StringEncoding];
[self.clientSocket writeData:data withTimeout:-1 tag:0];
}
/**
读取接收到的数据
@param sock 客户端 socket
@param data 接收到的数据
@param tag 数据标记
*/
- (void)socket:(GCDAsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag {
NSString *content = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"接收到的数据为 === %@",content);
//读取到服务器数据数值后,可以再次读取
[self.clientSocket readDataWithTimeout:-1 tag:0];
}
/**
客户端断开连接
@param sock 客户端socket
@param err 错误信息
*/
- (void)socketDidDisconnect:(GCDAsyncSocket *)sock withError:(NSError *)err {
NSLog(@"socket 断开连接");
NSLog(@"错误信息描述: %@",err.localizedDescription);
self.clientSocket.delegate = nil;
self.clientSocket = nil;
self.connected = NO;
[self.connectTimer invalidate];
}
@end
GCDAsyncSocket-客户端
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- packagework11; importjava.io.*; importjava.net.*; public ...
- 1)资源方面:200个客户在1台客户端会占用很多资源 2)线程方面:200个客户在1台客户端会产生线程干扰 3)带...
- 前奏:socket两套库,GCDAsyncSocket,GCDAsyncUdpSocket和AsyncSocket...
- 记录是一种精神,是加深理解最好的方式之一。 最近深入研究了Jedis的源码,对Jedis的使用进行深入理解,提笔记...