I'm currently writing a TCP/IP server for iOS devices in Swift. A connection is typically established in the following steps:
- Create a socket with the socket() system call
- Bind the socket to an address using the bind() system call. For a server socket on the Internet, an address consists of a port number on the host machine.
- Listen for connections with the listen() system call
- Accept a connection with the accept() system call. This call typically blocks until a client connects with the server.
- Send and receive data
The problem is step number 4. The server must always be listening for new connections, so the thread in which it runs is always blocked. How do I deal with this when the application enters in the background or the iDevice goes to sleep mode?