字真的好难看了
Socket 通讯流程图
主bundle栏
main.m
#import <Foundation/Foundation.h>
#import "MDServiceListener.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
//1.创建服务,监听对象:
MDServiceListener *listener = [[MDServiceListener alloc] init] ;
//2.开启监听:
[listener begin] ;
//3.开启运行循环,让服务端的socket不能停止:
[[NSRunLoop mainRunLoop] run] ;
}
return 0;
}
MDServiceListener.h
#import <Foundation/Foundation.h>
@interface MDServiceListener : NSObject
//开启服务:
- (void)begin ;
@end
MDServiceListener.m
#import "MDServiceListener.h"
#import "GCDAsyncSocket.h"
@interface MDServiceListener () <GCDAsyncSocketDelegate>
//serverSocket:
@property (nonatomic, strong) GCDAsyncSocket *serverSocket ;
//clientSocketArray:
@property (nonatomic, strong) NSMutableArray *clientSocketArray ;
@end
@implementation MDServiceListener
#pragma mark - lazyLoad
- (NSMutableArray *)clientSocketArray {
if (_clientSocketArray == nil) {
_clientSocketArray = [NSMutableArray array] ;
}
return _clientSocketArray ;
}
- (void)begin {
// 开启10086服务:端口号:5288
// 1.创建socket对象:
//服务端的socket只监听有没有客户端请求链接:
GCDAsyncSocket *serverSocket = [[GCDAsyncSocket alloc] initWithDelegate:self delegateQueue:dispatch_get_global_queue(0, 0)] ;
// 2.bind绑定端口并开启监听:
NSError *error = nil ;
[serverSocket acceptOnPort:5288 error:&error] ;
if (error == nil) {
NSLog(@"10086服务开启成功") ;
} else {
//失败的原因是端口号被其他程序占用:
NSLog(@"10086服务开启失败---error = %@" , error) ;
}
self.serverSocket = serverSocket ;
}
#pragma mark - GCDAsyncSocketDelegate协议方法
#pragma mark - 有新的客户端的Socket连接到服务器
- (void)socket:(GCDAsyncSocket *)sock didAcceptNewSocket:(GCDAsyncSocket *)newSocket {
NSLog(@"sock = %@" , sock) ;
NSLog(@"newSocket = %@" , newSocket) ;
// 1.保存客户端的socket:
// 防止 Connected to wintermiao.lan.情况发生,因为方法结束会提前释放 ;
// 把它放到一个数组里:
[self.clientSocketArray addObject:newSocket] ;
//提供服务:
NSMutableString *serviceString = [NSMutableString string] ;
[serviceString appendString:@"欢迎来到10086在线服务 , 请输入下面数字选择服务\n"] ;
[serviceString appendString:@"[0]在线充值\n"] ;
[serviceString appendString:@"[1]客服投诉\n"] ;
[serviceString appendString:@"[2]优惠信息\n"] ;
[serviceString appendString:@"[3]special service\n"] ;
[serviceString appendString:@"[4]退出服务\n"] ;
NSData *data = [serviceString dataUsingEncoding:NSUTF8StringEncoding] ;
[newSocket writeData:data withTimeout:-1 tag:0] ;
// 监听客户端有没有数据请求到服务器: --> -1:不超时 ;,tag值取 0 ,暂时用不到:
[newSocket readDataWithTimeout:-1 tag:0] ;
}
#pragma mark - 客户端发送数据在终端里实现,回车确认
#pragma mark - 服务器读取(监听)客户端发来的请求数据
- (void)socket:(GCDAsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag {
//NSData转NSString:
NSString *clientString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] ;
//处理请求,把数据返回给客户端:
NSInteger code = [clientString integerValue] ;
NSString *resposeStr = nil ;
switch (code) {
case 0:
resposeStr = @"充值服务暂停中...\n" ;
break;
case 1:
resposeStr = @"投诉服务暂停中...\n" ;
break;
case 2:
resposeStr = @"优惠信息没有...\n" ;
break;
case 3:
resposeStr = @"特殊服务也没有...\n" ;
break;
case 4:
resposeStr = @"退出服务...\n" ;
break;
default:
break;
}
NSData *data1 = [resposeStr dataUsingEncoding:NSUTF8StringEncoding] ;
NSLog(@"读取数据: 客户端的clien]tSocket = %@---data = %@ ---- clientString = %@" ,sock , data, clientString) ;
[sock writeData:data1 withTimeout:-1 tag:0] ;
//code == 4:退出服务:
if (code == 4) {
//从数组中移除客户端对象:
[_clientSocketArray removeObject:sock] ;
}
//每次读完数据后,都要调用一次监听数据的方法,否则点击回车之后再输入传输内容就不会被监听到了
[sock readDataWithTimeout:-1 tag:0] ;
}
@end