sockets - NSInputStream can't read from TCP Connection [TCP-Server] -


i created tcp server using this (quite old) example apple. server , running, listening on specified port incoming connections.

everything looks perfect, client app can connect server , -stream:handleevent: called expected.

but when write stream, server kind of rejects connection. client says 30 bytes has been written successfully.

but logs say:

did read 0 bytes inputstream. data received:  nsstreameventerroroccurred error: error domain=nsposixerrordomain code=14 "the operation couldn't completed. bad address" 

in client side i'm using following code establish connection:

cfstreamcreatepairwithsockettohost(null, (cfstringref)@"10.0.10.40", 58896, &readstream, &writestream);  _istream = (__bridge nsinputstream *)readstream; _ostream = (__bridge nsoutputstream *)writestream;  [_istream setdelegate:self]; [_ostream setdelegate:self];  [_istream scheduleinrunloop:[nsrunloop currentrunloop] formode:nsdefaultrunloopmode]; [_ostream scheduleinrunloop:[nsrunloop currentrunloop] formode:nsdefaultrunloopmode];  [_istream open]; [_ostream open]; 

on server side creates socket (a cfsocketcallback provided)

_ipv4socket = cfsocketcreate(kcfallocatordefault, pf_inet, sock_stream, ipproto_tcp, kcfsocketacceptcallback, (cfsocketcallback)&jwtcpserveracceptcallback, nil); 

then in callback i'm pairing using this:

cfstreamcreatepairwithsocket(kcfallocatordefault, nativesockethandle, &readstream, &writestream); cfreadstreamsetproperty(readstream, kcfstreampropertyshouldclosenativesocket, kcfbooleantrue); cfwritestreamsetproperty(writestream, kcfstreampropertyshouldclosenativesocket, kcfbooleantrue); 

and bridge cast streams nsstreams, set delegate, schedule in in current runloop , open them.

i'm not quite sure why can happen don't provide whole server code here. much.

when need specific piece of code or guess caused, i'll post respective code here.

update

i did lot of research in own code. tracked issue down -stream:handleevent method invocation.

- (void)stream:(nsstream *)astream handleevent:(nsstreamevent)eventcode {     uint8_t *buffer = null;     nsmutabledata *receiveddata;      switch(eventcode) {         case nsstreameventhasbytesavailable:          if(astream == _istream) {             receiveddata = [nsmutabledata data];              while([_istream read:buffer maxlength:1] > 0) {             //    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ returns -1                 if([[[nsstring alloc] initwithbytes:buffer length:sizeof(buffer) encoding:nsutf8stringencoding] isequaltostring:@"\n"]) {                     break;                 }                  [receiveddata appendbytes:buffer length:sizeof(buffer)];             }               nslog(@"did receive %li bytes of data: %@", [receiveddata length], [[nsstring alloc] initwithdata:receiveddata encoding:nsutf8stringencoding]);         }      } } 

update 2

i read this great apple article tcp servers , clients. walked through steps , program identical code provided apple. problem remains.

i found solution! declaring buffer wrong. address meant error message wasn't network address, thought...

uint8_t *buffer = null; 

must turned into

uint8_t buffer[1]; 

Comments

Popular posts from this blog

php - get table cell data from and place a copy in another table -

javascript - Mootools wait with Fx.Morph start -

php - Navigate throught databse rows -