Search code examples
javatelnet

Why is my Tivo giving me COMMAND_TIMEOUT over telnet?


I'm trying to connect my Tivo over telnet, using Java.

Here's my little test script so far.

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    
    }
}

Using Windows CMD I'm successfully able to send commands, like IRCODE PAUSE to pause the Tivo, but trying to do that here, just gives my COMMAND_TIMEOUT. Here's a sample of the output.

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

Using Windows when I connect, I immediately get the CH_STATUS 0142 LOCAL, so it seems like it's reading the response on a bit of a delay. Here's the guide I followed to get the Windows Telnet working.

Can anyone see why I'm getting the TIMEOUT errors?


Solution

  • I change the method to send commands, and now it seems to work fine. I extracted the code to it's own 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());   
        }
    }
    

    Obviously it's rather rough at the moment, but it works well. Sending the command .sendCommand("IRCODE GUIDE"); will open up the guide on the Tivo box.