我目前正在用 Objective-C 编写一个简单的 TCP 服务器类,它应该能够绑定和侦听特定端口。我特别有一个-(void) start
创建 CFSockets 的函数,将其绑定到指定的端口并将其添加到运行循环。它看起来像这样:
- (void) start {
//This function is called after the initialiser
if([self isStarted]) //if already started, return without doing anything
return;
//create a socket
CFSocketContext socketCtxt = {0, (__bridge void *)self, NULL, NULL, NULL};
socket = CFSocketCreate(kCFAllocatorDefault, PF_INET, SOCK_STREAM, IPPROTO_TCP, kCFSocketAcceptCallBack, (CFSocketCallBack)&SocketAcceptCallback, &socketCtxt);
NSAssert(socket, @"Error: Cannot create a socket...");
int yes = 1;
setsockopt(CFSocketGetNative(socket), SOL_SOCKET, SO_REUSEADDR, (void *)&yes, sizeof(yes));
//bind it to the desired port
struct sockaddr_in serverAddress;
socklen_t nameLen = 0;
nameLen = sizeof(serverAddress);
memset(&serverAddress, 0, sizeof(serverAddress));
serverAddress.sin_len = nameLen;
serverAddress.sin_family = AF_INET;
serverAddress.sin_port = htons(self.port);
serverAddress.sin_addr.s_addr = htonl(INADDR_ANY);
NSData* address4 = [NSData dataWithBytes:&serverAddress length:nameLen];
if(kCFSocketSuccess != CFSocketSetAddress(socket, (CFDataRef)address4)) {
if (socket) CFRelease(socket);
socket = NULL;
NSAssert(NO, @"Unable to bind the created socket to the desired port");
}
//add to the current run loop
CFRunLoopSourceRef rls = CFSocketCreateRunLoopSource(kCFAllocatorDefault, socket, 0);
assert(rls!=0);
CFRunLoopAddSource(CFRunLoopGetCurrent(), rls, kCFRunLoopCommonModes);
//done!
}
我还定义了SocketAcceptCallback
这样的回调函数:
static void SocketAcceptCallback(CFSocketRef sock, CFSocketCallBackType type, CFDataRef address, const void *data, void *info) {
//this method is called after a connection is accepted
//here we bind the received connection to a pair of
//NSStreams
if(type == kCFSocketAcceptCallBack){
CFSocketNativeHandle nativeSocketHandle = *(CFSocketNativeHandle *)data;
struct sockaddr_in peerAddress;
socklen_t peerLen = sizeof(peerAddress);
NSString * peer = nil;
if (getpeername(nativeSocketHandle, (struct sockaddr *)&peerAddress, (socklen_t *)&peerLen) == 0) {
peer = [NSString stringWithUTF8String:inet_ntoa(peerAddress.sin_addr)];
} else {
peer = @"Generic Peer";
}
NSLog(@"%@ has connected", peer);
//Find corresponding SocketServer object -- it is passed to the callback in the INFO pointer
SocketServer *obj = (__bridge SocketServer*)info;
CFReadStreamRef readStream = NULL;
CFWriteStreamRef writeStream = NULL;
CFStreamCreatePairWithSocket(NULL, nativeSocketHandle, &readStream, &writeStream);
if (readStream && writeStream) {
CFReadStreamSetProperty(readStream, kCFStreamPropertyShouldCloseNativeSocket, kCFBooleanTrue);
CFWriteStreamSetProperty(writeStream, kCFStreamPropertyShouldCloseNativeSocket, kCFBooleanTrue);
//bridge to NSStreams
obj.inputStream = (NSInputStream *)readStream;
obj.outputStream = (NSOutputStream *)writeStream;
//set delegates
[obj.inputStream setDelegate:obj];
[obj.outputStream setDelegate:obj];
//schedule the runloops
[obj.inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[obj.outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
//open connections
[obj.inputStream open];
[obj.outputStream open];
} else {
// on any failure, need to destroy the CFSocketNativeHandle
// since we are not going to use it any more
close(nativeSocketHandle);
}
/*
if (readStream) CFRelease(readStream);
if (writeStream) CFRelease(writeStream); */
}
}
在回调中,我将连接桥接到一对 NSStreams,我将它们作为我的类中的成员字段并用于读取和写入。
现在的问题:我试图运行这个服务器并通过 telnet 连接到它。它连接了,但没有调用上述回调(我通过在其中添加诊断 NSLog 进行了检查)。我怀疑我没有正确地将套接字添加到运行循环,但我自己找不到任何错误。关于它可能是什么的任何想法?