Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are there some general Network programming best practices?

I am implementing some networking stuff in our project. It has been decided that the communication is very important and we want to do it synchronously. So the client sends something the server acknowledges.

Are there some general best practices for the interaction between the client and the server. For instance if there isn't an answer from the server should the client automatically retry? Should there be a timeout period before it retries? What happens if the acknowledgement fails? At what point do we break the connection and reconnect? Is there some material? I have done searches but nothing is really coming up.

I am looking for best practices in general. I am implementing this in c# (probably with sockets) so if there is anything .Net specific then please let me know too.

like image 276
uriDium Avatar asked Mar 17 '10 08:03

uriDium


3 Answers

First rule of networking - you are sending messages, you are not calling functions.

If you approach networking that way, and don't pretend that you can call functions remotely or have "remote objects", you'll be fine. You never have an actual "thing" on the other side of the network connection - what you have is basically a picture of that thing.

Everything you get from the network is old data. You are never up to date. Because of this, you need to make sure that your messages carry the correct semantics - for instance, you may increment or decrement something by a value, you should not set its value to the current value plus or minus another (as the current value may change by the time your message gets there).

like image 66
kyoryu Avatar answered Oct 18 '22 02:10

kyoryu


If both the client and the server are written in .NET/C# I would recommend WCF insted of raw sockets as it saves you a from a lot of plumbing code with serialization and deserialization, synchronization of messages etc.

That maybe doesn't really answer your question about best practices though ;-)

like image 24
Anders Abel Avatar answered Oct 18 '22 03:10

Anders Abel


The first thing to do is to characterize your specific network in terms of speed, probability of lost messages, nominal and peak traffic, bottlenecks, client and server MTBF, ...

Then and only then you decide what you need for your protocol. In many cases you don't need sophisticated error-handling mechanisms and can reliably implement a service with plain UDP.

In few cases, you will need to build something much more robust in order to maintain a consistent global state among several machines connected through a network that you cannot trust.

like image 1
mouviciel Avatar answered Oct 18 '22 03:10

mouviciel