How To Create A Socket Based iPhone App and Server
Posted by CesareThis is an excerpt of a tutorial I wrote for raywenderlich.com.
Many iOS apps use HTTP to communicate to a web server, because it’s easy, convenient, and well-supported.
However, in some cases you might find the need to go a bit lower level than HTTP, and communicate using TCP sockets to your own custom server.
The advantages of doing this are several:
- You can send just the exact data you need to send – making your protocol lean and efficient.
- You can send connected clients data whenever you want, rather than requiring the clients to poll.
- You can write socket servers without a dependency of a web server, and can write in the language of your choice.
- Sometimes you just have to use sockets, if you are connecting to a legacy server!
In this tutorial, you’ll get some hands-on experience writing an iPhone app that communicates to a TCP socket server using NSStream/CFStream. Also, you’ll write a simple socket server for it to connect to, using Python!
The iPhone app and chat server will implement chat functionality, so you can chat between multiple devices in real-time!
This tutorial assumes you have a basic familiarity with Python and iOS programming. If you are new to Python programming, check out the official Python tutorial. If you are new to iOS programming, check out some of the iOS tutorials on this site first.
Without further ado, let’s do some socket programming!
Read the tutorial on raywenderlich.com
Leave a Reply