Use of the Android Network Service Discovery (NSD) protocol

Source: Internet
Author: User

The Android Network Service Discovery Protocol (NSD) can be used to discover an app on a nearby device in a small-scale network. This can be very helpful for some social networking, multiplayer, and multi-player applications.

The use of Android NSD is broadly divided into four types of operations:

1. Registering a network service

2. Discovering Network Services

3. Connect to network services

4. Log off the network service


When using NSD, be sure to note:

Remember to add Android.permission.INTERNET permissions to the manifest, or the program will crash.


I. Registration of Web Services

Registering a network service requires two things: Network Service information (NSDSERVICEINFO) and registering event listeners (Nsdmanager.registrationlistener)

These two things can be completed after: Nsdmanager.registerservice issued to register the network services. The instance code is as follows:

public void Registerservice (view view) {//NOTE: Do not hard-code the port when registering the network service, obtain//an available port number for your network service in the following way. int port = 0;try { ServerSocket sock = new ServerSocket (0);p ort = Sock.getlocalport (); Sock.close ();} catch (Exception e) {toast.maketext (Getapplicationcontext (), "Can not set port", Toast.length_short);} The name, type, port nsdserviceinfo nsdserviceinfo = new Nsdserviceinfo () of the registered Network Service, Nsdserviceinfo.setservicename ("Nsd_test_ Program "); Nsdserviceinfo.setservicetype (" _http._tcp. "); Nsdserviceinfo.setport (port);//To implement a registration event listener for a network service, the listener object should be saved for subsequent logoff Nsreglistener = new Nsdmanager.registrationlistener () {@Overridepublic void onunregistrationfailed (nsdserviceinfo arg0, int arg1) { Toast.maketext (Getapplicationcontext (), "unregistration Failed", Toast.length_short). Show (); @Overridepublic void onserviceunregistered (Nsdserviceinfo arg0) {Toast.maketext (Getapplicationcontext (), "Service Unregistered ", Toast.length_short). Show ();} @Overridepublic void onserviceregistered (Nsdserviceinfo arg0) {Toast.maketext (getapplicatIoncontext (), "Service registered", Toast.length_short). Show (); @Overridepublic void onregistrationfailed (nsdserviceinfo arg0, int arg1) {Toast.maketext (Getapplicationcontext (), " Registration Failed ", Toast.length_short). Show ();}};/ /Get System Network Service Manager, prepare to register Nsdmanager Nsdmanager = (Nsdmanager) getapplicationcontext (). Getsystemservice (Context.nsd_ SERVICE); Nsdmanager.registerservice (Nsdserviceinfo, NSDMANAGER.PROTOCOL_DNS_SD, Nsreglistener);}

Note: the Registerservice () method is executed asynchronously, and if there is an action that must be performed after the service has been registered, take these actions in the Onserviceresgistered event.


Two. Discovering Network services

To discover that a nearby network service needs to define a network service discovery time Listener, the code is as follows:

public void Discoverservice (view view) {Nsdiclistener = new Nsdmanager.discoverylistener () {@Overridepublic void Onstopdiscoveryfailed (String servicetype, int errorCode) {Toast.maketext (Getapplicationcontext (), "Stop Discovery Failed ", Toast.length_short). Show ();} @Overridepublic void onstartdiscoveryfailed (String servicetype, int errorCode) {Toast.maketext (getapplicationcontext (), "Start Discovery Failed", Toast.length_short). Show (); @Overridepublic void Onservicelost (Nsdserviceinfo serviceinfo) {Toast.maketext (Getapplicationcontext (), "Service Lost ", Toast.length_short). Show ();} @Overridepublic void Onservicefound (Nsdserviceinfo serviceinfo) {///Discovery NETWORK SERVICE triggers this event// You can get those services Toast.maketext (Getapplicationcontext (), "Service Found", toast.length_short) that you really care about by using switch or if. Show (); @Overridepublic void ondiscoverystopped (String servicetype) {Toast.maketext (Getapplicationcontext (), "Discovery Stopped ", Toast.length_short). Show ();} @Overridepublic void ondiscoverystarted (String servicetype) {Toast.makEText (Getapplicationcontext (), "Discovery Started", Toast.length_short). Show ();}}; Nsdmanager Nsdmanager = (Nsdmanager) getapplicationcontext (). Getsystemservice (Context.nsd_service); Nsdmanager.discoverservices ("_http._tcp", Nsdmanager.protocol_dns_sd,nsdiclistener);}


Three. Connect to network Services

By defining a network service connection time listener to poll the resolved network service, you can further obtain the address and port of the network service and then decide whether to connect, sample code:

public void Initresolvelistener (view view) {Nsresolvelistener = new Nsdmanager.resolvelistener () {@Overridepublic void Onserviceresolved (Nsdserviceinfo arg0) {//You can get the address and port information for the corresponding network service here, and then decide if you want to establish a connection with it. After that is some socket operation} @Overridepublic void onresolvefailed (nsdserviceinfo arg0, int arg1) {}};}


four. Log off the network service

To log off the network service, you should save the handle related to the network service beforehand, and log off the network service through the Nsdmanager.unregisterservice and Nsdmanager.stopservicediscovery methods, instance code:

public void Unregisterservice (view view) {Nsdmanager Nsdmanager = (Nsdmanager) getapplicationcontext (). Getsystemservice (Context.nsd_service); Nsdmanager.stopservicediscovery (Nsdiclistener); Turn off Network Discovery Nsdmanager.unregisterservice (nsreglistener);    Log off the Network service}



If reproduced please specify the source: Http://blog.csdn.net/gophers




Use of the Android Network Service Discovery (NSD) protocol

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.