views:

70

answers:

2

Hi All,

I am going to build a client-server application. The client here is an iPad (or an android-based) tablet. The server is a normal pc. Both the clients and the server are connected to the same network (using WiFi).

Is there a standard way (protocol) for communication between the clients and the server? Are there any framework that can be used to ease this communication?

Thank you

+1  A: 

The answer depends by what you define by "server", "client", and "protocol".

Technically, the answer is "yes"; from a practical standpoint the framework you are looking for is called "socket", but regarding the protocol things may get complicated.

A protocol is a syntax structure governing data exchange, i.e., a set of rules you use to request/provide a service (see the IETF website for a list of standard ones). Sockets, on the other hand, provide you merely a communication channel to bring bytes from one side to another and, on top of which, you are required to implement the protocol. The good news is that socket are language independent and you can send messages between heterogeneous devices (ipad/android/linux/windows).

Using sockets in java is easy (I am making it very short here)

server side

ServerSocket ss = new ServerSocket(port);
Socket s = ss.accept();
InputStream is = s.getInputStream();

client side

Socket s = new Socket("server.address", port); // same port as above
OutputStream os = s.getOutputStream();

When you write something using os.write() the same bytes will be read by is.read(). What you write on os is the implementation of your protocol.

This topic is covered (for java language) quite well by "Thinking in Enterprise java" by Bruce Eckel, you can access the digital edition for free. In C/C++/Objective C things are more complicated but you can easily google for tutorials.

Each service defines its own protocol and you should decide if one of the existing will do or you have to define your own, depending on which service you want to implement between the two devices.

If, as in the standard approach, the PC plays the role of server and clients want to retrieve information from it, you might want to consider installing a (very) lightweight web server and access data using HTTPUrlConnection. This is a wrapper for a socket with HTTP protocol management already implemented. Beware, this is for Java; there is no "standard framework equivalent" for C/C++, I honestly have no idea about objective C.

Please, be also aware of the following:

  • If client and servers has different architectures binary data exchange may get painful, better define your protocol as a sequence of strings (like SMTP) and encode/decode binaries using base64 or some other method you may want to implement
  • In order to link the two sockets client has to know the server IP address; if you are running DHCP on your WIFi network then you also need to implement a discovery phase for your service

As a last side note: "client" and "server" are just labels you put on communicating entities depending on who is requesting a service/information (client) and who is providing it (server). Communication is in reality symmetrical and you can use the same structures/functions/code on both endpoints.

Dario
Thanks Dario for the nice answer
fouad
+1  A: 

Do you mean like what happens in games such as macracers? (macracers.com - an iMac is the server and ifones are the clients. You drive the silly car around on the iMac screen using the iPhones as steering wheels.)

The very general answer to your question is NO, you must invent and use your own protocol. There is no ready-to-use solution.

You mention iPad. If you are dealing in the Mac/iPhone/iPad universe, your best bet if you know nothing about networking is to start with "GameKit" which is extremely easy to use. You can find any number of full working GameKit examples in the extensive ifone developer doco. It should give you no trouble.

So then. Essentially on the server end, your messages -- using your own happy protocol -- will come out like this,

// the message arrives and you then do this...
[data getBytes:&getMe length:sizeof(CommsProt)];

whereas on the client end to send messages, you do this...

NSData * data = [NSData dataWithBytes:&sendMe length:sizeof(CommsProt)];
// ...now send that data using GameKit or whatever system you end up with

and you will basically define your protocol, at least the chunks of data, like this:

typedef struct _CommsProt
    {
    BOOL        happyThing;
    someThings  wotJustHappened;
    float       happyValue;
    float       anotherHappyValue;
    // etc
    }
    CommsProt;

Happily, it's very simple. So (A) you have to decide on some sort of (simple) networking layer. Start with GameKit if you are using iOS. After that (B) just make a simple data protocol like the above example, and you're away - good luck!

Some notes...

(i) As Dario points out, "client" and "server" mean basically nothing. You will be able to send the handbags of information (such as "CommsProt" above) in either direction. If you want to think of, and refer to one, end as a server (particularly if you have a hub-type arrangement), that's fine. (By the way, commonly you might use a different data structure in each direction, that's perfectly fine.)

(ii) Regarding sockets. If you get heavily in to networking, you will have to deal with sockets and write your own sockets code. However it is very likely you can choose a networking layer where you never even have to say the word "sockets"! GameKit and Bonjour for instance completely takes care of handling sockets for you, and the other very difficult issue which is FINDING one of your client/servers. If you are new I recommend you completely set aside sockets for now, and use a system such as GameKit (or whatever is equivalent on windose) for your networking layer.

(iii) Indeed AT FIRST just use something incredibly simple like GameKit, while you figure out your protocol and all the other headaches. Later, if necessary you can rewrite the networking layer, or, switch to some other package that you hear about. Happily everything up to the code examples above will be unchanged, only the networking layer will change.

((iv) BTW just for the record - you mention WiFi. GameKit and most convenience packages, are completely agnostic to the physical layer, they take care of it for you. GK will work fine however the fones are connected -- bluetooth, ethernet, whatever!)

Joe Blow
Thanks Joe Blow for the nice answer
fouad