Answer the question
In order to leave comments, you need to log in
TCP / IP: what to do if the Internet is lost while the server is reading a packet from the client?
TCP / IP: what to do if the Internet is lost while the server is reading a packet from the client?
The situation is as simple as 3 kopecks.
Client code:
...
var bytes = new byte[30000000];
...
server.Write(bytes, 0, bytes.Length); //шлём пакет серверу
...
...
var client = listener.AcceptTcpClient();
...
var bytes = new byte[30000000];
client.Read(bytes, 0, bytes.Length); //читаем пакет от клиента
...
Answer the question
In order to leave comments, you need to log in
First, let's separate the flies from the cutlets. Packets in TCP, you have a buffer.
Second, TCP guarantees that the packet will be delivered (in the order you specify when sending it). Until the packet is transmitted in its entirety, the TCP/IP stack will repeat it. The algorithm of the protocol is as follows. However, there are circumstances stronger than TCP (disappearance of a connection from this series) and any program must take into account the fact that the data may not reach or reach incompletely.
Thirdly, if not all the data you are waiting for came to the buffer, then you either need to transfer the checksum (what to do if it didn’t [all] come?), or, more correctly, transfer not a flat buffer, but some a data structure where you can check that the field values have changed (they were filled when receiving data) compared to the initial values. And proceeding from this, decide that the entire structure has arrived (the last field, for example, is the checksum, by default - 0) or it must be requested again.
And how did you want? It's like a fairytale? You can write like in a fairy tale if your program works 101% only and exclusively on the local network . In this case, the disconnection of a connection or segment is an emergency and it is not for you to fix it.
In general, I fully support Smithson and Oleg Tsilyurik
Check error codes returned by transmit/receive methods. When working with a network, you should always take into account that the transmission or reception will fail or not all data will be received / transmitted.
Those. in fact, you should write a program based on the fact that receiving / transmitting errors are not only possible, but they will definitely always be.
And yes, custom keep-alives for TCP are complete bullshit - everything is already implemented in the protocol.
It is necessary either to write the message number and size when sending. Since this is TCP, the client will send odd numbers, receive even numbers, the server will receive odd numbers, and send even numbers. If suddenly there is a gap or something else - we are waiting for our package. If the package is missed, please send your number. That is, it is also necessary to implement peculiar service requests.
Usually use already ready NetworkManager'y.
In short, I'm confused. This bearded TCP is a real mind blowout. You don't wish it on your enemy.
If I understand the problem correctly, then either do Heartbeat (a fashionable name for a bicycle) or wait until the server timeouts after an indefinite period (well, an hour there).
Classics of the genre on this link. Read the "how not to" section carefully.
Detection of Half-Open (Dropped) Connections
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question