Skip to content

Instantly share code, notes, and snippets.

@richard1122
Last active April 5, 2023 08:00
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save richard1122/b8fc8dd2f4b74e7975cd to your computer and use it in GitHub Desktop.
Save richard1122/b8fc8dd2f4b74e7975cd to your computer and use it in GitHub Desktop.
A simple example for Android NSD(network service discovery) and server socket
package com.richard1993.android.inputhack;
import android.app.Service;
import android.content.Intent;
import android.net.nsd.NsdManager;
import android.net.nsd.NsdServiceInfo;
import android.os.IBinder;
import android.util.Log;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
/**
* Created by rd on 14-9-5.
*/
public class ListeningService extends Service {
private final String INTENT_NEW_MSG = "INTENT_NEW_MSG";
@Override
public void onCreate() {
super.onCreate();
Log.v("service", "listeningService started");
new serviceSocketThread().start();
}
@Override
public IBinder onBind(Intent intent) {
Log.v("service", "listeningService binded");
return null;
}
private final NsdManager.RegistrationListener registrationListener = new NsdManager.RegistrationListener() {
@Override
public void onRegistrationFailed(NsdServiceInfo nsdServiceInfo, int i) {
Log.v("NSD", "register failed :" + nsdServiceInfo.toString());
}
@Override
public void onUnregistrationFailed(NsdServiceInfo nsdServiceInfo, int i) {
Log.v("NSD", "unRegister failed :" + nsdServiceInfo.toString());
}
@Override
public void onServiceRegistered(NsdServiceInfo nsdServiceInfo) {
Log.v("NSD", "Service Registered: " + nsdServiceInfo.toString());
}
@Override
public void onServiceUnregistered(NsdServiceInfo nsdServiceInfo) {
Log.v("NSD", "Service unRegistered: " + nsdServiceInfo.toString());
}
};
private class serviceSocketThread extends Thread {
ServerSocket mServerSocket = null;
@Override
public void run() {
super.run();
try {
mServerSocket = new ServerSocket(0);
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
int localPort = mServerSocket.getLocalPort();
Log.v("socket", "server socket started at: " + mServerSocket.getLocalPort());
CommonHelper.registerNSDService(ListeningService.this, localPort, registrationListener);
while (!Thread.currentThread().isInterrupted()) {
Socket socket = null;
try {
socket = mServerSocket.accept();
new clientSocketThread(socket).start();
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
CommonHelper.unregisterNSDService(ListeningService.this, registrationListener);
}
}
private class clientSocketThread extends Thread {
final Socket mSocket;
final BufferedReader mInputReader;
final OutputStream mOutputStream;
public clientSocketThread(Socket socket) throws IOException {
Log.v("socket", "new client socket received: " + socket.toString());
this.mSocket = socket;
this.mInputReader = new BufferedReader(new InputStreamReader(mSocket.getInputStream()));
this.mOutputStream = mSocket.getOutputStream();
}
@Override
public void run() {
super.run();
while (!Thread.currentThread().isInterrupted()) {
try {
String data = mInputReader.readLine();
if (data == null) throw new IOException("Connection closed");
Log.v("input", "data received: " + data);
} catch (IOException e) {
break;
}
}
Log.v("socket", "client socket closed: " + mSocket.toString());
}
}
}
@sahishah
Copy link

how can i run this program ?

@vmwsree
Copy link

vmwsree commented Mar 11, 2015

share commonhelper class

@shethmanan
Copy link

Please explain how to run this code....

@shethmanan
Copy link

shethmanan commented Oct 30, 2016

Please explain how to run this code....
Where is the CommonHelper class?

@j-nth
Copy link

j-nth commented Sep 26, 2020

Please share CommonHelper class....

@iyadfawwaz
Copy link

Please share CommonHelper class....

it's easy I will share it

@iyadfawwaz
Copy link

`package sy.iyad.sd;

import android.content.;
import android.net.nsd.NsdManager;
import android.annotation.
;
import android.net.nsd.*;

public class CommonHelper
{

public static final String SERVICE_NAME="NsdChat";
public static final String SERVICE_TYPE="_http._tcp";
private Context context;
private static NsdManager nsdManager;
private NsdManager.RegistrationListener listener;
private static NsdServiceInfo service;


public CommonHelper(@NonNull Context context){
	
	this.context = context;
	nsdManager = (NsdManager) context.getSystemService(Context.NSD_SERVICE);
	
	service = new NsdServiceInfo();
	service.setServiceName(SERVICE_NAME);
	service.setServiceType(SERVICE_TYPE);
	
	
}

public static void registerNSDService(@NonNull Context context,int port,@NonNull NsdManager.RegistrationListener listener){
	
	CommonHelper commonHelper = new CommonHelper(context);
	commonHelper.setupService(port);
	nsdManager.registerService(service,NsdManager.PROTOCOL_DNS_SD,listener);
	
}

public static void unregisterNSDService(@NonNull Context context,NsdManager.RegistrationListener listener){
	
	nsdManager.unregisterService(listener);
}

private void setupService(int port){
	service = new NsdServiceInfo();
	service.setServiceName(SERVICE_NAME);
	service.setServiceType(SERVICE_TYPE);
	service.setPort(port);
}

}`

@ReticentFella
Copy link

Why a common helper class is used?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment