Why is my Tivo giving me COMMAND_TIMEOUT over telnet? - java

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?

According to this forum post, the telnet commands have to be continually issued as it's not expecting delays between commands.
Tivo UI Control via Telnet

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.

Related

How do I make java sockets work? So confused

This is the code for my server, its supposed to take an input from the user, print it into console, then send it back to the user.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class DateServer {
public static void main(String[] args) throws Exception {
ServerSocket listener = new ServerSocket(10219);
Socket s = listener.accept();
InputStreamReader in = new InputStreamReader(s.getInputStream());
BufferedReader input = new BufferedReader(in);
PrintWriter out = new PrintWriter(s.getOutputStream());
out.println("connected");
out.flush();
System.out.println("connected");
String test;
while (true) {
try {
test = input.readLine();
System.out.println(test);
out.println(test + " is what I recieved");
out.flush();
} catch(Exception X) {System.out.println(X);}
}
}
}
This is the code for the client:
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.*;
public class DateClient {
public static Scanner keyboard = new Scanner(System.in);
public static void main(String[] args) throws Exception {
System.out.println("Enter IP Address of a machine that is");
System.out.println("running the date service on port 10219:");
String serverAddress = keyboard.next();
Socket s = new Socket(serverAddress, 10219);
BufferedReader input = new BufferedReader(new InputStreamReader(s.getInputStream()));
PrintWriter out = new PrintWriter(s.getOutputStream());
System.out.println(input.readLine());
while(true){
try{
System.out.println(input.readLine());
out.println(keyboard.next());
out.flush();
} catch(Exception X){System.out.println(X);}
}
}
}
This was designed to work across a LAN network. I have no idea why it doesn't work, all that happens is the client will get the message "connected" and nothing else will happen, no matter what is typed into the client end. I'm a noob when it comes to java, but after a bunch of googling and searching through the java libraries, I can't seem to make it work. What did I do wrong?
You send one line from the server to the client, but in your client you wait for two lines before accepting user input to be sent to the server.
Bearing in mind that input.readLine() will block until data is received, can you spot the deadlock here:
Server:
out.println("connected");
while (true) {
try {
input.readLine();
}
}
Client:
input.readLine();
while(true) {
try {
input.readLine();
out.println(keyboard.next());
}
}
(extraneous code trimmed away to show just the problematic sequence of statements)
Both your client and server mutually wait for each other trying to do input.readLine().
This can be easily seen if you remove server's out.println("connected") and its corresponding client's first input.readLine().
On the client, you should probably write first and only then read the response. Try reordering the following lines:
System.out.println(input.readLine());
out.println(keyboard.next());
out.flush();
to get
out.println(keyboard.next());
out.flush();
System.out.println(input.readLine());
In the client, try changing
PrintWriter out = new PrintWriter(s.getOutputStream());
System.out.println(input.readLine());
while(true){
try{
System.out.println(input.readLine());
out.println(keyboard.next());
out.flush();
} catch(Exception X){System.out.println(X);}
}
to
PrintWriter out = new PrintWriter(s.getOutputStream());
while(true){
try{
System.out.println(input.readLine());
out.println(keyboard.nextLine());
out.flush();
} catch(Exception X){System.out.println(X);}
}
Your client is trying to read two lines, but your server sends just one, then polls for input, so both are locked. Also, sinc your server is reading line-by-line, your client should be sending data line-by-line.

Printing messages in console in Eclipse

I created a java application in Eclipse to connect Plant Simulation via Socket. My first step is just sending and receiving messages "Hello" between plant simulation (PS) and my java application. It worked, but I thought it was strange.
Java application is server. PS is client.
I run "ServerJava" first time. When I active "MyClientSocket" in PS, message "Hallo Plant Simulation" is received in PS. Then I run method "m_sendMessage" in PS to send message "Hallo Java" to ServerJava. It is sent to ServerJava, but its not printed in console. If I deactive "MyClientSocket" to disconnect, its printed in console.
How can message be printed immediately, when I run the method "m_sendMessage" in PS?
My codes:
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class ServerJava {
public static void main(String[] args) {
ServerSocket listener = null;
String line = null;
Socket clientSocket = null;
// ServerSocket with Port 30005
try {
listener = new ServerSocket(30005);
} catch (IOException e) {
System.out.println(e);
System.exit(1);
}
try {
System.out.println("Server is waiting to accept user...");
clientSocket = listener.accept();
System.out.println("Accept a client!");
BufferedReader is = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
BufferedWriter os = new BufferedWriter(new OutputStreamWriter(clientSocket.getOutputStream()));
while (true) {
// Message to client: Hello Plant Simulation
os.write("Hello Plant Simulation");
os.flush();
//Message from client: Hello Java
line = is.readLine();
System.out.println(line);
os.close();
}
} catch (IOException e) {
System.out.println(e);
e.printStackTrace();
}
System.out.println("Sever stopped!");
}
}
Code for m_sendMessage
Thank you in Advance for your help.

Java – Server Only Responds when the Client has Stopped

I recently programmed a simple Java server, and a client to test the server. The server is supposed to receive messages from the client, and send a random substring of the message back. The problem is this: When I send the message using the client program, the server does not respond. Then, when I kill the client program, the server leaps into action, and attempts to send the data back to the client. The server reads the data correctly but starts processing it only when I stop the client program.
This is the client code:
import java.io.*;
import java.net.*;
class ServerTest{
public static void main(String args[]) throws Exception{
Socket clientSocket = new Socket(myIpAdress, 8001);
//Send the message to the server
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(clientSocket.getOutputStream()));
String sendMessage = "randSubstring:StackOverflowIsAwsome";
bw.write(sendMessage);
bw.flush();
System.out.println("Message sent: "+sendMessage);
String message = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())).readLine();
System.out.println("Message received from the server : " +message);
clientSocket.close();
}
}
My server code consists of two classes. This one is the listener:
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
public class ServerListener {
public static void main(String args[]) throws Exception {
String clientSentence;
ServerSocket socket = new ServerSocket(8001);
while(true) {
Socket connectionSocket = socket.accept();
BufferedReader input = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
//DataOutputStream output = new DataOutputStream(connectionSocket.getOutputStream());
clientSentence = input.readLine();
if(clientSentence.startsWith("randSubstring:")){
Thread connection = new Thread(new ServerConnection(connectionSocket, clientSentence));
connection.start();
}
Thread.sleep(300);
}
}
}
This is the thread that will not start until the client is stopped:
import java.io.BufferedOutputStream;
import java.io.BufferedWriter;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.util.Random;
public class ServerConnection implements Runnable{
private Socket serverConnection;
private String sentence;
public ServerConnection(Socket connection, String clientSentence){
serverConnection = connection;
sentence = clientSentence;
}
#Override
public void run() {
Random r = new Random();
String substring = sentence.substring(0, r.nextInt(sentence.length()));
try {
OutputStream os = serverConnection.getOutputStream();
OutputStream out = new BufferedOutputStream(os);
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(out));
bw.write(substring);
bw.close();
out.close();
os.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
I am using a Macintosh with Yosemite. Is this happening because I am trying to run the programs on the same computer, or would the problem occur if the programs were run from different computers? Any help is appreciated.
In the server you do a readLine(..), which means that it will wait for a end-of-line character.
But in your sender code, you just send a string with no line ending.
So either you make sure you also send a end of line char or your server wait's for something else as "delimiter"
You're reading a line but you aren't writing a line. Add a line terminator to the sent message. Otherwise readLine() won't return until the peer closes the connection.
NB The I/O in the try block after the accept should be in the Runnable, not where it is. Don't do I/O in the accept loop.

Chat Client/Server - How to instruct Client to Accept input from either Console or Server

How do you code a chat client so that it listens for input both from the server and from the console? This is my current client which successfully sends to and accepts input from the server. As you can see, it doesn't have any code that will enable it to successfully listen for and accept input from the console while also being open to input from the server. The server input would be messages from other chat clients. A message sent from any chat client is broadcast to all other clients. I am pretty new to Java and am completely stuck even though I have a feeling the answer will be depressingly obvious.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
public class ChatClientMain
{
public static void main(String[] args) throws UnknownHostException, IOException
{
//DNS of Chat Server
final String HOST = "localhost";
//Port number for chat server connection
final int PORT = 6789;
Socket serverSocket = new Socket(HOST, PORT);
try
{
//Will need three streams for communication: console-client, client-server, server-client
PrintWriter toServer = new PrintWriter(serverSocket.getOutputStream(), true);
BufferedReader fromServer = new BufferedReader(new InputStreamReader(serverSocket.getInputStream()));
BufferedReader fromUser = new BufferedReader(new InputStreamReader(System.in));
//User must be logged in; any username is acceptable
System.out.print("Enter username > ");
toServer.println("LOGIN " + fromUser.readLine());
String serverResponse = null;
while((serverResponse = fromServer.readLine()) != null)
{
System.out.println("Server: " + serverResponse);
if(serverResponse.equals("LOGOUT"))
{
System.out.println("logged out.");
break;
}
System.out.print("command> ");
toServer.println(fromUser.readLine());
}
toServer.close();
fromServer.close();
fromUser.close();
serverSocket.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
If your main thread is listening to the input from the server, you may start another thread that would keep listening for input from the console. You will have to ensure that you handle input properly. Some flag may be set to indicate if the input is from the server or from the console.

How to Run Stand Alone Java Application from server to any Client

I have Developed an Stand alone application (.jar) which is working on Linux environment, I want to Keep this Application on a server(Linux) and want to access through other systems.
Please suggest if this is Possible.
Have you considered Java Webstart ? It'll allow clients to download your application from a webserver and run it locally.
It's traditionally used with GUI (Swing etc.) apps, but I've used it to run daemon and server processes locally.
It'll handle application updates automatically so your clients will only download a version if they'll need it. Otherwise they'll access their locally cached version.
Linux system implements the Berkeley socket API, so yes, you can open communication for the other machines.
For this, you can use package java.net. For socket connection we can use: Socket, ServerSocket, and SocketAddress.
Socket is used for the client, ServerSocket is used to created a socket server, and SocketAddress is used to provide information that will be used as a target socket.
As the illustration, please find the projects below:
First Project SocketServerApp.java - build this and then run java -jar SocketServerApp.jar
package socketserverapp;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class SocketServerApp {
public static void main(String[] args) throws IOException {
//we defines all the variables we need
ServerSocket server = null;
Socket client = null;
byte[] receivedBuff = new byte[64];
int receivedMsgSize;
try
{
//activate port 8881 as our socket server
server = new ServerSocket(8881);
System.out.println("Server started");
//receiving connection from client
client = server.accept();
System.out.println("Client connected");
}
catch (IOException e)
{
System.out.println(e.getMessage());
System.exit(-1);
}
//prepare data stream
InputStream in = client.getInputStream();
OutputStream out = client.getOutputStream();
//greet user if there is a client connection
String data;
data = "Hello from the Server!";
out.write(data.getBytes());
//accepting data from client and display it in the console.
java.util.Arrays.fill(receivedBuff, (byte)0);
while (true) {
receivedMsgSize = in.read(receivedBuff);
data = new String(receivedBuff);
//if client type "exit", then exit loop and close everything
if (data.trim().equals("exit"))
{
out.write(data.getBytes());
break;
}
java.util.Arrays.fill(receivedBuff, (byte)0);
System.out.println ("Client: " + data);
}
//close all resources before exiting
out.close();
in.close();
client.close();
server.close();
}
}
Second project is the SocketClientApp.java - build this and then run java -jar SocketClientApp.jar
package socketclientapp;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
public class SocketClientApp {
public static void main(String[] args) throws IOException {
Socket client = null;
InputStream in = null;
OutputStream out = null;
byte[] receivedMsg = new byte[64];
try {
client = new Socket("localhost", 8881);
in = client.getInputStream();
out = client.getOutputStream();
} catch (UnknownHostException e) {
System.err.println(e.getMessage());
System.exit(1);
} catch (IOException e) {
System.err.println(e.getMessage());
System.exit(1);
}
String fromServer;
String fromUser;
in.read(receivedMsg);
fromServer = new String(receivedMsg);
System.out.println("Server: " + fromServer);
fromUser = "Hello from Client";
System.out.println("Sent to server: " + fromUser);
out.write(fromUser.getBytes());
fromUser = "exit";
out.write(fromUser.getBytes());
System.out.println("Sent to server: " + fromUser);
out.close();
in.close();
client.close();
}
}
Short to say this is a TCP/IP Communication. This type of communication method is very usual in an enterprise that has many kinds of softwares.
Hope that helps.

Categories

Resources