python - Not able to calling NSStreamEventHasBytesAvailable -
i've written server python (twisted). have connected server , sending data server i'm not getting data server , nsstreameventhasbytesavailable not getting called.
this how connect server:
cfstreamcreatepairwithsockettohost(null, (cfstringref)@"localhost", 3000, &readstream, &writestream); inputstream = (nsinputstream *)readstream; outputstream = (nsoutputstream *)writestream; [inputstream setdelegate:self]; [outputstream setdelegate:self]; [inputstream scheduleinrunloop:[nsrunloop currentrunloop] formode:nsdefaultrunloopmode]; [outputstream scheduleinrunloop:[nsrunloop currentrunloop] formode:nsdefaultrunloopmode]; [inputstream open]; [outputstream open];
here problem:
-(void)stream:(nsstream *)thestream handleevent:(nsstreamevent)streamevent { nsstring *event; switch (streamevent) { case nsstreameventhasbytesavailable: event = @"nsstreameventhasbytesavailable"; if (thestream == inputstream) { uint8_t buffer[1024]; int len; while ([inputstream hasbytesavailable]) { nslog(@"input =====%@",inputstream); len = [inputstream read:buffer maxlength:1024]; nslog(@"len -=======%d",len); if (len > 0) { nsmutablestring *output = [[nsmutablestring alloc] initwithbytes:buffer length:len encoding:nsutf8stringencoding]; nslog(@"received data--------------------%@", output); } } } break; } }
this server side code:
from twisted.internet.protocol import factory, protocol twisted.internet import reactor class iphonechat(protocol): def connectionmade(self): self.factory.clients.append(self) print "clients ", self.factory.clients def connectionlost(self, reason): self.factory.clients.remove(self) def datareceived(self, data): = data.split(':') print if len(a) > 1: command = a[0] content = a[1] msg = "" if command == "msg": msg = content print msg def message(self, message): self.transport.write(message + '\n') c in self.factory.clients: c.message(msg) factory = factory() factory.protocol = iphonechat factory.clients = [] reactor.listentcp(650, factory) print "iphone chat server started" reactor.run()
i have tried lot , googled,but did not find solution. it's killing time if 1 have worked on please guide me , post sample code. in advance.
i don't know iphone, find things wrong server. i'd rather write as
from twisted.internet.protocol import factory, protocol twisted.internet import reactor class iphonechat(protocol): def connectionmade(self): self.factory.clients.append(self) print "clients ", self.factory.clients def connectionlost(self, reason): self.factory.clients.remove(self) def datareceived(self, data): = data.split(':') print if len(a) > 1: command = a[0] content = a[1] msg = "" if command == "msg": msg = content self.message(msg) print msg def message(self, message): c in self.factory.clients: c.transport.write(message + '\n') factory = factory() factory.protocol = iphonechat factory.clients = [] reactor.listentcp(650, factory) print "iphone chat server started" reactor.run()
when tested out telnet client, worked out well.
Comments
Post a Comment