为什么我的 Tivo 通过 telnet 给我 COMMAND_TIMEOUT?

Why is my Tivo giving me COMMAND_TIMEOUT over telnet?

我正在尝试使用 Java 通过 telnet 连接我的 Tivo。

这是我到目前为止的小测试脚本。

package tivotelnettest;


import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.Socket;

public class TivoTelnetTest
{

    /***
     * Main for the TelnetClientExample.
     ***/
    public static void main(String[] args) throws Exception
    {

        // Create object of Socket.
        Socket socket = new Socket("192.168.0.10", 31339);

        // The command.
        String command = null;

        // Create object of Input Stream to read from socket.
        DataInputStream dataInputStream = new DataInputStream(socket.getInputStream());

        // Create object of Output Stream  to write on socket .
        DataOutputStream dataOutputStream = new DataOutputStream(socket.getOutputStream());

        // Object of Buffered Reader to read command from terminal.
        BufferedReader buffRead = new BufferedReader(new InputStreamReader(System.in));

        System.out.println("Welcome to Telnet Client");
        System.out.println("<Telnet Prompt>");


        System.out.println("Waiting for command: ");
        command = buffRead.readLine();

        while(!"EXIT".equals(command)){

            System.out.println("Sending command: " + command);
            dataOutputStream.writeChars(command);//sends command to server
            System.out.println("Response: " + dataInputStream.readLine()); //gets the response of server

            System.out.println("Waiting for command: ");
            command = buffRead.readLine();
        }



        socket.close();  //close port  
        dataInputStream.close();  //close input stream      
        dataOutputStream.close(); //close output stream      
        buffRead.close();  //close buffered Reader    
    }
}

使用 Windows CMD 我可以成功地发送命令,例如 IRCODE PAUSE 来暂停 Tivo,但是在这里尝试这样做,只会给我 COMMAND_TIMEOUT。这是输出示例。

Welcome to Telnet Client
<Telnet Prompt>
Waiting for command: 
IRCODE PAUSe
Sending command: IRCODE PAUSe
Response: CH_STATUS 0142 LOCAL
Waiting for command: 
IRCODE PAUSE
Sending command: IRCODE PAUSE
Response: COMMAND_TIMEOUT
Waiting for command: 
IRCDE PAUSE
Sending command: IRCDE PAUSE
Response: COMMAND_TIMEOUT

当我连接时使用 Windows,我立即得到 CH_STATUS 0142 LOCAL,所以它似乎有点延迟地读取响应。 Here's 我遵循的指南使 Windows Telnet 正常工作。

谁能看出我为什么会收到 TIMEOUT 错误?

根据此论坛 post,必须不断发出 telnet 命令,因为它不会在命令之间出现延迟。

Tivo UI Control via Telnet

我更改了发送命令的方法,现在似乎可以正常工作了。我将代码提取到它自己的 class.

package tivotelnettest;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

public class Tivo {

    private static final int PORT = 31339;

    private Socket pingSocket = null;
    private PrintWriter out = null;
    private BufferedReader in = null;

    public void connect(){
        try {
            pingSocket = new Socket("192.168.0.10", PORT);
            out = new PrintWriter(pingSocket.getOutputStream(), true);
            in = new BufferedReader(new InputStreamReader(pingSocket.getInputStream()));
        } catch (IOException e) {
            System.out.println("Error connecting: " + e.getMessage());
        }
        System.out.println("Connected");
    }

    public void disconnect() throws IOException {
        out.close();
        in.close();
        pingSocket.close();
    }

    public void sendCommand(String command) throws IOException {
        command = command.toUpperCase().trim();
        System.out.println("Sending command: " + command);

        out.println(command);
        System.out.println("Response: " + in.readLine());   
    }
}

显然目前还比较粗糙,但效果很好。发送命令 .sendCommand("IRCODE GUIDE"); 将打开 Tivo 框上的指南。