gpt4 book ai didi

ios - NSOuputStream发送旧值- objective-c

转载 作者:行者123 更新时间:2023-12-01 16:39:48 26 4
gpt4 key购买 nike

我已经创建了用于套接字的代码,用于发送/接收值。我都做了第一次运作良好。我面临的问题是,每当我第二次发送新值时,都会传递旧值。像是,我第一次向套接字发送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”函数。我在这里想念/发出的东西。
等待您的回复。
任何帮助。提前致谢。

最佳答案

我只是调用了以下代码,它将被刷新

NSString *command = [NSString stringWithFormat:@"%@\r",username.StringValue];
NSData *data = [command dataUsingEncoding:NSUTF8StringEncoding];
[outputStream write:[data bytes] maxLength:[data length]];

关于ios - NSOuputStream发送旧值- objective-c ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25358048/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com