我在我的网络上有一个Roku设备,我希望能够通过编程来发现它。官方Roku文件说:
有一个标准的SSDP多播地址和端口(239.255.255.250:1900),用于本地网络通信。Roku响应这个ip地址和端口上的M-搜索查询. 为了查询roku ip地址,程序可以使用http协议向239.255.255.250端口1900发送以下请求:
他们提供了一个使用netcat的例子,他们说wireshark可以用来查找结果。他们还说:
外部控制协议允许通过网络控制Roku。外部控制服务可以通过SSDP (简单服务发现协议)发现。该服务是一个简单的RESTful API ,程序可以在任何编程环境中访问它。
我有一个java程序,它控制我的Roku的IP地址,我想实现一个函数,使用这个SSDP在网络上发现它。
如何用java发送M-SEARCH查询?我完全不知道该怎么做。这像一个get/post请求吗?如果有人能为我指明正确的方向,我将非常感激!
发布于 2015-02-09 15:42:01
我找到了一个java解决方案:
/* 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();
}
}
https://stackoverflow.com/questions/28402554
复制