Are there some common network programming methods?

I use some network materials in our project. It was decided that communication is very important, and we want to do it synchronously. Thus, the client sends what the server confirms.

Are there general guidelines for interaction between client and server? For example, if there is no response from the server, should the client automatically try again? Should there be a waiting period before it repeats? What happens if the confirmation fails? At what point will we break the connection and reconnect? Is there any material? I performed a search but nothing happens.

I am looking for best practices in general. I implement this in C # (possibly with sockets), so if there is something .Net specific, then please let me know too.

+6
network-programming
source share
5 answers

The first rule of the network is that you send messages, you do not call functions.

If you approach the network this way and do not pretend that you can remotely call functions or have "remote objects", everything will be okay. You never had a real β€œthing” on the other side of the network connection β€” what you have is basically a photograph of this thing.

All you get from the network is old data. You are never updated. Because of this, you need to make sure that your messages carry the correct semantics - for example, you can increase or decrease something by a value, you should not set its value to the current value plus or minus another (since the current value can change by the time when your message gets there).

+6
source share

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

This may not really answer your question about best practices though :-)

+2
source share

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 will you decide what you need for your protocol. In many cases, you do not need complex error handling mechanisms and can reliably implement a service with simple UDP.

In a few cases, you need to create something more reliable to maintain a consistent global state between multiple machines connected over a network that you cannot trust.

+1
source share

The most important thing I found is that messages should always be stateless (read on REST if that means nothing to you)

For example, if your application monitors the number of shipments over the network, do not send incremental updates (+ x), but always new ones.

+1
source share

In general, think about network programming, I think you should learn about:
1. Nest (of course).
2. Plug and thread.
3. The locking process (use a mutex or semaphore or others).

I hope for this help.

0
source share

All Articles