2

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.

JJ_Jason
  • 349
  • 8
  • 24
  • @Lirik Funny. (I know what I asked so skip it). – JJ_Jason Jan 06 '12 at 22:10
  • 1
    sorry, but you're basically describing the UDP protocol and asking if it's possible :). – Kiril Jan 06 '12 at 22:33
  • If you haven't found your answer here yet, you'll likely find it in the [STUN protocol](http://en.wikipedia.org/wiki/STUN) (though yes, you will still have to bind your UDP connection for it to work). In order to avoid tracking clients yourself, I'd recommend leaning on a protocol that facilitates this for you. I use torrent trackers in my "serverless VPN" to do this for me. You may be able to do the same. – M.Babcock Mar 17 '12 at 04:59

2 Answers2

3

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.

Kiril
  • 39,672
  • 31
  • 167
  • 226
  • 1
    Thanks. I red that as well. But the question is if I can send data without having to know: "www.contoso.com" and 11000 (ip.port)? This way, I have to send a message from the client, so that the server could respond. Is there any way that my client could only and only, listen? (Maybe I was a little under-detailing it). – JJ_Jason Jan 06 '12 at 23:00
  • And the server, *only* sending (or optionally, send only if there is a listener, to free resources). – JJ_Jason Jan 06 '12 at 23:08
  • The www.contoso.com address is the address of the UDP pipe which is visible to all of the clients. Every client has to know about that pipe, otherwise they can't talk to each other. Optional sending is hampered by two things: it's difficult to determine when a listener is connected and it also conflicts with your initial requirement of "anonymity." – Kiril Jan 07 '12 at 01:00
  • I am afraid that you do not understand me. I need clarification around the following. A situation similar to a UdpClient (server here) with IpAdress.Broadcast, on my UDP server. I want to avoid the following steps: udp server goes online, client sends message to server, server responds. Instead i need this: server broadcasts non stop, clients bind/listen/connect whatever, and receive, disconnect as they wish. – JJ_Jason Jan 07 '12 at 01:43
  • Did I mention that my server is on a remote address?? It is broadcasting on an other computer behind a NAT. – JJ_Jason Jan 07 '12 at 02:07
  • Are you saying it cannot be done? If you say that udp works that way one more time i will attack you, using udp packets. – JJ_Jason Jan 07 '12 at 02:17
  • 1
    It works **EXACTLY LIKE YOU NEED IT TO WORK** so use it! Just use it! :) LOL Regarding your UDP host, you're going to have to do something like [UDP hole punching](http://en.wikipedia.org/wiki/UDP_hole_punching) to get it to work with your clients. – Kiril Jan 07 '12 at 02:30
  • Would you mind providing with an example of a server (with a known ext. IP), and multiple clients, listening to the broadcast from the IP. I will give you pizza. – JJ_Jason Jan 07 '12 at 02:41
  • So this is what was bugging me. **Once port state has been successfully established and the hosts are communicating, port state may be maintained by either normal communications traffic, or in the prolonged absence thereof, by so called keep-alive packets, usually consisting of empty UDP packets or packets with minimal non-intrusive content.** Cannot believe / did not know that this is standard procedure. – JJ_Jason Jan 07 '12 at 03:53
  • 1
    JJ_Jason, you can check out an example of UDP client/servers here: http://www.codeproject.com/KB/IP/testingsocketservers.aspx and more details on the UDP client here: http://www.codeproject.com/script/Articles/ViewDownloads.aspx?aid=2614&zep=SocketServerTest%2fSocketServerTest%2fUDPSocketClient.cs&rzp=%2fKB%2fIP%2ftestingsocketservers%2f%2fjbsocketservertestsource.zip – Kiril Jan 07 '12 at 16:48
0

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.