Networking Tutorial for iOS: How To Create A Socket Based iPhone App and Server

I think that technically you can. But only temporarily :slight_smile: The app running the server ā€œin backgroundā€ will be killed after a while by the iOS watchdog.

Ok, thanks for the reply. And for resolve this issue, I could check from the client if the server ā€œin backgroundā€ is up? and run up if isnā€™t.

Kind Regards.

Well, if the server is down it wonā€™t receive data, so itā€™s easy to find out when itā€™s down.
But I think you can run both the server and the client within the same app.

That is the trouble, It canā€™t be the same app.
I was thinking in Check if is running the server app, and send the message when it is running, or start up wait a while and send the message.

In your experince it is possible?

Kind Regards,
AO

I donā€™t think so but Iā€™m happy if you prove me wrong :slight_smile:

Thanks funkyboy, I will try it.

Hi. Thanks for this example. I rewrote Objective-C client example on C++ for Cocos2d-x (iOS + Android cross platform game engine). Now chat is working for both mobile platforms. :wink:

@keepwalking cool. Feel like sharing it with us?

Iā€™ll think about it. :blush: In begin, Iā€™ll try to implement cross-platform real-time multiplayer on your examples but for cocos2d-x.

Hi, thanks for the tutorial!
I managed to make it work on the iPad simulator and also by using telnet (both on my OS and on another OS running on a virtual machine), using the IP adress of my computer and not ā€˜localhostā€™, but still I canā€™t make it work on the real iPad. I get the ā€œCan not connect to the host!ā€ message, corresponding to the NSStreamEventErrorOccurred case.
What am I missing? Do I need to set something on the iPad itself? It is connected to the same wi-fi networkā€¦
Thanks in advance for any help.

@opossum1189 Once you run the app on a real device there might be a few ā€œinterferencesā€:

  • you are using cellular connection, which canā€™t know about a local IP
  • your wireless is setup in a way that doesnā€™t allow connections on the port you are connecting to
  • you server is running on a machine that doesnā€™t allow connections on the port you are listening from

These are just a few off the top of my mind

I think Iā€™ve found the solution, I had to deactivate Windows Firewallā€¦ I tried it yesterday but it didnā€™t workā€¦ Anyway now itā€™s working! Thanks!

Hello,
I am new for socket connection and each time following your guidance here. As per in tutorial I just wrote two lines in python file name id ā€œserver.pyā€

from twisted.internet import reactor
reactor.run()

And simply run python server.py command in terminal and each time getting below error message.

Traceback (most recent call last):
  File "server.py", line 1, in <module>
    from twisted.internet import reactor
ImportError: No module named twisted.internet

Can you please guide me for solve this issue ?

Iā€™m using OS X Capitan and Python version is 2.7.10.

https://pip.pypa.io/en/stable/installing/

Hey there, I am thinking about writing a framework to easily deal with WebSockets in Swift 3. I know there is Starscream out there but that is on Apache v2 License. Is it worth that there is a Version with MIT License? What do you think=