I've been experimenting with java's Sockets with Windows own telnet
program with my test program (see below).
I was amazed that I was actually able to log onto telnet with my test
program
This is a major lightbulb moment for me!
I set up my test program so that upon connection I output a welcome
message to the client (telnet). That worked...YES!
Then, in the test program, I enter a while loop which is supposed to
read in commands: String command = input.readUTF();
In telnet, I type "help" [ENTER]. Nothing happens...
"help" is one of the valid commands defined in the test program.
Why isn't the command I typed in telnet being sent to my test program
after I hit the ENTER key? What am I doing wrong? Do I have to tell
the telnet program that I am expecting input from the user or something
like that?
Below is the code for my test program. What do I need to write
in my code in the test program so that telnet knows enough to send
commands typed in by the user?
//COMMANDS
private final String HELP = "help";
private final String SHUTDOWN = "shutdown";
public SocketListener(Socket socket, LocalSocketListener listener)
{
connection = socket;
try
{
input = new DataInputStream(connection.getInputStream());
output = new DataOutputStream(connection.getOutputStream());
}
catch(IOException e)
{
e.printStackTrace();
System.exit(1);
}
lsl = listener;
}
public void run()
{
String command = "";
//Send out a confirmation of connection.
try
{
output.writeUTF(" Welcome to " + lsl.ApplicationName + " " +
connection.getInetAddress().getHostAddress() + "\r\n\r\nType command:
");
}
catch(IOException io){io.printStackTrace();}
while(true)
{
try
{
//Get the name of the application being addressed.
//If it is this application that is being addressed,
//then get the command sent by the calling program.
System.out.println("waiting for a command...");
command = input.readUTF();
System.out.println("command: " + command);
if(command.equals(HELP))
{
displayHelp();
}
else if(command.equals(SHUTDOWN))
{
lsl.myApp.shutdown();
input.close();
output.close();
connection.close();
break;
}
else
{
output.writeUTF("\r\n500 Command not recognized\r\n");
}
}
catch(Exception e)
{
//e.printStackTrace();
break;
}
}
}
private void displayHelp()
{
try
{
output.writeUTF("This server only accepts two commands: 'help'
and 'shutdown'\r\n\r\n");
}
catch(IOException ioe)
{
ioe.printStackTrace();
}
}
Bookmarks