views:

38

answers:

3

Hi all

We want to implement a server/client application for internal use. Does anyone have know how to implement an application protocol based on the socket?

Best Regards,

+1  A: 

A protocol in sockets could be implemented almost as a serial protocol. But a good example is using commands, that you could implement using some layout based in text positions in its more basic form, e.g.

Client writes:

$ADD|Yongwei|Xing|25|M|#

Server responds and aknowledge if everything went ok (the data was valid) or a Error with a code if not:

$ACK#

or:

$ERR|2|#

Where ADD is the command that your code has to read and evaluate. Maybe you can use the first three chars as the command, and the parameters delimited, just as this example.

I recommend that you use special chars instead of the $ and # delimiters. Special chars are the first 30 chars of the ASCII table, and there are the right chars for the job:

dec   char    meaning:
  2   STX     Start of text
  3   ETX     End of text
  4   EOT     End of trasmission
  5   ENQ     Enquiry (asking for data, asking for a chance of transmission)
  6   ACK     Acknowledge (confirmation of data received, allowing trasmission)

Ackowledge could be used as a confirmation, e.g.

Client notice server that he intends to transmit some data by sending the Enquiry ascii char (I'm using < and > to express a special char):

<ETX>

If the server is ready to read the data, then confirms with an Acknowledge:

<ACK>

then the client start sending data with commands:

<STX>ADD|jhon|deer|56|M|<ETX>

and for every command the server confirms it with an <ACK> So the client keeps transmitting and when the data is trasmitted, then sends a <EOT> to notice the server that ti can stop reading data (an so trying to read it as a command).

SalvadorGomez
A: 

That is a fairly open ended question, but if you are operating at the socket level you are probably a level too deep, especially if you are doing application development. A library such as boost::asio for network communication and protocolbuffers for the representation and encoding / decoding would help. Using the Google Web Toolkit (GWT) would simplify creating the UI. You might also consider using App Engine for hosting the backend code, since it will give you higher reliability and scalability than most DIY jobs.

Michael Aaron Safyan
A: 

For internal use, the simplest solution is the best solution. Using sockets (winsock2 on Windows, POSIX sockets on *BSD and Linux), you should exchange some plain text between the client and the server like the FTP protocol for example :

In FTP, the client sends commands like ASCII keywords : QUIT, LIST, STOR... with arguments separated by space. A marker should indicate the end of the command. The simplest marker is the character \n. Then the server sends back a return code to the client to indicate if there is an error or not. The return code is just a number, all in plain text. Along with the return code is a little message that explain the error if there is one.

FTP has been specified in an RFC in 1985. And it still works fine and is still widely used. HTTP is similar in the sense that the client sends an HTTP header composed of a few lines in plain text, separated by \n.

The main concern you should have is regarding the security of your solution. Even on an intranet, a security mechanism must be used to avoid leakage of your data on your corporate network for example.

The simplest thing to do is use ssh (like OpenSSH) to encrypt your network connection. It allows you to control who can call the server (authentication) and allows you to encrypt your data. Encrypting your data is not necessary if it's on a private network dedicated to your application alone.

Jérôme Radix