Using the same tcpclient to read / write and save it

I have the following situation:
Client → Router → Internet → Dedicated Server

Client: At startup, it connects to the server and disconnects. From time to time, it receives notifications from the server that the files have been changed. Starts the synchronization process and then notifies the server if it was successful. Sometimes it can lose the connection, so you need to establish a new connection.

Server (Internet): Contains files that sometimes change. Accepts incoming clients and saves the tcpclient object for this client. It can never directly connect to the client, since the client is located behind multiple routers; that the reason the connection should remain open. Notifies the client (s) of changes. It also checks for each client a successful synchronization message.

Questions:

  • How to effectively open my connection on the client and server side?

  • What happens when a client wants to inform the server that the synchronization process was successful, but at the same time, the server notifies the client of a new update ..?

  • Is it good practice to just create a Tcpclient (client side) and save this object throughout the program? And when some network operation failed, try reconnecting to this tcpclient object?

I researched a lot, but could not find something that supports the same tcpclient.

Btw: This is a new thread based on my previous post, this solution was suggested (reuse of tcpclient) ( udp package failed )

Salutes Daan and thanks in advance for your attention.

+4
source share
3 answers

How to effectively keep the connection open on the client and server side?

TCP sends can send saved messages default if you activate SO_KEEPALIVE . But it might be a good idea to send your own save messages (to prevent disconnection of downtime in routers, etc.)

What happens when a client wants to notify the server that the synchronization process was successful, but at the same time, the server notifies the client of a new update ..?

Design your protocol so that it has a request identifier and may differ between notifications and responses.

Create your API as synchronous, but use asynchronous socket processing.

Is it possible to simply create a Tcpclient (client) and save this object in the whole program? And when some network operation failed, try reconnecting to this tcpclient object?

Are you sure it is possible to reconnect to the same client? IIRC is not possible with Socket , and the same applies to TcpClient

I would use the same client until it disconnected.

+6
source
  • To ensure that the connection remains open and that there is no network failure between the client and server, you can add two messages to your protocol: Keep-alive and keep-alive-reply. Either the client or the server (it depends on you) send a keep-alive message at regular intervals (once per minute), and the other side responds with keep-alive-reply. If the sender of the keep-alive message does not receive a response when it is time to send the next keep-alive message, the connection can be considered obsolete and close the connection. At the other end, which sends responses, you need a timer. If the save request was not received at the appointed time, it should close the connection.
  • The client simply allows notifications from the server queue and process them when possible. Either you increase the receive buffer in the client ( setsockopt and SO_RCVBUF ), or save your own queue. To keep track of which sync-done message is suitable for a do-sync message, you must add some kind of message id to your messages. It can be a simple integer value that starts zero, and then the server increments it by one for each “do synchronization” message.
  • As long as you have a working connection, hold the connection object. When the time comes for reconnecting, I will create a new connection object to be safe. But he could use the reuse of the old one, I don't know C # and .NET very well.
+3
source

Heyup Daan. This issue has already been resolved in the latest commit to networkComms.net . It maintains a network connection by sending one byte every two seconds if it does not detect that other traffic is not being sent. If you look at this open source C # library of network communications, starting with example 11 lines here , you might be able to get up and run a little faster than creating everything from scratch yourself.

0
source

Source: https://habr.com/ru/post/1399162/


All Articles