Question

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.

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top