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

时间:2015-05-26 21:58:19

标签: java telnet

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

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

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错误吗?

2 个答案:

答案 0 :(得分:0)

根据这篇论坛帖子,必须不断发布telnet命令,因为它不期望命令之间的延迟。

Tivo UI Control via Telnet

答案 1 :(得分:0)

我将方法更改为发送命令,现在它似乎工作正常。我将代码提取到它自己的类中。

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框上的指南。