前段时间写手机端的socket通讯,因为需要测试,所以用“GCDAsyncSocket”临时写了个socketServer,可以拿自己的socket通讯去测试
Magic.gif demo地址: https://github.com/joymakee/macSocketServer.git主要功能如下
一款用于测试socket数据的mac服务器 app
1.打开应用后,会自动获取本机ip并显示,端口默认8080
2.点击"开始监控",进行端口8080的数据监听
3.当服务器接收到数据后会显示来源地址、端口以及数据内容,如果有多个socket连接到本服务器,则会进行数据转发 比如a、b、c同时连接socket服务器,那么 当a向服务器传送数据时,服务器会将数据转发给b、c,同理b发送数据时a和c也可收到
//.h文件
#import <Foundation/Foundation.h>
@class GCDAsyncSocket;
typedef void (^SocketBlock)(GCDAsyncSocket *client,NSString *message);
@interface SerViceAPP : NSObject
+ (instancetype)shareInstance;
-(void)openSerVice;
- (void)closeService;
@property (nonatomic,copy) SocketBlock messageBlock;
@end
//.m文件
#import "SerViceAPP.h"
#import "GCDAsyncSocket.h"
#import "GCDAsyncSocket+category.h"
@interface SerViceAPP()<GCDAsyncSocketDelegate>
@property(nonatomic, strong)GCDAsyncSocket *serve;
@property(nonatomic, strong)NSMutableArray *socketConnectsM;
@property(nonatomic, strong)NSThread *checkThread;
@end
static SerViceAPP *instance;
@implementation SerViceAPP
+ (instancetype)shareInstance{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
instance = [[super alloc] init];
});
return instance;
}
-(NSThread *)checkThread{
return _checkThread = _checkThread?:[[NSThread alloc]initWithTarget:self selector:@selector(checkClientOnline) object:nil];
}
-(GCDAsyncSocket *)serve{
if (!_serve) {
_serve = [[GCDAsyncSocket alloc] initWithDelegate:self delegateQueue:dispatch_get_global_queue(0, 0)];
_serve.delegate = self;
}
return _serve;
}
-(NSMutableArray *)socketConnectsM{
return _socketConnectsM= _socketConnectsM?:[NSMutableArray array];
}
-(void)openSerVice{
NSError *error;
if (self.serve.isDisconnected) {
[self.checkThread start];
BOOL sucess = [self.serve acceptOnPort:8088 error:&error];
NSLog(sucess?@"端口开启成功,并监听客户端请求连接...":@"端口开启失...");
}
}
- (void)closeService{
if (!self.checkThread.isCancelled) {
[self.checkThread cancel];
}
if (self.serve.isConnected) {
@synchronized (_serve) {
[self.serve disconnect];
}
}
}
#pragma delegate
- (void)socket:(GCDAsyncSocket *)serveSock didAcceptNewSocket:(GCDAsyncSocket *)clientSocket{
//建立链接 并保存socket以供转发使用
NSLog(@"%@ IP: %@: %zd 客户端请求连接...",clientSocket,clientSocket.connectedHost,clientSocket.connectedPort);
// 1.将客户端socket保存起来
clientSocket.timeNew = [NSDate date];
[self.socketConnectsM addObject:clientSocket];
[clientSocket readDataWithTimeout:-1 tag:0];
}
- (void)socket:(GCDAsyncSocket *)clientSocket didReadData:(NSData *)data withTag:(long)tag {
//收到数据进行数据转发
NSString *clientStr = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
![clientStr isEqualToString:@"heart"] && clientStr.length!=0 &&self.messageBlock?self.messageBlock(clientSocket,clientStr):nil;
for (GCDAsyncSocket *socket in self.socketConnectsM) {
if (![clientSocket isEqual:socket]) {
//群聊 发送给其他客户端
if(![clientStr isEqualToString:@"heart"] && clientStr.length!=0)
{
[self writeDataWithSocket:socket str:clientStr];
}
}
else{socket.timeNew = [NSDate date];}
}
[clientSocket readDataWithTimeout:-1 tag:0];
}
- (void)socketDidDisconnect:(GCDAsyncSocket *)sock withError:(NSError *)err{
//链接断开 ,人为或者异常,是否需要重连逻辑
NSLog(@"又下线");
[self.socketConnectsM enumerateObjectsUsingBlock:^(GCDAsyncSocket *client, NSUInteger idx, BOOL * _Nonnull stop) {
if([client isEqual:sock]){
[self.socketConnectsM removeObject:client];
*stop = YES;
}
}];
}
-(void)exitWithSocket:(GCDAsyncSocket *)clientSocket{
[self writeDataWithSocket:clientSocket str:@"成功退出\n"];
[self.socketConnectsM enumerateObjectsUsingBlock:^(GCDAsyncSocket *client, NSUInteger idx, BOOL * _Nonnull stop) {
if([client isEqual:clientSocket]){
[self.socketConnectsM removeObject:client];
*stop = YES;
}
}];
NSLog(@"当前在线用户个数:%ld",self.socketConnectsM.count);
}
- (void)socket:(GCDAsyncSocket *)sock didWriteDataWithTag:(long)tag{
NSLog(@"数据发送成功..");
}
- (void)writeDataWithSocket:(GCDAsyncSocket*)clientSocket str:(NSString*)str{
[clientSocket writeData:[str dataUsingEncoding:NSUTF8StringEncoding] withTimeout:-1 tag:0];
}
#pragma checkTimeThread
//开启线程 启动runloop 循环检测客户端socket最新time
- (void)checkClientOnline{
@autoreleasepool {
[NSTimer scheduledTimerWithTimeInterval:35 target:self selector:@selector(repeatCheckClinetOnline) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop]run];
}
}
//移除 超过心跳的 client
- (void)repeatCheckClinetOnline{
if (self.socketConnectsM.count == 0) {
return;
}
NSDate *date = [NSDate date];
NSMutableArray *arrayNew = [NSMutableArray array];
for (GCDAsyncSocket *socket in self.socketConnectsM ) {
if ([date timeIntervalSinceDate:socket.timeNew]>30) {
continue;
}
[arrayNew addObject:socket ];
}
self.socketConnectsM = arrayNew;
}
客户端的跟这差不多,http://www.jianshu.com/p/c73ab1cb9243
网友评论