According to wikipedia SSDP is a network protocol for advertisement and discovery of network services and presence information. In another words, SSDP is used to broadcast message in order to discover devices. Message which I am sending through SSDP looks like:
M-SEARCH * HTTP/1.1\r\n
HOST: 239.255.255.250:1900\r\n
MAN: "ssdp:discover"\r\n
MX: 3\r\n
ST: urn:milosev:com:Stanko:1\r\n
Detailed explanation of headers you can read in this document.
Code in Android which I am using for sending M-SEARCH request looks like this:
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); StrictMode.setThreadPolicy(policy); try { MulticastSocket s = new MulticastSocket(1900); s.joinGroup(InetAddress.getByName("239.255.255.250") ); DatagramPacket packet = new DatagramPacket(DISCOVER_MESSAGE.getBytes(), DISCOVER_MESSAGE.length(), getBroadcastAddress(), 1900); s.setBroadcast(true); s.send(packet); } catch (IOException e) { e.printStackTrace(); }
Where DISCOVER_MESSAGE is constant which looks like this:
private final static String DISCOVER_MESSAGE = "M-SEARCH * HTTP/1.1\r\n" + "HOST: 239.255.255.250:1900\r\n" + "MAN: \"ssdp:discover\"\r\n" + "MX: 3\r\n" + "ST: urn:milosev:com:Stanko:1\r\n";
getBroadcastAdress is like:
InetAddress getBroadcastAddress() throws IOException { Context mContext = getApplicationContext(); WifiManager wifi = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE); DhcpInfo dhcp = wifi.getDhcpInfo(); // handle null somehow int broadcast = (dhcp.ipAddress & dhcp.netmask) | ~dhcp.netmask; byte[] quads = new byte[4]; for (int k = 0; k < 4; k++) quads[k] = (byte) ((broadcast >> k * 8) & 0xFF); return InetAddress.getByAddress(quads); }
Permissions in \app\src\main\AndroidManifest.xml which we need:
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
Code to receive messages looks like this:
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); StrictMode.setThreadPolicy(policy); try { MulticastSocket s = new MulticastSocket(1900); s.joinGroup(InetAddress.getByName("239.255.255.250") ); DatagramPacket packet = new DatagramPacket(DISCOVER_MESSAGE.getBytes(), DISCOVER_MESSAGE.length(), getBroadcastAddress(), 1900); s.setBroadcast(true); while(true) { byte[] buf = new byte[1024]; DatagramPacket receivePacket = new DatagramPacket(buf, buf.length); s.receive(receivePacket); String msg = new String(receivePacket.getData(), 0, receivePacket.getLength()); } } catch (IOException e) { e.printStackTrace(); }
---
Just one update, in order to receive answer it seems that I have to use same object for receiving, in my case I just placed code for receiving immediately under the code for sending request...
---
One more update, it seems that getBroadcastAddress is not needed, we can simply write InetAddress.getByName("239.255.255.250")