7

下面的代码有什么问题?我使用 AsyncSocket 连接到 SOCKS 代理并在onSocketWillConnect委托方法上设置代理设置。如果我省略对的调用,CFReadStreamSetProperty套接字CFWriteStreamSetProperty连接将顺利进行。否则,我在没有可跟踪堆栈跟踪的已释放实例上得到 [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);
4

1 回答 1

1

从 CFReadStream 的文档中:

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

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

于 2012-11-22T13:04:00.957 回答