设置SOCKS代理配置后CFStream崩溃

时间:2012-08-22 09:50:10

标签: objective-c ios socks cfnetwork cfstream

以下代码有什么问题?我使用AsyncSocket连接到SOCKS代理并在onSocketWillConnect委托方法上设置代理设置。如果我省略对CFReadStreamSetPropertyCFWriteStreamSetProperty的调用,套接字连接将顺利进行。另外,我在一个没有可跟踪堆栈跟踪的解除分配的实例上得到[Not A Type retain](它可能与CFNetwork有关?)。有谁知道是什么给出了什么?

CFDictionaryRef proxyDict = CFNetworkCopySystemProxySettings();
CFMutableDictionaryRef socksConfig = CFDictionaryCreateMutableCopy(NULL, 0, proxyDict);
CFDictionarySetValue(socksConfig, kCFStreamPropertySOCKSProxyHost, CFSTR("192.168.1.148"));
CFDictionarySetValue(socksConfig, kCFStreamPropertySOCKSProxyPort, (__bridge CFNumberRef)[NSNumber numberWithInt:3129]);
CFDictionarySetValue(socksConfig, kCFStreamPropertySOCKSVersion, kCFStreamSocketSOCKSVersion4);

// set SOCKS for read streams
CFReadStreamRef readStream = [sock getCFReadStream];
if (!CFReadStreamSetProperty(readStream, kCFStreamPropertySOCKSProxy, socksConfig)) {
  CFStreamError error = CFReadStreamGetError(readStream);
  NSLog(@"[SEVERE] Web Socket Read Stream Error: %ld[%ld]", error.domain, error.error);
}

// set SOCKS for write stream
CFWriteStreamRef writeStream = [sock getCFWriteStream];
if (!CFWriteStreamSetProperty(writeStream, kCFStreamPropertySOCKSProxy, socksConfig)) {
  CFStreamError error = CFWriteStreamGetError(writeStream);
  NSLog(@"[SEVERE] Web Socket Write Stream Error: %ld[%ld]", error.domain, error.error);
}

// Release
CFRelease(socksConfig);
CFRelease(proxyDict);

1 个答案:

答案 0 :(得分:1)

来自CFReadStream的文档:

  

可以设置的属性配置流的行为,并且只能在特定时间修改,例如在流打开之前。 (事实上​​,除非另有说明,否则您应该假设您只能在打开流之前设置属性。)

onSocketWillConnect可能为时已晚,无法设置这些属性。