I’m trying to to use an application to communicate with Windows via sockets. At the minimum, I’m trying to at least figure out how I can create a connection from the iPhone (maybe using the iPhone to ping the Windows machine?) I’m not really clear on where I need to start. I’m pretty new to iOS development in general, and brand new to socket/network programming. I’ve tried several tutorials that haven’t gotten me far. My goal is:
- Connect to a server via sockets (the server will be a Windows machine with a service waiting for incoming connections from the iPhone)
- I will eventually be sending JSON packets to the server, as well as receiving JSON packets from the server
- Come up with a response in the iPhone indicating success or failure
If possible, I would like to write/build the client piece first, but I have been lost thus far. Hopefully the nice folks in the SO community can lend a hand and point me in the right direction.
Thanks in advance!
Your question is very wide especially that it does not target a specific programing problem. I will give you some guidelines or point of start.
For iPhone you need to learn how to work with
NSStreamand you might start from here: Stream Programming Guide for Cocoa. You will need an output stream and an input stream so you can manage outgoing and incoming communications easily and in parallel. In other words you will benefit from theNSInputStreamand theNSOutputStreamclasses which are derived from the mainNSStreamclass. When you will send data using streams you will send raw data usinguint8_tbuffers so it is not obligatory to use JSON packets; I personally don’t, but it is up to you.On Windows I assume you will be using C# so you will need to learn
TcpListenerwhich listens on a certain IP address and a port number that you specify. This can start you up a bit: TcpListener Class. Also you will needTcpClientthrough which you will read and write to the stream.For efficient server you need to work Asynchronously and
TcpClienthas synchronous and asynchronous methods for that. Also for better functionality you might need to use threading in your server, or use the built inBackgroundWorkerclass which makes things much more easy.I don’t suggest programming the client alone then the server, I believe that they should go in parallel because it is a 2 sided communication and if you try that you will see that you must work little here and little there. It is not a surprise if I tell you that when I work on my client/server app I have on my desk a Mac and a PC and I switch between them every while.
Finally, I would like to comment on something which you did not ask about. Since your client is a mobile device then you have to expect that it is not always on (or no Internet connection always on it), therefore prepare yourself to have some database work in the server to be able to store messages that need to be sent later…
I hope this can help you start up. If you have a more specific question I might be able to illustrate better. By the way, this job is not that easy but it is great fun if you really like programming, especially when you start to get your first results 😉