Lede-Inc / LDNetDiagnoService_IOS

IOS平台利用ping和traceroute的原理,对指定域名(通常为后台API的提供域名)进行网络诊断,并收集诊断日志。
MIT License
1.16k stars 270 forks source link

TCP 对于有问题的地址超时 #25

Closed chenfengfeng closed 5 years ago

chenfengfeng commented 6 years ago

如果tcp 一个地址:135.201.234.90

检查了代码CFSocketConnectToAddress, timeout 设置3秒 不起作用,并不会跳转到TCPServerConnectCallBack 等待时间差不多是1分多钟 请问这个算是 bug 吗?能解决吗?

wesleydotyang commented 5 years ago

原因是前几步创建失败,没有处理

-(BOOL)connectWithAddress:(NSData *)addr{
    struct sockaddr *pSockAddr = (struct sockaddr *)[addr bytes];
    int addressFamily = pSockAddr->sa_family;

    //创建套接字
    CFSocketContext CTX = {0, (__bridge_retained void *)(self), NULL, NULL, NULL};
    _socket = CFSocketCreate(kCFAllocatorDefault, addressFamily, SOCK_STREAM, IPPROTO_TCP,
                             kCFSocketConnectCallBack, TCPServerConnectCallBack, &CTX);

    if(!_socket){
        return NO;
    }

    //执行连接
    CFSocketError res = CFSocketConnectToAddress(_socket, (__bridge CFDataRef)addr, 3);
    if(res != kCFSocketSuccess){
        CFRelease(_socket);
        return NO;
    }