手机升级到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:函数