ios - How do you recive TCP data through sockets -


i working on project in need send , receive data between mobile ios device , computer. far have code send data ios device computer can not figure out how receive data computer ios device. looking advice/code so. post parts of code needed understand have set far.

- (void)setup {     nsurl *url = [nsurl urlwithstring:host];      nslog(@"setting connection %@ : %i", [url absolutestring], port);     // cfstreamcreatepairwithsockettohost(kcfallocatordefault, (__bridge cfstringref)[url host], port, &readstream, &writestream);     cfstreamcreatepairwithsockettohost(kcfallocatordefault, @"localhost", port, &readstream, &writestream);      if(!cfwritestreamopen(writestream)) {         nslog(@"error, writestream not open");          return;     }     [self open];      nslog(@"status of outputstream: %lu", (unsigned long)[outputstream streamstatus]);     return; }  - (void)open {     nslog(@"opening streams.");      inputstream = (__bridge nsinputstream *)readstream;     outputstream = (__bridge nsoutputstream *)writestream;      [inputstream retain];     [outputstream retain];      [inputstream setdelegate:self];     [outputstream setdelegate:self];      [inputstream scheduleinrunloop:[nsrunloop currentrunloop] formode:nsdefaultrunloopmode];     [outputstream scheduleinrunloop:[nsrunloop currentrunloop] formode:nsdefaultrunloopmode];      [inputstream open];     [outputstream open]; }  - (void)stream:(nsstream *)stream handleevent:(nsstreamevent)event {     nslog(@"stream triggered.");      switch(event) {         case nsstreameventhasspaceavailable: {             if(stream == outputstream) {                 nslog(@"outputstream ready.");             }             break;         }         case nsstreameventhasbytesavailable: {             if(stream == inputstream) {                 nslog(@"inputstream ready.");                  uint8_t buf[1024];                 unsigned int len = 0;                  len = [inputstream read:buf maxlength:1024];                  if(len > 0) {                     nsmutabledata* data=[[nsmutabledata alloc] initwithlength:0];                      [data appendbytes: (const void *)buf length:len];                      nsstring *s = [[nsstring alloc] initwithdata:data encoding:nsasciistringencoding];                      [self readin:s];                      [data release];                 }             }              break;         }         default: {             nslog(@"stream sending event: %i", event);              break;         }     } }  - (void)writeout:(nsstring *)s {     uint8_t *buf = (uint8_t *)[s utf8string];     [self open];     [outputstream write:buf maxlength:strlen((char *)buf)];      nslog(@"writing out following:");     nslog(@"%@", s); } 

i run setup code , run write code , works great. need receive data computer ios stated above.


Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -