Reputation: 3359
I have been asked to build a part of the app that downloads binary data and posts certain binary data back to the server through a socket. The reason for this is that they need lightning fast transactions.
In the past I have only used web services with JSON/XML.
Can someone point me in the right direction of what I need to learn to perform these functions?
Thanks
Upvotes: 0
Views: 783
Reputation: 3826
First off, doing binary over sockets doesn't guarantee "lightning fast" anything. You need to be sure that what you're sending is compact and can be handled quickly on the receiving end.
That being said, I'd recommend using protocol buffers for the data. It's compact, well-tested and easy to handle on server and client: https://code.google.com/p/protobuf/. This will let you nicely define your domain objects in your .proto files and share these between the client and server applications. There's even support for generating objective-c with protoc: https://code.google.com/p/metasyntactic/wiki/ProtocolBuffers. Note that protocol buffers themselves do not actually provide a protocol to use over sockets, but you can use a very simple one such as writing 4 bytes with the length of the protocol buffer and then the protocol buffer itself immediately following (https://developers.google.com/protocol-buffers/docs/techniques#streaming).
In terms of how to send it, CocoaAsyncSocket is a nice way to write socket-based applications on iOS: https://github.com/robbiehanson/CocoaAsyncSocket. Then your app can be as simple as constructing your protobuf object, calling the data
method on it to extract an NSData *
representation and then sending it using GCDAsyncSocket
's writeData
method.
Receiving would be the same. When your delegate on GCDAsyncSocket
is notified that there's data available, read it out and parse it to your protocol buffer instance.
Upvotes: 2
Reputation: 100622
Developer.apple.com appears to be down at the moment so this link may not be exactly correct, but checkout CFSocket for the TCP socket functionality that Apple directly exposes. It's all Core Foundation C stuff but what you'll end up with is a bunch of relevant callback functions that are called whenever transactions come in, and a function to call to send data out.
Upvotes: 1