Sunday, October 4, 2015

Working With GCDAsyncSocket

One of the most powerful features of GCDAsyncSocket is its queued architecture. This allows to control the socket whenever it is convenient to the developer and not when the socket tells when it is ready. 

For e.g the below method will return immediately and the delegate method didConnectToHost:port: will be invoked when the connection has completed. 

[asyncSocket connectToHost:host onPort:port error:nil];

At this moment the connection is not yet established. It has internally just started the asynchronous connection attempt. However, AsyncSocket lets to reading / writing. Below method is going to queue the read request. After the connection is established, the read request will be automatically made. 

[asyncSocket readDataToLength:LENGTH_HEADER withTiemout:TIMEOUT_NONE tag:TAG_HEADER];

In addition to it, we can also do multiple read/write. 

[asyncSocket writeData:msgHeader withTimeout:TIMEOUT_NONE tag:TAG_HEADER];

//We don’t have to wait for the write to complete 

[asyncSocket writeData:msgBody withTimeout:TIMEOUT_NONE tag:TAG_BODY];


References:

No comments:

Post a Comment