问题
I was trying on a TCP connection app, and I am getting a NSStreamEvent "4" on handleEvent. What am I doing wrong?
My code is like,
-(void) initNetworkCommunication {
CFReadStreamRef readStream;
CFWriteStreamRef writeStream;
CFStreamCreatePairWithSocketToHost(NULL, (CFStringRef)@"localhost", 80, &readStream, &writeStream);
inputStream = (__bridge_transfer NSInputStream *)readStream;
outputStream = (__bridge_transfer NSOutputStream *)writeStream;
[inputStream setDelegate:self];
[outputStream setDelegate:self];
[inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[inputStream open];
[outputStream open];
}
- (IBAction)didTapButton:(id)sender {
NSString *response = inputTextField.text;
NSLog(@"%@", response);
NSData *data = [[NSData alloc] initWithData:[response dataUsingEncoding:NSASCIIStringEncoding]];
[outputStream write:[data bytes] maxLength:[data length]];
}
- (void)stream:(NSStream *)theStream handleEvent:(NSStreamEvent)streamEvent {
switch (streamEvent) {
case NSStreamEventOpenCompleted:
NSLog(@"Stream opened");
break;
case NSStreamEventHasBytesAvailable:
NSLog(@"Stream has bytes available");
break;
case NSStreamEventErrorOccurred:
NSLog(@"Can not connect to the host!");
break;
case NSStreamEventEndEncountered:
NSLog(@"Stream closed");
break;
default:
NSLog(@"Unknown event: %@ : %d", theStream, streamEvent);
}
}
The console gives,
2012-05-29 13:37:07.132 GestureTrial[24289:f803] Stream opened
2012-05-29 13:37:07.133 GestureTrial[24289:f803] Stream opened
2012-05-29 13:37:07.133 GestureTrial[24289:f803] Unknown event: <__NSCFOutputStream: 0x6b85c70> : 4
when tried to send a message to server. I tried it with a tcp tester app for Mac, and it's working fine, so might not be a firewall issue. The output is same for device as well as simulator. Any help would be much appreciated.
回答1:
Actually you're not doing anything wrong.
This event (it is NSStreamEventHasSpaceAvailable
) usually occours after writing to the stream telling you that stream is ready for writing again and after opening a writable stream. Please refer to NSStream Class Reference or, to be exact: Stream Event Constants.
If you're not familliar to <<
operator, it means shift bits to left for n places (each shift equals to multiplying by 2). Translation would be:
typedef enum {
NSStreamEventNone = 0,
NSStreamEventOpenCompleted = 1,
NSStreamEventHasBytesAvailable = 2,
NSStreamEventHasSpaceAvailable = 4,
NSStreamEventErrorOccurred = 8,
NSStreamEventEndEncountered = 16
};
In many applications you will se this event simply ignored (not handled) because it usually occours very soon after writing to the stream. If something goes wrong you get NSStreamEventErrorOccurred
or NSStreamEventEndEncountered
and these are the ones you need to handle. You could use NSStreamEventHasSpaceAvailable
as a flag that it is o.k. to send some more data.
You should also know that both streams (inputStream and outputStream) are calling the same delegate method. That's why you get two NSStreamEventOpenCompleted
events to begin with. But again in many cases this shouldnt be a problem. You can always check which stream is the originator of the event if needed.
来源:https://stackoverflow.com/questions/10795344/nsstream-handle-event-giving-status-4