connect永远阻塞线程及解决方案

简介: connect永远阻塞线程及解决方案

手机升级到ios9后这两天测试发现应用前后台切换并且网络快速闪断出现2次connect永远阻塞线程的问题。

在远程连接服务器前后收到网络异常通知,导致connect永远阻塞,进而线程阻塞。当然在进行连接前要判断是否有网络,若没有连接就别去连接了,当然若你连接时网络正常,在connect连接响应返回前网络异常了,就会出现被永远阻塞吧!

最好的解决方案是干掉这个僵死的线程。用了很多方法杀不死ios的阻塞线程。

暂时的解决方案:若发现长连接线程永远就另一个长连接线程。毕竟connect永远阻塞极少见。

发现connect永远阻塞的情况,可以通过在连接时存储当前时间。

连接函数响应时设置为0,当用户操作或获取到位置信息时发现这个全局时间非0并且和当前时间的差达到100秒时说明长连接线程永远阻塞了。

connect函数将激发TCP的三次握手过程,在连接建立成功或者出错时返回,出错可能是下列这种情况:

若TCP客户没有收到SYN分节的响应,则返回ETIMEOUT错误,,举例来说,调用connect函数时,4.4BSD内核发送一个SYN,若无响应6s后再发送一个,若仍然无响应则等待24s后再发送一个,若等待了75s仍未响应则返回本错误。

            //将打开的socket设为非阻塞的,可以用fcntl(socket, F_SETFL, O_NDELAY)完成, 若网络有问题会75秒返回结果
            fcntl((int)socket, F_SETFL, O_NDELAY);
//            if ((NotReachable == [self getNetworkStatus]) || (ReachableUnknown == [self getNetworkStatus]))
//            {
//                 sleep(3);
//                break;
//            }
            _connectTime = (long long)[[NSDate date] timeIntervalSince1970];
            FLDDLogDebug(@"_connectTime = %lld\n", _connectTime);
            if((NotReachable == [self getNetworkStatus]) || (ReachableUnknown == [self getNetworkStatus]))
            {
                close(server_sock_fd);
                self.socketConnectStat = SOCKECT_CONNECT_ABNORMAL;
                curDate = [NSDate date];
                nowTime = (long long)[curDate timeIntervalSince1970];
                frontHearTime = nowTime;
                sleep(3);
                 _connectTime = 0;
                goto SKIP;
            }

//            nowTime = (long)[[NSDate date] timeIntervalSince1970];

            int i = connect(server_sock_fd, (struct sockaddr *)&server_addr, sizeof(struct sockaddr_in));
            FLDDLogDebug(@"connect after _connectTime = %lld\n", (long long)[[NSDate date] timeIntervalSince1970]);
            _connectTime = 0;

长连接被永远被阻塞的日志:

015/10/19 12:08:36:127  SingleAsyncSocket.m:__40-[SingleAsyncSocket creatPushConnection]_block_invoke:1574 Debug:i= 0
2015/10/19 12:08:36:127  SingleAsyncSocket.m:__40-[SingleAsyncSocket creatPushConnection]_block_invoke:1579 Debug:ret2= 2
2015/10/19 12:08:36:127  SingleAsyncSocket.m:__40-[SingleAsyncSocket creatPushConnection]_block_invoke:1590 Debug:select 出错!
2015/10/19 12:08:36:127  SingleAsyncSocket.m:__40-[SingleAsyncSocket creatPushConnection]_block_invoke:1072 Debug:SKIP fd= 15
2015/10/19 12:08:36:127  Singleton.m:-[Singleton getLoginOrderStat]:224 Debug:函数
2015/10/19 12:08:36:130  SingleAsyncSocket.m:__40-[SingleAsyncSocket creatPushConnection]_block_invoke:1154 Debug:socket sucess 
2015/10/19 12:08:36:130  SingleAsyncSocket.m:__40-[SingleAsyncSocket creatPushConnection]_block_invoke:1155 Debug:g_fd= 9
2015/10/19 12:08:36:130  SingleAsyncSocket.m:__40-[SingleAsyncSocket creatPushConnection]_block_invoke:1160 Debug:main loop 
2015/10/19 12:08:36:131  Singleton.m:-[Singleton getLoginOrderStat]:224 Debug:函数
2015/10/19 12:08:36:131  SingleAsyncSocket.m:-[SingleAsyncSocket getNetworkStatus]:53 Debug:函数
2015/10/19 12:08:36:312  AppDelegate.m:-[AppDelegate applicationWillEnterForeground:]:426 Debug:函数
2015/10/19 12:08:36:313  Singleton.m:-[Singleton setBackgroundFlag:]:1001 Debug:函数
2015/10/19 12:08:36:313  SingleAsyncSocket.m:-[SingleAsyncSocket checkNetWork]:37 Debug:函数
2015/10/19 12:08:36:326  mainViewController.m:-[mainViewController ActiveRotationAnimationFromBackground]:813 Debug:函数
2015/10/19 12:08:36:326  NoticeCarView.m:-[NoticeCarView roteanmation]:31 Debug:函数
2015/10/19 12:08:36:360  SingleAsyncSocket.m:-[SingleAsyncSocket reachabilityDidChanged:]:945 Debug:函数
2015/10/19 12:08:36:360  SingleAsyncSocket.m:-[SingleAsyncSocket setNetworkStatus:]:59 Debug:函数
2015/10/19 12:08:36:360  SingleAsyncSocket.m:-[SingleAsyncSocket reachabilityDidChanged:]:949 Debug:handleNetChanged
2015/10/19 12:08:36:360  SingleAsyncSocket.m:-[SingleAsyncSocket reachabilityDidChanged:]:953 Info:收到网络异常通知,没有网络! 网络状态:0
2015/10/19 12:08:36:360  StartOrderViewController.m:-[StartOrderViewController reachabilityNotNet:]:137 Debug:函数
2015/10/19 12:08:36:360  mainViewController.m:-[mainViewController reachabilityNotNet:]:327 Debug:函数
2015/10/19 12:08:36:410  mainViewController.m:-[mainViewController mapView:didUpdateUserLocation:updatingLocation:]:1340 Debug:userLocation.coordinate latitude: 30.208424, longitude:120.219590, horizontalAccuracy:65.000000, speed:-1.000000
2015/10/19 12:08:36:512  mainViewController.m:-[mainViewController mapView:didUpdateUserLocation:updatingLocation:]:1340 Debug:userLocation.coordinate latitude: 30.206766, longitude:120.217200, horizontalAccuracy:1414.000000, speed:-1.000000
2015/10/19 12:08:36:726  mainViewController.m:-[mainViewController mapView:didUpdateUserLocation:updatingLocation:]:1340 Debug:userLocation.coordinate latitude: 30.208904, longitude:120.218935, horizontalAccuracy:65.000000, speed:-1.000000
2015/10/19 12:08:37:373  AppDelegate.m:-[AppDelegate applicationDidBecomeActive:]:444 Debug:函数
2015/10/19 12:08:37:373  AppDelegate.m:-[AppDelegate startSdkWith:appKey:appSecret:]:538 Debug:函数
2015/10/19 12:08:44:476  AppDelegate.m:-[AppDelegate applicationDidEnterBackground:]:416 Debug:函数
2015/10/19 12:08:44:478  Singleton.m:-[Singleton setBackgroundFlag:]:1001 Debug:函数
2015/10/19 12:08:44:478  Singleton.m:-[Singleton clearBackgroundChangeTime]:1029 Debug:函数
2015/10/19 12:08:49:513  mainViewController.m:-[mainViewController mapView:didUpdateUserLocation:updatingLocation:]:1340 Debug:userLocation.coordinate latitude: 30.209027, longitude:120.218747, horizontalAccuracy:65.000000, speed:-1.000000
2015/10/19 12:08:52:439  SingleAsyncSocket.m:-[SingleAsyncSocket reachabilityDidChanged:]:945 Debug:函数
2015/10/19 12:08:52:439  SingleAsyncSocket.m:-[SingleAsyncSocket setNetworkStatus:]:59 Debug:函数
2015/10/19 12:08:52:439  SingleAsyncSocket.m:-[SingleAsyncSocket reachabilityDidChanged:]:949 Debug:handleNetChanged
2015/10/19 12:08:52:440  SingleAsyncSocket.m:-[SingleAsyncSocket reachabilityDidChanged:]:959 Debug:收到网络正常通知。 网络状态:1
2015/10/19 12:08:52:440  StartOrderViewController.m:-[StartOrderViewController reachabilityRecove:]:151 Debug:函数
2015/10/19 12:08:52:440  mainViewController.m:-[mainViewController reachabilityRecove:]:336 Debug:函数
2015/10/19 12:08:52:576  SingleAsyncSocket.m:-[SingleAsyncSocket reachabilityDidChanged:]:945 Debug:函数
2015/10/19 12:08:52:576  SingleAsyncSocket.m:-[SingleAsyncSocket setNetworkStatus:]:59 Debug:函数
2015/10/19 12:08:52:577  SingleAsyncSocket.m:-[SingleAsyncSocket reachabilityDidChanged:]:949 Debug:handleNetChanged
2015/10/19 12:08:52:577  SingleAsyncSocket.m:-[SingleAsyncSocket reachabilityDidChanged:]:959 Debug:收到网络正常通知。 网络状态:1
2015/10/19 12:08:52:577  StartOrderViewController.m:-[StartOrderViewController reachabilityRecove:]:151 Debug:函数
2015/10/19 12:08:52:577  mainViewController.m:-[mainViewController reachabilityRecove:]:336 Debug:函数
2015/10/19 12:08:55:978  AppDelegate.m:-[AppDelegate applicationWillEnterForeground:]:426 Debug:函数
2015/10/19 12:08:55:978  Singleton.m:-[Singleton setBackgroundFlag:]:1001 Debug:函数
2015/10/19 12:08:55:978  SingleAsyncSocket.m:-[SingleAsyncSocket checkNetWork]:37 Debug:函数
2015/10/19 12:08:55:991  mainViewController.m:-[mainViewController ActiveRotationAnimationFromBackground]:813 Debug:函数
2015/10/19 12:08:55:991  NoticeCarView.m:-[NoticeCarView roteanmation]:31 Debug:函数
2015/10/19 12:08:56:017  SingleAsyncSocket.m:-[SingleAsyncSocket reachabilityDidChanged:]:945 Debug:函数
2015/10/19 12:08:56:018  SingleAsyncSocket.m:-[SingleAsyncSocket setNetworkStatus:]:59 Debug:函数
2015/10/19 12:08:56:018  SingleAsyncSocket.m:-[SingleAsyncSocket reachabilityDidChanged:]:949 Debug:handleNetChanged
2015/10/19 12:08:56:018  SingleAsyncSocket.m:-[SingleAsyncSocket reachabilityDidChanged:]:959 Debug:收到网络正常通知。 网络状态:1
2015/10/19 12:08:56:018  StartOrderViewController.m:-[StartOrderViewController reachabilityRecove:]:151 Debug:函数
2015/10/19 12:08:56:018  mainViewController.m:-[mainViewController reachabilityRecove:]:336 Debug:函数
2015/10/19 12:08:56:051  SingleAsyncSocket.m:-[SingleAsyncSocket reachabilityDidChanged:]:945 Debug:函数
2015/10/19 12:08:56:051  SingleAsyncSocket.m:-[SingleAsyncSocket setNetworkStatus:]:59 Debug:函数
2015/10/19 12:08:56:051  SingleAsyncSocket.m:-[SingleAsyncSocket reachabilityDidChanged:]:949 Debug:handleNetChanged
2015/10/19 12:08:56:051  SingleAsyncSocket.m:-[SingleAsyncSocket reachabilityDidChanged:]:959 Debug:收到网络正常通知。 网络状态:1
2015/10/19 12:08:56:051  StartOrderViewController.m:-[StartOrderViewController reachabilityRecove:]:151 Debug:函数
2015/10/19 12:08:56:051  mainViewController.m:-[mainViewController reachabilityRecove:]:336 Debug:函数
2015/10/19 12:08:57:177  AppDelegate.m:-[AppDelegate applicationDidBecomeActive:]:444 Debug:函数
2015/10/19 12:08:57:177  AppDelegate.m:-[AppDelegate startSdkWith:appKey:appSecret:]:538 Debug:函数
2015/10/19 12:08:59:252  mainViewController.m:-[mainViewController mapView:didUpdateUserLocation:updatingLocation:]:1340 Debug:userLocation.coordinate latitude: 30.209522, longitude:120.218383, horizontalAccuracy:126.952225, speed:-1.000000
2015/10/19 12:09:06:976  AppDelegate.m:-[AppDelegate applicationDidEnterBackground:]:416 Debug:函数
2015/10/19 12:09:06:976  Singleton.m:-[Singleton setBackgroundFlag:]:1001 Debug:函数
2015/10/19 12:09:06:976  Singleton.m:-[Singleton clearBackgroundChangeTime]:1029 Debug:函数
2015/10/19 12:09:08:164  mainViewController.m:-[mainViewController mapView:didUpdateUserLocation:updatingLocation:]:1340 Debug:userLocation.coordinate latitude: 30.209256, longitude:120.218757, horizontalAccuracy:165.000000, speed:-1.000000
2015/10/19 12:09:10:590  mainViewController.m:-[mainViewController mapView:didUpdateUserLocation:updatingLocation:]:1340 Debug:userLocation.coordinate latitude: 30.209046, longitude:120.218733, horizontalAccuracy:65.000000, speed:-1.000000
2015/10/19 12:09:26:551  mainViewController.m:-[mainViewController mapView:didUpdateUserLocation:updatingLocation:]:1340 Debug:userLocation.coordinate latitude: 30.208799, longitude:120.218951, horizontalAccuracy:65.000000, speed:-1.000000
2015/10/19 12:10:05:491  mainViewController.m:-[mainViewController mapView:didUpdateUserLocation:updatingLocation:]:1340 Debug:userLocation.coordinate latitude: 30.208579, longitude:120.218952, horizontalAccuracy:65.000000, speed:-1.000000
2015/10/19 12:10:18:377  mainViewController.m:-[mainViewController mapView:didUpdateUserLocation:updatingLocation:]:1340 Debug:userLocation.coordinate latitude: 30.208350, longitude:120.219238, horizontalAccuracy:65.000000, speed:-1.000000
2015/10/19 12:10:24:871  mainViewController.m:-[mainViewController mapView:didUpdateUserLocation:updatingLocation:]:1340 Debug:userLocation.coordinate latitude: 30.208137, longitude:120.219243, horizontalAccuracy:65.000000, speed:-1.000000
2015/10/19 12:10:37:797  mainViewController.m:-[mainViewController mapView:didUpdateUserLocation:updatingLocation:]:1340 Debug:userLocation.coordinate latitude: 30.208284, longitude:120.219028, horizontalAccuracy:65.000000, speed:-1.000000
2015/10/19 12:10:50:739  mainViewController.m:-[mainViewController mapView:didUpdateUserLocation:updatingLocation:]:1340 Debug:userLocation.coordinate latitude: 30.208358, longitude:120.219260, horizontalAccuracy:65.000000, speed:-1.000000
2015/10/19 12:10:52:030  AppDelegate.m:-[AppDelegate applicationWillEnterForeground:]:426 Debug:函数
2015/10/19 12:10:52:030  Singleton.m:-[Singleton setBackgroundFlag:]:1001 Debug:函数


目录
相关文章
|
1月前
|
Java 数据库 Android开发
【专栏】Kotlin在Android开发中的多线程优化,包括线程池、协程的使用,任务分解、避免阻塞操作以及资源管理
【4月更文挑战第27天】本文探讨了Kotlin在Android开发中的多线程优化,包括线程池、协程的使用,任务分解、避免阻塞操作以及资源管理。通过案例分析展示了网络请求、图像处理和数据库操作的优化实践。同时,文章指出并发编程的挑战,如性能评估、调试及兼容性问题,并强调了多线程优化对提升应用性能的重要性。开发者应持续学习和探索新的优化策略,以适应移动应用市场的竞争需求。
|
1月前
|
消息中间件 Linux 调度
【Linux 进程/线程状态 】深入理解Linux C++中的进程/线程状态:阻塞,休眠,僵死
【Linux 进程/线程状态 】深入理解Linux C++中的进程/线程状态:阻塞,休眠,僵死
125 0
|
18天前
|
设计模式 安全 Java
Java 多线程系列Ⅳ(单例模式+阻塞式队列+定时器+线程池)
Java 多线程系列Ⅳ(单例模式+阻塞式队列+定时器+线程池)
|
19天前
|
算法 Java
Java多线程基础-13:一文阐明死锁的成因及解决方案
死锁是指多个线程相互等待对方释放资源而造成的一种僵局,导致程序无法正常结束。发生死锁需满足四个条件:互斥、请求与保持、不可抢占和循环等待。避免死锁的方法包括设定加锁顺序、使用银行家算法、设置超时机制、检测与恢复死锁以及减少共享资源。面试中可能会问及死锁的概念、避免策略以及实际经验。
21 1
|
25天前
|
监控 网络协议 iOS开发
程序退到后台的时候,所有线程被挂起,系统回收所有的socket资源问题及解决方案
程序退到后台的时候,所有线程被挂起,系统回收所有的socket资源问题及解决方案
35 0
|
1月前
|
前端开发 JavaScript UED
由于JavaScript是单线程的,因此在处理大量异步操作时,需要确保不会阻塞UI线程
【5月更文挑战第13天】JavaScript中的Promise和async/await常用于处理游戏开发中的异步操作,如加载资源、网络请求和动画帧更新。Promise表示异步操作的结果,通过.then()和.catch()处理回调。async/await作为Promise的语法糖,使异步代码更简洁,类似同步代码。在游戏循环中,使用async/await可清晰管理资源加载和更新,但需注意避免阻塞UI线程,并妥善处理加载顺序、错误和资源管理,以保证游戏性能和稳定性。
30 3
|
1月前
|
Java
【Java多线程】分析线程加锁导致的死锁问题以及解决方案
【Java多线程】分析线程加锁导致的死锁问题以及解决方案
34 1
|
1月前
|
存储 缓存 安全
【Java多线程】线程安全问题与解决方案
【Java多线程】线程安全问题与解决方案
31 1
|
1月前
|
存储 消息中间件 Java
Java多线程实战-异步操作日志记录解决方案(AOP+注解+多线程)
Java多线程实战-异步操作日志记录解决方案(AOP+注解+多线程)
|
1月前
|
监控
写一个线程来监控各线程是否发生阻塞
写一个线程来监控各线程是否发生阻塞
28 0