NSOutputStream 发送旧值 - Objective C
Posted
技术标签:
【中文标题】NSOutputStream 发送旧值 - Objective C【英文标题】:NSOuputStream sends Old Value - Objective C 【发布时间】:2014-08-18 07:16:41 【问题描述】:我创建了用于发送/接收值的套接字的代码。我都做了。第一次效果很好。我面临的问题是每当我第二次发送新值时,旧值都会被传递。就像,我第一次向套接字发送 0 值时,套接字端的日志文件在日志文件中接收为“0”,并且我从服务器接收到数据,它运行良好。但是当我将另一个值(如“2”)发送到套接字时,但服务器套接字在日志文件中接收先前的值作为“0”。我无法发送新值。
我的代码是
Appdelegate.h
@property (strong) NSInputStream *inputStream;
@property (strong) NSOutputStream *outputStream;
- (void) applicationDdifinishLaunching....
[self initNetworkCommunication:@"192.168.1.38"];
Appdelegate.m
- (void) initNetworkCommunication:(NSString *) getIp
NSLog(@"GETIP = %@",getIp);
CFStringRef aCFString = (__bridge CFStringRef)getIp;
CFReadStreamRef readStream;
CFWriteStreamRef writeStream;
CFStreamCreatePairWithSocketToHost(NULL, aCFString, 1500, &readStream, &writeStream);
CFReadStreamSetProperty(readStream, kCFStreamPropertyShouldCloseNativeSocket, kCFBooleanTrue);
CFWriteStreamSetProperty(writeStream, kCFStreamPropertyShouldCloseNativeSocket, kCFBooleanTrue);
NSInputStream *inStream = (__bridge NSInputStream *) readStream;
NSOutputStream *outStream = (__bridge NSOutputStream *) writeStream;
[inStream setDelegate:self];
[outStream setDelegate:self];
[inStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[outStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[inStream open];
[outStream open];
self.inputStream = inStream;
self.outputStream = outStream;
[self performSelectorInBackground:@selector(StartDatatoServer:) withObject:@"0"];
- (void) StartDatatoServer:(id) get
NSString *command = [NSString stringWithFormat:@"%@",get];
//NSData *datafixed = [[command stringByAppendingString:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding];
NSMutableData *data = (NSMutableData *)[[command stringByAppendingString:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding];
unsigned long length = [data length];
uint8_t *readBytes = (uint8_t *) [data bytes];
uint8_t buffer[length];
(void)memcpy(buffer, readBytes, length);
length = [outputStream write:(const uint8_t *)buffer maxLength:length];
if (-1 == length)
NSLog(@"Error writing to stream %@: %@", outputStream, [outputStream streamError]);
else
NSLog(@"Wrote %ld bytes to stream %@.", (long)length, outputStream);
[self writeLogFile:command TypeName:@"ServerStart"];
- (void)stream:(NSStream *)theStream handleEvent:(NSStreamEvent)streamEvent
switch (streamEvent)
case NSStreamEventOpenCompleted:
if ([theStream isKindOfClass:[inputStream class]])
NSLog(@"Input stream opened");
else
NSLog(@"output stream opened");
break;
case NSStreamEventHasSpaceAvailable:
NSLog(@"NSEvethaspcape");
break;
case NSStreamEventHasBytesAvailable:
// Listening Server Acknowledgement
NSLog(@"NSStreamEventHasBytesAvailable");
if (theStream == inputStream)
break;
case NSStreamEventErrorOccurred:
NSLog(@"NSStreamEventErrorOccurred");
// When disconnect or Error occured between socket & server
break;
case NSStreamEventEndEncountered:
// Occur when Server is Closed
NSLog(@"NSStreamEventEndEncountered");
[theStream close];
[theStream removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
theStream = nil;
break;
default:
NSLog(@"Unknown event");
在Windowcontroller.h中
anObject = @"2";
-(void) LoginConnectionProcess:(id)anObject
AppDelegate *appDelegate = (AppDelegate *)[[NSApplication sharedApplication] delegate];
appDelegate.inputStream.delegate = self;
appDelegate.outputStream.delegate = self;
NSString *command = [NSString stringWithFormat:@"%@",anObject];
NSMutableData *data = (NSMutableData *)[[command stringByAppendingString:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding];
unsigned long length =
[data length];
NSLog(@"anOjbe= %@",command);
uint8_t *readBytes = (uint8_t *) [data bytes];
uint8_t buffer[length];
(void)memcpy(buffer, readBytes, length);
length = [appDelegate.outputStream write:(const uint8_t *)buffer maxLength:length];
if (-1 == length)
NSLog(@"Errors writing to stream %@: %@", appDelegate.outputStream, [appDelegate.outputStream streamError]);
else
NSLog(@"Wrotes %ld bytes to stream %@.", (long)length, appDelegate.outputStream);
- (void)stream:(NSStream *)theStream handleEvent:(NSStreamEvent)streamEvent
switch (streamEvent)
case NSStreamEventOpenCompleted:
NSLog(@"ConnectionOpened");
break;
case NSStreamEventHasSpaceAvailable:
NSLog(@"NSStreamEventHasSpaceAvailable");
break;
case NSStreamEventHasBytesAvailable:
NSLog(@"NSStreamEventHasBytesAvailable");
if (theStream == appDelegate.inputStream)
break;
case NSStreamEventErrorOccurred:
break;
case NSStreamEventEndEncountered:
[theStream close];
[theStream removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
theStream = nil;
break;
default:
NSLog(@"Unknown Events");
每当我第二次调用时,流“NSStreamEventHasSpaceAvailable”函数只会触发。但从未调用过“NSStreamEventHasBytesAvailable”函数。我在这里缺少/问题。 等待您的回复。 任何帮助。提前致谢。
【问题讨论】:
代码看起来很烂。为什么要重置流代表?为什么第二个代码片段不使用StartDatatoServer:
?当您可以直接在 NSData
对象中轻松访问字节时,为什么要将 NSData
对象复制到缓冲区?
@***foe 那么我如何访问 NSOuputstream 来发送数据,并且需要在打开套接字时立即发送数据
任何想要发送数据的代码都可以调用StartDataToServer:
并与流交互。
@***foe 好的。我将数据作为“0”发送到服务器,当连接打开时,我将该代码替换为“NSStreamEventOpenCompleted”函数。还是一样的工作。我只是第一次打电话。我有一个疑问。你能澄清一下,那个函数经常再次调用吗?
@***foe 我再次使用相同的代码,输出相同。 Here
【参考方案1】:
我刚刚调用了下面的代码,它会被刷新
NSString *command = [NSString stringWithFormat:@"%@\r",username.StringValue];
NSData *data = [command dataUsingEncoding:NSUTF8StringEncoding];
[outputStream write:[data bytes] maxLength:[data length]];
【讨论】:
以上是关于NSOutputStream 发送旧值 - Objective C的主要内容,如果未能解决你的问题,请参考以下文章
在 iPhone 上录制音频并使用 NSOutputStream 通过网络发送
NSOutputStream 没有调用委托的 NSStreamEventHasSpaceAvailable