我已经创建了用于发送/接收值的套接字的代码。我做到了。第一次运作良好。我面临的问题是每当我第二次发送新值时,旧值就会传递。比如,第一次向socket发送0值,socket端的日志文件在日志文件中接收为“0”,我从服务器接收数据,运行良好。但是当我向socket发送另一个像“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”函数。我在这里遗漏/问题。 等待您的回复。 任何帮助。提前致谢。
答案 0 :(得分:0)
我刚刚调用了以下代码,它将被刷新
NSString *command = [NSString stringWithFormat:@"%@\r",username.StringValue];
NSData *data = [command dataUsingEncoding:NSUTF8StringEncoding];
[outputStream write:[data bytes] maxLength:[data length]];