I have created code used for sockets for send/receive value. I did it all. first time it works well. I face the problem is Whenever i send the new value at second time, the old value is passed. like, first time i send 0 value to socket, the log file on socket side is receive as "0" in log file and i received data from sever, its working well. but when i send another value like "2" to socket, but the server socket receiving previous value as "0" in log file. i cant send new value.
My code is
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");
}
}
In 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");
}
}
and whenever i called second time, the stream "NSStreamEventHasSpaceAvailable" function only fired. but the "NSStreamEventHasBytesAvailable" function is never called. What i missing/issue in here. Waiting your reply. Any help. Thanks in Advance.
I just called the following code, it would be flushed
NSString *command = [NSString stringWithFormat:@"%@\r",username.StringValue];
NSData *data = [command dataUsingEncoding:NSUTF8StringEncoding];
[outputStream write:[data bytes] maxLength:[data length]];