How to Do Network Discovery Using Udp Broadcast

How to do Network discovery using UDP broadcast

It's very simple to make same thing in C#

Server:

var Server = new UdpClient(8888);
var ResponseData = Encoding.ASCII.GetBytes("SomeResponseData");

while (true)
{
var ClientEp = new IPEndPoint(IPAddress.Any, 0);
var ClientRequestData = Server.Receive(ref ClientEp);
var ClientRequest = Encoding.ASCII.GetString(ClientRequestData);

Console.WriteLine("Recived {0} from {1}, sending response", ClientRequest, ClientEp.Address.ToString());
Server.Send(ResponseData, ResponseData.Length, ClientEp);
}

Client:

var Client = new UdpClient();
var RequestData = Encoding.ASCII.GetBytes("SomeRequestData");
var ServerEp = new IPEndPoint(IPAddress.Any, 0);

Client.EnableBroadcast = true;
Client.Send(RequestData, RequestData.Length, new IPEndPoint(IPAddress.Broadcast, 8888));

var ServerResponseData = Client.Receive(ref ServerEp);
var ServerResponse = Encoding.ASCII.GetString(ServerResponseData);
Console.WriteLine("Recived {0} from {1}", ServerResponse, ServerEp.Address.ToString());

Client.Close();

Retrieve available clients via UDP broadcast

There is a standard protocol to advertise services on the network, which you may like to consider: Simple Service Discovery Protocol, based on periodic UDP multicast:

The Simple Service Discovery Protocol (SSDP) is a network protocol based on the Internet protocol suite for advertisement and discovery of network services and presence information. It accomplishes this without assistance of server-based configuration mechanisms, such as Dynamic Host Configuration Protocol (DHCP) or Domain Name System (DNS), and without special static configuration of a network host. SSDP is the basis of the discovery protocol of Universal Plug and Play (UPnP) and is intended for use in residential or small office environments.

In this protocol clients join that UDP multicast group to discover local network services and initiate connections to them, if they wish to. And this is pretty much the intended use case for the protocol, which is somewhat different from your use case.

One benefit of IP/UDP multicast is that multicast packets can be dropped in the network adapter if no process on the host has joined that multicast group. Another one is that IP/UDP multicast can be routed across networks.


From the diagram you posted:

  • The server is the mediator (design pattern) whose location must be known to every other process of the distributed system.
  • The clients need to connect/register with the server.
  • Your master client is a control application.

It makes sense for the server to advertise itself over UDP multi-cast.

Online clients would connect to the server using TCP on start or TCP connection loss. If a client terminates for any reason that breaks the TCP connection and the server becomes immediately aware of that, unless the client was powered off or its OS crashed. You may like to enable frequent TCP keep-alives for the server to detect dead clients as soon as possible, if no data is being transmitted from the server to the clients. Same applies to the clients.

All communications between the server and the clients happen over TCP. Otherwise you would need to implement reliable messaging over UDP or use PGM, which can be a lot of work. Multicast UDP should only be used for server discovery, not bi-directional communication that requires reliable delivery.

The master client also connects to the server, possibly on another port, for control. The master client can discover all available servers (if there is more than one) and allow the user to choose which one to connect to.

How to do Network discovery using UDP broadcast in IOS

You can use Cocoa Async Socket

https://github.com/robbiehanson/CocoaAsyncSocket

Below is the code

    GCDAsyncUdpSocket *udpSocket = [[GCDAsyncUdpSocket alloc] initWithDelegate:self delegateQueue:dispatch_get_main_queue()];

NSError *error = nil;

if (![udpSocket enableReusePort:YES error:&error])
{
return;
}

if (![udpSocket bindToPort:8888 error:&error])
{
return;
}
if (![udpSocket beginReceiving:&error])
{
return;
}

error = nil;
if(![udpSocket enableBroadcast:YES error:&error])
{
}

NSData *data = [@"DISCOVER_FUIFSERVER_REQUEST" dataUsingEncoding:NSUTF8StringEncoding];
[udpSocket sendData:data toHost:@“255.255.255.255” port:8888 withTimeout:10 tag:100];

below is the delegate method where you will get response

- (void)udpSocket:(GCDAsyncUdpSocket *)sock didReceiveData:(NSData *)data
fromAddress:(NSData *)address
withFilterContext:(id)filterContext
{
NSString *msg = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

NSString *msg1 = [NSString stringWithFormat:@"RECV: %@ FROM: %@", msg,[GCDAsyncUdpSocket hostFromAddress:address]];

if([msg isEqualToString:@"DISCOVER_FUIFSERVER_RESPONSE"])
{
}
}

And if got error while udp socket connection this delegate method will be called

- (void)udpSocketDidClose:(GCDAsyncUdpSocket *)sock withError:(NSError *)error
{
if (error) {
NSString *msg = [NSString stringWithFormat:@"RECV: error: %@", [error localizedDescription]];
}
}


Related Topics



Leave a reply



Submit