It's a simple service discovery protocol (like Zeroconf or WS-Discovery) and framework implemented in ObjectiveC. Services are available to publish their :
int port
(required)NSString *type
(required)NSString *title
(optional)NSData *payload
(optional)
// const
#define MULTICAST_GROUP @"239.255.255.240"
#define MULTICAST_PORT 4470
#define RESPONSE_PORT (MULTICAST_PORT + 1)
#define SERVICE_TYPE @"MyServiceType1"
#define SERVICE_PORT 1204
#define SERVICE_TITLE @"Hello world"
// publish
publisher = [[ASServicePublisher alloc] initWithMulticastGroup:MULTICAST_GROUP
andPort:MULTICAST_PORT
andDelegate:self];
publisher.delegateQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
ASServiceInfo *serviceInfo = [[ASServiceInfo alloc] initWithPort:SERVICE_PORT
andType:SERVICE_TYPE
andTitle:SERVICE_TITLE
andPayload:somePayload];
[publisher registerService:serviceInfo];
locator = [[ASServiceLocator alloc] initWithMulticastGroup:group
andMulticastPort:port
andResponsePort:(port + 1)
andDelegate:self];
locator.mode = ASMODE_UDP;
locator.responseTimeoutMillis = 3000; // 3s
locator.delegateQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
...
- (void) didDiscoverService:(ASServiceInfo*)serviceInfo onHost:(NSString*)host {
NSLog(@"Discovered service with title \"%@\" on %@:%i", serviceInfo.title, host, serviceInfo.port);
}
- Publisher listens for UDP milticast requests from Locator.
- Locator starts listening for response (UDP or TCP, depends on locator
mode
) and sendsServiceRequest
with required fields:
string type
// service type to discoverMode mode
// TCP or UDP response modeint port
// either TCP or UDP port for response
- Publisher receives request, accepts or rejects it (in listener
boolean onServiceRequestReceived()
or comparing requested and actual service type) and sendsServiceResponse
over TCP directly to requester host and port in request (if TCP mode reponse was in request) or UDP multicast response (same UDP group but port in request). - Locator receives response and notifies service is found.
See Project/
for more information. Average discovery time in my home network is about 60 ms.
Just use CocoaPod
:
- clone
ASServiceDiscovery-ios
repo:
git clone https://github.com/4ntoine/ServiceDiscovery-ios
- add to your project 'Podfile':
pod 'ASServiceDiscovery', path := '(path to cloned repo 'ASServiceDiscovery.podspec' file)'
- install pod:
pod install
See according Java implementation: https://github.com/4ntoine/ServiceDiscovery-java
Free for non-commercial usage, contact for commercial usage.
Anton Smirnov
dev [at] antonsmirnov [dot] name
2015