此处完全遵循 Apple 的文档:https ://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Streams/Streams.html - 我无法让任何网络 NSOutputStream写入数据。它总是返回一个 Apple 文档似乎说不应该返回的数字(0 - Apple 声称这是针对固定大小的流,而不是网络流??)。
Apple 的所有示例代码都忽略了返回代码(似乎假设它是肯定的 - 我在网上找到的很多示例都使用无符号类型!这是非常错误的 :( )。
我已经成功:
- 带有 kCFSocketAcceptCallBack 的 CFSocketCreate() 和指向我的 handleConnect 函数的指针
- CFSocketSetAddress() 带有“any”和手动选择的端口
- CFRunLoopAddSource() 开始监听端口
...所有这些都有效,通过远程登录到 IP 地址 + 端口来确认...
...但随后就出现了可怕的错误,只需遵循 Apple 文档:
void handleConnect (
CFSocketRef s,
CFSocketCallBackType callbackType,
CFDataRef address,
const void *data,
void *info
)
{
NSLog(@"handleConnect called with callbackType = %li", callbackType);
(myclass) server = (myclass*) info;
BOOL canAccept = callbackType & kCFSocketAcceptCallBack;
BOOL canWrite = callbackType & kCFSocketWriteCallBack;
BOOL canRead = callbackType & kCFSocketReadCallBack;
NSLog(@" ... acceptable? %@ .. readable? %@ .. writeable? %@", canAccept?@"yes":@"no", canRead?@"yes":@"no", canWrite?@"yes":@"no" );
if( canAccept)
{
NSLog(@"[%@] Accepted a socket connection from remote host. Address = %@", [server class], addressString );
/**
"which means that a new connection has been accepted. In this case, the data parameter of the callback is a pointer to a CFSocketNativeHandle value (an integer socket number) representing the socket.
To handle the new incoming connections, you can use the CFStream, NSStream, or CFSocket APIs. The stream-based APIs are strongly recommended."
*/
// "1. Create read and write streams for the socket with the CFStreamCreatePairWithSocket function."
CFReadStreamRef clientInput = NULL;
CFWriteStreamRef clientOutput = NULL;
// for an AcceptCallBack, the data parameter is a pointer to a CFSocketNativeHandle
CFSocketNativeHandle nativeSocketHandle = *(CFSocketNativeHandle *)data;
CFStreamCreatePairWithSocket(kCFAllocatorDefault, nativeSocketHandle, &clientInput, &clientOutput);
// "2. Cast the streams to an NSInputStream object and an NSOutputStream object if you are working in Cocoa."
// "3. Use the streams as described in “Writing a TCP-Based Client.”
self.outputBuffer = [NSMutableData dataWithData:[@"Hello" dataWithEncoding:NSUTF8StringEncoding]];
self.outputBytesWrittenRecently = 0;
((NSOutputStream*)output).delegate = self;
[((NSOutputStream*)output) scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[((NSOutputStream*)output) open]; // MUST go last, undocumented Apple bug
}
}
...和委托方法:
-(void)stream:(NSStream *)aStream handleEvent:(NSStreamEvent)eventCode
{
BOOL streamOpened = eventCode & NSStreamEventOpenCompleted;
BOOL streamReadyToWrite = eventCode & NSStreamEventHasSpaceAvailable;
BOOL streamReadyToRead = eventCode & NSStreamEventHasBytesAvailable;
BOOL streamEnded = eventCode & NSStreamEventEndEncountered;
BOOL streamErrored = eventCode & NSStreamEventErrorOccurred;
if( streamReadyToWrite )
#define C_BUFFER_IN_MEMORY_SIZE_WRITE ( 4096 ) // Apple vaguely recommends 4k, but I tried numbers down to 100 with no effect
uint8_t extraCBufferNSStreamSucks[ C_BUFFER_IN_MEMORY_SIZE_WRITE ];
NSRange rangeOfBytesToAttemptToWriteNext = { self.outputBytesWrittenRecently, MIN( C_BUFFER_IN_MEMORY_SIZE_WRITE, self.outputBuffer.length - self.outputBytesWrittenRecently) };
[self.outputBuffer getBytes:&extraCBufferNSStreamSucks range:rangeOfBytesToAttemptToWriteNext];
NSLog(@"About to write data to stream, this might block...");
NSInteger amountWritten = [self.outputS write:extraCBufferNSStreamSucks maxLength:C_BUFFER_IN_MEMORY_SIZE_WRITE];
NSLog(@"...finished write data to stream, it might have blocked");
if( amountWritten > 0 )
{
self.outputBytesWrittenRecently += amountWritten;
NSRange totalWrittenRange = { 0, self.outputBytesWrittenRecently };
NSLog(@"Written %i bytes: %@", amountWritten, [[[NSString alloc] initWithData:[self.outputBuffer subdataWithRange:totalWrittenRange] encoding:NSUTF8StringEncoding] autorelease] );
if( self.outputBytesWrittenRecently == self.outputBuffer.length )
{
NSLog(@"Finished writing data!");
}
}
else
NSLog(@"ERROR: failed to write bytes to stream (bytes written = %i)", amountWritten);
}