5

If i want to develop an iPhone app with a client-server design (iPhone devices as the clients and a c# server),two questions:

  1. Is it possible to use my own laptop to run the server on? and if not what are my options?
  2. Do i have to develop my own protocol for transferring messages?

So if i understood right the process of sending a message like "CREATE NEW USER" from the client to the server is as follow: 1. The client will create a JSON/XML containing the command "CREATE NEW USER" and the new user details. 2. The client will send this JSON/XML via HTTP request (as the body of the HTTP request) with a URL that maps to a c# method on the server side. 3. This will trigger the appropriate method on the server and the new user will be created on the database. 4. The server will create JSON/XML containing the replay "CREATED" and will send it to the client vie HTTP response (as the body of the HTTP response). Is that correct?

Eyal
  • 10,777
  • 18
  • 78
  • 130
  • I see you're new - you can also up vote with the arrows. Reading the stack overflow faq really helps. It's on the menu bar next to your alias – bryanmac Oct 01 '11 at 14:10

4 Answers4

3

You want either xml or json over http. Web Services and REST over http was created for interoperability problems between different platforms which is what you're facing.

Since you're using C# for the server, you can look into WCF and use either a REST pattern or SOAP (web services) to expose your actions and data. Concerning the data you can serialize those objects over the wire as JSON or XML.

For iPhone consumption, I would recommend REST (since that basically maps a url request path to a C# method). From the phones perspective, it's just a url request and xml or json data comes back.

In C# you simply create your methods and decorate them with DataContract attributes. Then, on your methods you map them to url relative paths. Search the net for WCF and REST services. You can run it in any host from a command line to a windows service to IIS.

http://msdn.microsoft.com/en-us/library/bb412178.aspx

When creating those C# services, if REST, you can hit the requests in a browser and see the data come through. You should also look into Fiddler to inspect your traffic: http://www.fiddler2.com/fiddler2/

On the phone side, you first need to make the http request. You can do that with iOS classes but wrappers like ASIHTTPRequest make it much easier. Once you get the response back, you have to parse it. If you choose XML the iOS classes offer simple ways to parse the xml response. If you choose JSON, there's classes like SBJSON.

http://allseeing-i.com/ASIHTTPRequest/ - (Read this ASIHTTPRequest blog before using)

https://github.com/stig/json-framework

rest web services in iphone

There's also a much higher level framework called RESTKit which makes the iPhone side much easier.

https://github.com/RestKit/RestKit

Hope that helps tie it together for you.

EDIT: Adding the create new user scenario:

The client creates a user object with the data (username, password etc...) and sends an HTTP PUT request to http://yourserver/myservice/users. The client serializes the user object to JSON/XML in the body.

What is the recommended/effective request payload for a REST PUT method?

PUT vs POST in REST

The server receives the request. On the server, you have a WCF "myservice" service (it's a class). It has a "public User CreateUser(User user)" method. In that method it creates a user object by doing whatever it has to do (call database etc...). It returns the User object because perhaps the server added info like the user id. The article below has a put request example.

http://damianm.com/tech/building-a-rest-client-with-wcf/

The client would get the response and the user object with all the details like id, etc... would be in the body as JSON/XML. It would deserialize that into a User object on the phone.

The server could also expose things like: /User/{id} --> public User GetUser(string id);

Community
  • 1
  • 1
bryanmac
  • 38,941
  • 11
  • 91
  • 99
  • Thank you for your detailed reply, i edited my question after reading your reply with what i understood, can you please response to that? I actually wanted to write it as a comment to your answer but it was too long... so as my "Stack Overflow Mentor" is it OK that i edited my question? or should i write it as two small comments to your answer? – Eyal Oct 02 '11 at 09:04
  • I'll append my answer with your follow up questions – bryanmac Oct 02 '11 at 14:16
  • Wow thanks this is all new to me, i am trying to understand how all of this fits in with the things I'm familiar with when building client-server app. Do i still need to have a big while(True) loop on the server side, and use TCPClient.GetStream() to read the HTTP requests from the buffers? Do i need to detect when one HTTP request finish and another started? – Eyal Oct 03 '11 at 08:38
  • No - WCF takes care of that for you. See the hosting your own service section in the first link: http://msdn.microsoft.com/en-us/library/bb412178.aspx – bryanmac Oct 06 '11 at 00:07
  • So how can i manage my clients? I usually keep the connected clients in some array, so i can check if a client is connected, and to forward messages from one client to another. Is it possible at all to maintain a persistent connection with a logged in client using WCF? – Eyal Oct 09 '11 at 11:00
  • If you do something like a TCP server, you can keep connected clients - the typical pattern for http/rest is the client connects, does the request, gets the data and it's done. It's an http request/response pattern which scales well for stateless servers. – bryanmac Oct 11 '11 at 04:09
2

I'd strongly recommended to rely on the HTTP protocol. Don't implement your own networking protocol!

Use GET requests to fetch data from the server and POST requests to send big amounts of data from the client to the server.

In order to structure your data, encode the data using JSON. Here is a nice tutorial that explains how to do that with ASIHTTPRequest and JSONKit: http://www.icodeblog.com/2011/09/30/dynamically-controlling-your-application-from-the-web/

And yes, you can run the server on your working machine.

Felix
  • 35,354
  • 13
  • 96
  • 143
1

You can do this fairly easily with ASIHTTPRequest. It's a free 3rd party solution that can talk to different types of web services.

ASIHTTPRequest website

Jeff Wolski
  • 6,332
  • 6
  • 37
  • 69
  • thanks for your reply, i saw it is using the HTTP protocol, but HTTP protocol is for transferring web pages and working with web servers no? my server does not work with web pages, i need a way to send commands from the client to the server, for example: the client send a "Create new User" command to the server, the server then create the user on the db and return an answer to the client. do you mean i need to put the "Create new User" command in the HTTP message body? – Eyal Oct 01 '11 at 06:42
  • You can send those messages as a GET or POST request. – Jeff Wolski Oct 01 '11 at 13:14
  • `ASIHTTPRequest` is no longer the best solution as the developer of it even states in this blog article http://allseeing-i.com/[request_release]; – Paul.s Oct 02 '11 at 11:48
1

Not sure if this answers your question, but just as a suggestion so you don't have to deal a lot with server side stuff, use Parse

aherlambang
  • 14,290
  • 50
  • 150
  • 253