Java Sockets and Telnet


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: Java Sockets and Telnet

  1. #1
    Alan Shiers Guest

    Java Sockets and Telnet

    Hi,

    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?

    Please advise,

    Alan

    ***************************************************

    import java.net.*;
    import java.io.*;

    public class LocalSocketListener extends Thread
    {

    protected String ApplicationName = "";
    private Application1 myApp = null;
    private ServerSocket socket = null;
    private SocketListener sl = null;

    public LocalSocketListener(String appName, Application1 application,
    int port)
    {
    ApplicationName = appName;
    myApp = application;

    //This class will only listen to the loopback address
    //and allow only one logon.
    //boolean isLocalClient = false;
    try
    {

    socket = new ServerSocket(port, 10,
    InetAddress.getByName("127.0.0.1"));
    //InetAddress addr = socket.getInetAddress();
    //isLocalClient = addr.getHostAddress().equals( "127.0.0.1" )
    //|| addr.equals( InetAddress.getLocalHost() );
    //System.out.println("isLocalClient: " + isLocalClient + "\n" +
    // "addr.getHostAddress(): " +
    addr.getHostAddress() + "\n" +
    // "InetAddress.getLocalHost(): " +
    InetAddress.getLocalHost());
    }
    catch(UnknownHostException uhe)
    {
    uhe.printStackTrace();
    }
    catch(IOException io)
    {
    io.printStackTrace();
    }

    }

    public void run()
    {
    //System.out.println("Waiting for external program to log on.");
    while(true)
    {

    try
    {
    sl = new SocketListener(socket.accept(), this);
    sl.start();
    }
    catch(SocketException se)
    {
    se.printStackTrace();
    break;
    }
    catch(InterruptedIOException iioe)
    {
    iioe.printStackTrace();
    break;
    }
    catch(IOException e)
    {
    //System.out.println("IOException in execute()");
    e.printStackTrace();
    break;
    }
    catch(Exception exc)
    {
    //System.out.println("Exception in execute()");
    exc.printStackTrace();
    break;
    }
    }
    }

    class SocketListener extends Thread
    {
    private Socket connection;
    private LocalSocketListener lsl;
    private DataInputStream input;
    private DataOutputStream output;

    //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();
    }
    }

    }
    }
    Attached Files Attached Files

  2. #2
    MarkN Guest

    Re: Java Sockets and Telnet


    Alan,
    Word of advice - Telnet in Windows is a major security risk.

    Mark

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center