UDP Server Client Subscriber Publisher

3k Views Asked by At

I'm not exactly sure if the following scenario is possible.

Using only UdpClient or a Udp Socket, i would like to achieve a one publisher and multiple client environment.

Udp server is broadcasting on an isolated machine, over the internet. One, or multiple clients 'subscribe' or 'unsubscribe' as needed, thus receiving the broadcast.

Is is possible? Thanks.

EDIT: If so, might the answer-er elaborate?

EDIT: Is it possible without tracking the subscribers?

ADDITIONAL INFO:

Existing, client code:

IPEndPoint IPEP = new IPEndPoint(IPAddress.Parse("EXTERNAL IP"), PORT);
UdpClient UC = new UdpClient();
byte[] REZ;
while (true)
{
    REZ = UC.Receive(ref IPEP);
     Console.WriteLine("REC: " + Encoding.ASCII.GetString(REZ));
}

Can the server be that simple as well? Am I missing something?

ADDITIONAL INFO: When using the real EXTERNAL IP i get the following error: You must call the Bind method before performing this operation.

2

There are 2 best solutions below

10
On BEST ANSWER

The answer is still "Yes, it's possible." Basically, your question is describing the UDP protocol, all of the stuff that you're asking about are built into the UDP protocol. In the UDP protocol, you don't know anything about the subscribers unless they explicitly identify themselves (as part of the data they send). However, in UDP, there is no notion of a publisher and subscriber, there are just clients. Your clients can send data and they can receive data and every client connected to the pipe can see what's being published by every other client.

  • If you want to have a strict publisher, then you just make one client send data onto the pipe.
  • If you want to have a strict subscriber, then you just make a given client receive data from the pipe (just like you had in your example).

Can the server be that simple as well? Am I missing something?

In UDP there is technically no client and server, every endpoint is a client. But the answer is (again): Yes, the server can be that simple as well:

UdpClient udpClient = new UdpClient("www.contoso.com", 11000);
Byte[] sendBytes = Encoding.ASCII.GetBytes("Is anybody there");
try
{
    udpClient.Send(sendBytes, sendBytes.Length);
}
catch ( Exception e )
{
    Console.WriteLine( e.ToString());
}

The above code was taken directly from the documentation for UdpClient.Send.

0
On

UDP is not no client and no server, its "No connection". You need to know and use source and destination ip addresses to send and receive data.

Broadcast addresses can be used on a local network that can be used to send and receive but would need networks to be configured to pass beyond the local network. These broadcast addresses allow you to send 1 message that can be heard by all recipients on the local network. Receivers will be able to see the IP of the address sending, senders will not know who is receiving.

UDP without using a broadcast (Multicast) address will require duplicating the message for all recipients that need to receive it.

So, the anonymity can be achieved by using broadcast IP address of the subnet of the network and it works for even non UDP.

UDP doesn't require a connection to the receiver to send, it will just send a blind transmission, not knowing or caring if the client is listening but it must know the clients ip address. If you use it, you will need to know if you need to continue sensing data to a client or not.