Hello, I am writing a client server based on the UDP protocol, I made a debuff on the local host, but when I decided to run the application on the local network, I ran into another problem, namely, the packets are sometimes lost, even if I turn off the Internet and leave only wi- fy. What could be the problem, on the local host everything works as correctly as possible. I do not know what to do in this situation, I ask for your help.

  • 2
    UDP implies that error checking and correction are either not needed or must be performed in an application. ru.wikipedia.org/wiki/UDP This protocol makes it easy to lose packets for the reasons described in the link above. - user207618
  • If you are given an exhaustive answer, mark it as correct (a daw opposite the selected answer). - Nicolas Chabanovsky ♦

2 answers 2

UDP packets have the right to get lost, everything is in order. This is the expected behavior. If you need guaranteed delivery, use TCP.

Or you can send an acknowledgment to the server, and in the absence of such an acknowledgment, repeat the transmission. But such a bike will essentially repeat what TCP does inside.

  • I decided to remake the logic for TCP, now I have very strange problems ... namely, when I send files, I have the following sequence: a) client (accepts file): 1. Send (empty message that would co-write) 2. Receive - I receive information about the file Loop (until the end of the file is notified) 3. Receive - 1 packet 4. Send (empty message) to notify that I have finished the conversion of the packet - the end of the loop (after the loop, save the file) Server (sender) (but I inverted the same thing) Now I will add - simply good
  • b Server: 1. Receive - we receive an empty readiness message 2. Send - file info for a cycle for all packets 3. Send - send 1 packet 4. Receive - I am waiting for confirmation (empty message) end of cycle 5. Send - end of file sending ( in the client's cycle is checked, I explained before the announcement of the cycle) - simply good
  • Actually, it worked perfectly on the local host on the UDP protocol, now on TCP there is a very strange situation that even with such synchronization, somewhere either the client or the server can freeze (usually the server) and that's it ... Solve the problem in a strange way ( but maybe he didn’t decide, but there were no errors) I set a dream for the 15ms stream at the beginning of each iteration - simply good
  • But in another place, where I call this method at the reception, it doesn’t matter ... There may be such a deadlock, and both applications, that the client, that the server is stuck. - simply good
  • I repeat the question, what kind of nonsense? why everything worked fine on UDP (127,0,0,1), and now with TCP, everything works terribly, if I may say so (also Local host), I don’t know what to push the crutches to me ... - simply good

now a very strange situation arises on TCP that even with such synchronization, somewhere either the client or the server can freeze (more often the server) and that’s all ...

Probably because TCP uses blocking sockets for communication. To connect, receive, send messages did not stop the flow, either select the operation in a separate stream (for simplicity, you can use the BackGroundWorker class), or use the asynchronous methods BeginAccept, BeginReceive, BeginSend, BeginConnect.

Very well the difference between synchronous and asynchronous requests is described here. Do you need async / await or not?

Or here's a detailed example of a TCP client server https://www.youtube.com/watch?v=xgLRe7QV6QI

If, nevertheless, decide to further investigate the issue with UDP, then to prevent packet loss, they usually write a wrapper over the UPD providing confirmation of each delivered packet. If for any package did not receive a response on delivery, it is sent again. To send large packages, they are manually fragmented into parts, each of which is signed.

You can organize a queue of packets for shipment in different ways, for example, a cyclic array.