GCDAsyncSocket 尝试通过不同端口连接服务器的解决方案
来源:互联网 发布:python snmpwalk 编辑:程序博客网 时间:2024/05/16 07:13
原先的设计是通过hostname和port来建立连接的:
[_asyncSocket connectToHost:hostname onPort:_host_port error:&err]
但是有这么一种情况,就是服务端有时候会把port改变掉,比如说它本来约定是用8001的,但是有时候进程被意外杀死之后,8001这个port就申请不到了,只好用8002这个端口,那么在客户端是不知道的,因此要求客户端在连接服务器时,先尝试用8001去连接,假如连接不成功,则改为用8002去连接。
这里面有一个难题,就是在GCDAsyncSocket的connectToHost这个方法里,不能马上知道成功或者失败,因为它的执行是非阻塞的,它通常会先返回成功,即使将来它会连不上,那么它连不上时会不会通过别的delegation来通知用户呢?
为了这个问题,特别研究了一下它里面的实现流程:
在
- (void)closeWithError:(NSError *)error
这个方法里面有:
// If the client has passed the connect/accept method, then the connection has at least begun.// Notify delegate that it is now ending.BOOL shouldCallDelegate = (flags & kSocketStarted) ? YES : NO;BOOL isDeallocating = (flags & kDealloc) ? YES : NO;// Clear stored socket info and all flags (config remains as is)socketFDBytesAvailable = 0;flags = 0;sslWriteCachedLength = 0;if (shouldCallDelegate){__strong id theDelegate = delegate;__strong id theSelf = isDeallocating ? nil : self;if (delegateQueue && [theDelegate respondsToSelector: @selector(socketDidDisconnect:withError:)]){dispatch_async(delegateQueue, ^{ @autoreleasepool {[theDelegate socketDidDisconnect:theSelf withError:error];}});}}
从这里可以看到,当连接失败时,它会呼叫
socketDidDisconnect:withError:
这个代理方法的,因此解决的方案就出来了,可以实现这个方法,然后再在里面重新用新的端口来建立连接
-(void)socketDidDisconnect:(GCDAsyncSocket *)sock withError:(NSError *)err{ NSLog(@"socketDidDisconnect port:%d:%@",[sock localPort],err); 为了谨慎起见,只处理错误号为61的情况 if (err.code == 61) { //hu:handle port change to 8002 NSLog(@"connection refused"); _host_port = 8002; [self connectServer]; }}
经过试验,这样的方式是可行的。
1 0
- GCDAsyncSocket 尝试通过不同端口连接服务器的解决方案
- windows服务器端口连接过多的一种解决方案
- 单IP通过不同端口访问不同服务器
- 通过ip地址及端口连接服务器
- “通过端口 1433 连接到主机 localhost 的 TCP/IP 连接失败“常见问题的解决方案
- 问题:尝试通过外部 USB 端口将设备连接到基于 Windows XP 的台式机时,该设备没有建立连接。
- 尝试了所有的服务器都无法成功连接服务器。。。。。
- SQL Server 2005通过端口1433连接到主机127.0.0.1的TCP/IP连接失败解决方案
- 服务器80端口被占用的解决方案
- ssh命令通过制定端口连接远程服务器
- 通过指定的端口进行远程连接
- SMTP 服务器要求安全连接或客户端未通过身份验证的各个解决方案(C#)
- SMTP 服务器要求安全连接或客户端未通过身份验证的各个解决方案(C#)
- 在tomcat下,实现通过端口访问不同的网站
- git 连接不同的服务器地址
- git 连接不同的服务器地址
- windows服务器部署多个不同端口的tomcat
- sqlserver 2000 远程连接 服务器的解决方案
- 内部类,匿名内部类讲解
- 一个股票软件开发了三年的人如何画制iOS k线图
- CentOS7.2安装JDK1.7
- ssm框架学习---开发中使用springMVC接收参数的问题
- hbase乱码问题解决
- GCDAsyncSocket 尝试通过不同端口连接服务器的解决方案
- Android Service服务
- 工作笔录
- openSUSE 国内镜像和镜像使用帮助
- FFmpeg的Android平台移植—编译篇
- C程序设计 例题 判读某一年是否为闰年(2)
- gdb调试程序
- 线程池浅谈
- MVP模式在Android开发中的应用