6

我的网络上有一个 Roku 设备,我希望能够以编程方式发现它。Roku官方文档说:

有一个用于本地网络通信的标准 SSDP 多播地址和端口 (239.255.255.250:1900)。Roku 对此 IP 地址和端口的 M-SEARCH 查询作出响应。

为了查询 roku ip 地址,您的程序可以使用 http 协议向 239.255.255.250 端口 1900 发送以下请求:

他们提供了一个使用 netcat 的示例,他们说可以使用 wireshark 来查找结果。他们还说:

外部控制协议使 Roku 可以通过网络进行控制。外部控制服务可通过 SSDP(简单服务发现协议)发现。该服务是一个简单的 RESTful API ,几乎可以由任何编程环境中的程序访问。

我有一个 java 程序,可以根据它的 IP 地址控制我的 Roku,我想实现一个功能,使用这个 SSDP 在网络上发现它。

如何使用 java 发送 M-SEARCH 查询?我完全不知道如何做到这一点。它像一个获取/发布请求吗?如果有人能指出我正确的方向,我将不胜感激!

4

1 回答 1

6

我找到了一个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();
    }
}
于 2015-02-09T23:42:01.297 回答