N
N
Nikolay Alekseev2020-09-28 17:58:00
Computer networks
Nikolay Alekseev, 2020-09-28 17:58:00

What is my fundamental mistake in understanding UDP and TCP?

Good day to all!
The tags have C sharp, because I work on it, and code examples, if they are needed, will be on it, but that's my problem.

I have written many programs that work over the network. It has always been TCP. I understood him, I live with him and everything is fine with him.
I came up with a new task for myself, and decided to use UDP for it, since it should be more convenient from the data I have.

The point is the following. There is a server. It accepts both TCP and UDP connections. At some point in time, something happens on one of the connected clients and it notifies the server about it via TCP. The task of the server is to notify all clients about this already via UDP, using Broadcast distribution. An example interaction scheme is below.
5f71f71912af6373560744.png
My idea was that at the moment when a message arrives from the client via TCP, the server looks at what was sent to it, and if it is worthy of further distribution, this message is simply resent further:

public void Send(byte[] data) {
  IPEndPoint targetEndPoint = new IPEndPoint( IPAddress.Broadcast, 27000 );
  socket.SendTo( data , targetEndPoint );
}

at this point in time, all clients receive this good through And then they already think how to live with it. I wrote a client and a server on a tcp connection, tested it, everything works, everyone is happy. Added a connection to the server via UDP to the client Added this very mailing list on the server side and this whole scheme does not work ... I think: well, then I just turned somewhere in the wrong direction or wrote something wrong somehow, got into the documentation . Reading:
byte[] data = udpClient.Receive( ref ip );

UDP is a simple protocol for transmitting data to a remote host in the most efficient way. However, because UDP is connectionless, delivery of UDP datagrams sent to a remote endpoint is not guaranteed.

And then everything broke for me. It turns out that I generally misunderstood the situation all this time and the UDP connection as such is not established?
It works simply on the principle: I will throw something into the network, maybe it will fly ...
I got hooked on UDP because of the possibility of broadcasting messages, since I did not need mandatory delivery. It won't come and it's fine.
But it turns out that if UDP does not support connection as such, then my idea is fundamentally a failure? And it's easier for me when a message is received by the server, just go through the cycle of all clients and send it to everyone?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
W
Wexter, 2020-09-28
@VariusRain

Well, let's start with the fact that broadcast UDP generally works within the same L2 network segment, i.e. to all hosts on the same network as the server itself. Packets will not go anywhere outside this network.
And yes, UDP does not guarantee the delivery of a packet. Want guaranteed - use TCP and do "broadcast" distribution. I don't see any problem using it for this

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question