iphone - How do I read data using CocoaAsyncSocket? -


i have created tcp socket connection in appdelegate didfinishlaunchingwithoptions method. easy part, , have connected server. having great difficulty reading data server in view. have been looking through tutorials on how appropriately (step step) read data using cocoaasyncsocket, haven't come across useful.

this code appdelegate:

-(bool)application:(uiapplication *)application didfinishlaunchingwithoptions:(nsdictionary *)launchoptions {     socket = [[asyncsocket alloc] initwithdelegate:self];     [self connect];      self.window = [[[uiwindow alloc] initwithframe:[[uiscreen mainscreen] bounds]] autorelease];     // override point customization after application launch.     self.viewcontroller = [[[tekmatrixviewcontroller alloc] initwithnibname:@"tekmatrixviewcontroller" bundle:nil] autorelease];     self.window.rootviewcontroller = self.viewcontroller;     [self.window makekeyandvisible];     return yes; }  

and here connect method, @ bottom of appdelegate file:

- (void)connect  {     [socket connecttohost:@"9.5.3.3" onport:11005 error:nil]; } 

that easy part. need read data server. know kind of nsdata or nsmutabledata object needs created take value of data read server. have been unsuccessful in finding tutorial or documentation points me in right direction. there several different read functions, different parameters, etc. if point me resource goes on in depth*(i newbie, after =p)* appreciate -- or if knows of easy way accomplish goal , wouldn't mind providing sample code here :d

this library i'm using: cocoaasyncsocket. i'm using library asyncsocket.h , asyncsocket.m

i've been stuck @ hours, great appreciated.

thanks!

this should work:

- (void)onsocket:(asyncsocket *)sock didreaddata:(nsdata *)data withtag:(long)tag {     nsdata *strdata = [data subdatawithrange:nsmakerange(0, [data length])];     nsstring *msg = [[nsstring alloc] initwithdata:strdata encoding:nsutf8stringencoding];     if(msg)     {         nslog(@"rx:%@",msg);     } } 

you should implement other delegate methods, example:

- (void)onsocket:(asyncsocket *)sock willdisconnectwitherror:(nserror *)err {     nslog(@"error - disconnecting");     //you'd want start reconnecting procedure here... }  - (void)onsocketdiddisconnect:(asyncsocket *)sock {     nslog(@"disconnected"); }  - (void)onsocket:(asyncsocket *)sock didconnecttohost:(nsstring *)host port:(uint16)port {     nslog(@"connected"); } 

edit: if memory serves me right there documentation , examples available library.


Comments

Popular posts from this blog

jquery - Invalid Assignment Left-Hand Side -

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -