NSFileHandle函数acceptConnectionInBackgroundAndNotify在第二次调用时不工作
问题描述:
我正在使用可可,我试图为我正在编写的程序创建一个tcp套接字服务器,我使用NSFileHandle和方法acceptConnectionInBackgroundAndNotify。我一直在关注here的代码。NSFileHandle函数acceptConnectionInBackgroundAndNotify在第二次调用时不工作
我遇到的问题是我可以连接我的客户端一次,但是当我尝试再次连接时,我得到一个连接拒绝错误。
之前,我连首次命令sudo lsof的-i -P返回以下信息:
RubyBeat 23964 pauljohnson 8u IPv4 0x0632c274 0t0 TCP *:1234 (LISTEN)
在第一时间与客户端连接后,我得到这个:
RubyBeat 23964 pauljohnson 5u IPv4 0x06a30334 0t0 TCP localhost:1234->localhost:51579 (CLOSED)
我的应用程序似乎没有重新打开套接字,以便在第一次连接之后侦听连接,并在第二次尝试连接时获取错误编号22。
我在可可中使用垃圾收集,并想知道是否会导致套接字问题?
我的代码是:
[self createSocket];
}
-(void)createSocket
{
// create socket and wait for events to come in
NSSocketPort* serverSock = [[NSSocketPort alloc] initWithTCPPort: 1234];
socketHandle = [[NSFileHandle alloc] initWithFileDescriptor: [serverSock socket]
closeOnDealloc: NO];
[[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(newConnection:)
name: NSFileHandleConnectionAcceptedNotification
object: socketHandle];
[socketHandle acceptConnectionInBackgroundAndNotify];
}
- (void)newConnection:(NSNotification*)notification
{
NSLog(@"connection accepted");
NSDictionary* userInfo = [notification userInfo];
NSFileHandle* remoteFileHandle = [userInfo objectForKey:
NSFileHandleNotificationFileHandleItem];
if([[userInfo allKeys] containsObject:@"NSFileHandleError"]){
NSNumber* errorNo = [userInfo objectForKey:@"NSFileHandleError"];
if(errorNo) {
NSLog(@"NSFileHandle Error: %@", errorNo);
return;
}
}
[socketHandle acceptConnectionInBackgroundAndNotify];
[[NSNotificationCenter defaultCenter] addObserver:self
selector: @selector(processSocketData:)
name: NSFileHandleReadCompletionNotification
object: remoteFileHandle];
// Send a message to the client, acknowledging that the connection was accepted
[remoteFileHandle writeData: [@"OK" dataUsingEncoding: NSASCIIStringEncoding]];
[remoteFileHandle readInBackgroundAndNotify];
}
/*
Handle client data
*/
- (void)processSocketData:(NSNotification *)note
{
NSData *data = [[note userInfo]
objectForKey:NSFileHandleNotificationDataItem];
NSNumber* errorNo = [[note userInfo] objectForKey:@"NSFileHandleError"];
if(errorNo) {
NSLog(@"NSFileHandle Error: %@", errorNo);
return;
}
// Do something here with your data
// search string for \n\n that terminates event
NSString* stringData = [[NSString alloc] initWithData: data encoding: NSASCIIStringEncoding];
NSLog(@"data received: %@", stringData);
NSDictionary* lastEvent = nil;
NSRange range = [stringData rangeOfString: @"\n\n"];
if (range.location != NSNotFound) {
NSArray* subStrings = [stringData componentsSeparatedByString:@"\n\n"];
NSMutableArray* events = [[NSMutableArray alloc] init];
[eventBuffer appendString: [subStrings objectAtIndex: 0]];
// handle first event - could be in parts
NSDictionary * json = (NSDictionary*)[eventBuffer JSONValue];
[events addObject:json];
for(int i = 1; i < [subStrings count]-1; i++){
NSString* subString = [subStrings indexOfObject:i];
NSDictionary * eventJson = (NSDictionary*)[subString JSONValue];
[events addObject:eventJson];
}
// we have at least one event to draw here
for(NSDictionary* event in events){
NSLog(@"event: %@", [event objectForKey:@"type"]);
}
lastEvent = [events lastObject];
// clear eventBuffer
[eventBuffer setString:@""];
// add end of data to eventBuffer?
}else {
[eventBuffer appendString:stringData];
}
// check event if it is a program exit event then stop receiving data
if([[lastEvent objectForKey: @"type"] compare: @"exit"] != NSOrderedSame){
// Tell file handle to continue waiting for data
[[note object] readInBackgroundAndNotify];
}else {
NSLog(@"exit received stopping receiving data");
}
}
答
我以前见过这个问题。
一旦读取完所有数据后,再进行一次“在后台接受连接”以进行另一轮读取数据。
+0
嗯,仍然没有工作,哦,我放弃了,只是用C中的标准套接字来写它。 – 2011-01-19 08:40:59
尝试使用readInBackgroundAndNotify代替readInBackgroundAndNotify。我这样做,它有帮助。 – 2015-06-23 08:38:10