0

我正在使用以下代码打开将文件发送到 FTP 的流。我需要的是从中取出套接字,以便我可以实现此处发布的第二个代码。

    self.networkStream = CFBridgingRelease(
                                           CFWriteStreamCreateWithFTPURL(NULL, (__bridge CFURLRef) url)
                                           );
    assert(self.networkStream != nil);

    if ([usernameText length] != 0) {
        success = [self.networkStream setProperty:usernameText forKey:(id)kCFStreamPropertyFTPUserName];
        assert(success);
        success = [self.networkStream setProperty:passwordText forKey:(id)kCFStreamPropertyFTPPassword];
        assert(success);
    }

    self.networkStream.delegate = self;
    [self.networkStream setProperty:(id)kCFBooleanFalse forKey:(id)kCFStreamPropertyFTPAttemptPersistentConnection];
    [self.networkStream setProperty:(id)kCFBooleanTrue forKey:(id)kCFStreamPropertyShouldCloseNativeSocket];

    [self.networkStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
    [self.networkStream open];

.

CFSocketRef cfsock = [sock getCFSocket]; 
CFSocketNativeHandle rawsock = CFSocketGetNative(cfsock); 
int flag = 1; 
int result = setsockopt(rawsock, IPPROTO_TCP, TCP_NODELAY,
                    (char *)&flag, sizeof(int)); 
if (result != 0)
    NSLog(@\"Could Not Disable Nagle...\");
else
    NSLog(@\"Nagle Is Disabled.\");

第二个块需要一个套接字才能执行,我只有一个流并且不知道如何将套接字从流中取出。

谢谢

4

1 回答 1

2

调用CFWriteStreamCopyProperty()您的写入流并指定kCFStreamPropertySocketNativeHandle获取包含底层 CFSocketNativeHandle 的 CFDataRef。然后,您可以使用CFDataGetBytes().

CFSocketNativeHandle rawsock;
CFDataRef socketData =
  CFWriteStreamCopyProperty((__bridge CFWriteStreamRef)(self.networkstream),
                            kCFStreamPropertySocketNativeHandle);
CFDataGetBytes(socketData,
               CFRangeMake(0, sizeof(CFSocketNativeHandle)),
               (UInt8 *)&rawsock);
CFRelease(socketData);

/* Make use of rawsock */
于 2013-05-02T18:04:59.600 回答