ios - NSOuputStream sends Old Value - Objective C -
i have created code used sockets send/receive value. did all. first time works well. face problem whenever send new value @ second time, old value passed. like, first time send 0 value socket, log file on socket side receive "0" in log file , received data sever, working well. when send value "2" socket, server socket receiving previous value "0" in log file. cant send new value.
my code
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) { 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 stream %@: %@", outputstream, [outputstream streamerror]); } else { nslog(@"wrote %ld bytes 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 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 stream %@: %@", appdelegate.outputstream, [appdelegate.outputstream streamerror]); } else { nslog(@"wrotes %ld bytes 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 called second time, stream "nsstreameventhasspaceavailable" function fired. "nsstreameventhasbytesavailable" function never called. missing/issue in here. waiting reply. help. in advance.
i called following code, flushed
nsstring *command = [nsstring stringwithformat:@"%@\r",username.stringvalue]; nsdata *data = [command datausingencoding:nsutf8stringencoding]; [outputstream write:[data bytes] maxlength:[data length]];
Comments
Post a Comment