I have a Roku device on my network and I want to be able to discover it programically. The official Roku documentation says:
There is a standard SSDP multicast address and port (239.255.255.250:1900) that is used for local network communication. The Roku responds to M-SEARCH queries on this ip address and port.
In order to query for the roku ip address, your program can send the following request using the http protocol to 239.255.255.250 port 1900:
They provide an example using netcat and they say that wireshark can be used to find the result. They also say:
The External Control Protocol enables the Roku to be controlled via the network. The External Control Service is discoverable via SSDP (Simple Service Discovery Protocol). The service is a simple RESTful API that can be accessed by programs in virtually any programming environment.
I have a java program that controls my Roku given its IP Address, and I would like to implement a function that discovers it on the network using this SSDP.
How do I send an M-SEARCH query with java? I have absolutely no conception of how to do this. Is it like an get/post request? If somebody could point me in the right direction I would be very grateful!
I found a java solution:
/* multicast SSDP M-SEARCH example for
* finding the IP Address of a Roku
* device. For more info go to:
* http://sdkdocs.roku.com/display/sdkdoc/External+Control+Guide
*/
import java.io.*;
import java.net.*;
class msearchSSDPRequest {
public static void main(String args[]) throws Exception {
/* create byte arrays to hold our send and response data */
byte[] sendData = new byte[1024];
byte[] receiveData = new byte[1024];
/* our M-SEARCH data as a byte array */
String MSEARCH = "M-SEARCH * HTTP/1.1\nHost: 239.255.255.250:1900\nMan: \"ssdp:discover\"\nST: roku:ecp\n";
sendData = MSEARCH.getBytes();
/* create a packet from our data destined for 239.255.255.250:1900 */
DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, InetAddress.getByName("239.255.255.250"), 1900);
/* send packet to the socket we're creating */
DatagramSocket clientSocket = new DatagramSocket();
clientSocket.send(sendPacket);
/* recieve response and store in our receivePacket */
DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
clientSocket.receive(receivePacket);
/* get the response as a string */
String response = new String(receivePacket.getData());
/* print the response */
System.out.println(response);
/* close the socket */
clientSocket.close();
}
}