Sunday, August 3, 2008

Cocoa AsyncSocket on iPhone

There has been a lot of interest recently regarding using AsyncSocket on the iPhone. (In other words, I've gotten a lot of email questions about it.) Just wanted to let everyone know that AsyncSocket works great on the iPhone, and I've received word from several developers that are already using it within their iPhone apps.

As a heads up, developers may need to be aware that the iPhone doesn't currently support IPv6. As such, AsyncSocket has recently been patched to prevent any problems this may cause:
CocoaAsyncSocket Changes

CocoaAsyncSocket Google Code Page Home

12 comments:

rich said...

Having trouble getting it to work with SSL on the iPhone. Any luck there?

Robbie Hanson said...

Check out the latest iPhone code:
BlogPost: HTTP Server - iPhone Example

Also, with regards to using SSL, take a look at the "SecureHTTPServer" sample project. It's a bit dated, but the SSL related code should be pretty much the same:
Downloads

Anonymous said...

Hello, It is not working on iPhone (I am using 2.2.1 simulator) I am just doing a simple http get. The code is taken from a previous post here on doing "statefull http", nothing fancy. The maybeDequeueWrite method never gets called. I will be happy to send the sample iphone project

Robbie Hanson said...

First try getting the latest version of the code.

If you're still having problems, then feel free to post to the CocoaAsyncSocket mailing list.

Anonymous said...

it worked after I added :
NSArray *runLoopModes = [NSArray
arrayWithObjects:NSRunLoopCommonModes, nil];
[asyncSocket setRunLoopModes:runLoopModes];

------------>
this was not in the "stateful http" code on the other post

Sorinu' said...

Hello, I want to send data from a C# tcpip server to my iphone through wifi. i made a AsyncSocket using the class from cocoa google code, if i make an nstimer(timeout 1 sec) and set the socket to read data with [socket readDataWithTimeout:-1 tag:0]; it will stop calling the didReadData method after 10 seconds.
The server will send data to my iphone when it has new data to send(meaning that it can send data at unknown intervals). How can i make the socket read the data when it will come on the socket?(something like socket listener in C#)

Thank you

Robbie Hanson said...

Hi Sorinu,

Can you post the socket code you've written to the CocoaAsyncSocket mailing list?

Anonymous said...

if i need to communicate b/w windows app and iphone app , do i need to run cocoaasyncscoket on iphone and dotnetasyncsocket on C# server ?
please help me ..
mail me at :
rahulgupta.87@gmail.com

Anonymous said...

The code is taken from a previous post here on doing "statefull http", nothing fancy.

viagra online said...

How can i make the socket read the data when it will come on the socket?

Anonymous said...

Can't understand sending ints. Could you review and let me know what a dumba** I am? int b = 1 does not appear in the Echo Server or is bounced back to my app. I also have the same issue if b = 0.

-(void)sendHello{
NSData *message =
[@"Hello, Echo Server!" dataUsingEncoding:NSUTF8StringEncoding];

NSData *newline =
[@"I am a new client." dataUsingEncoding:NSUTF8StringEncoding];
int b = 1;
NSMutableData *newMSg = [[NSMutableData alloc]initWithData:message];
NSMutableData *newNewLine = [[NSMutableData alloc]initWithData:newline];

[self send:newMSg];

[self flush];

[socket writeData:[NSData dataWithBytes:&b length:sizeof(b)] withTimeout:-1 tag:0];
[self send:newNewLine];

[self flush];


NSLog(@"sendHello");
}


-(void)send:(NSMutableData *)data{
NSData *flush = [@"\r" dataUsingEncoding:NSUTF8StringEncoding];
[data appendData:flush];
[socket writeData:data withTimeout:-1 tag:0];
}

- (int)flush{
NSMutableData *data = [NSMutableData dataWithBytes:"\n" length:sizeof("\n")];

int sentVal = 0;
[socket writeData:data withTimeout:-1 tag:0];

return sentVal;
}

Echo Server print:
Accepted client 127.0.0.1:51361
Hello, Echo Server!

Viagra said...

Thank you for letting us know it works!