🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.jbuilder.java.api
Article #956

java Sockets and telnet

#956
From: Alan Shiers
Date: Wed, 10 Jul 2002 17:21
211 lines
6538 bytes
Hi,

This posting is particularly aimed at John McGrath. I submitted this
same posting as my last post in the thread "an idea to consider".  Since
no one appears to be answering further to that thread, I thought to
start a new one. Hope you don't mind. I'd like to get some answers to my
latest question regarding the use of telnet and my test program using
Sockets.

I've been experimenting with Windows own telnet program and my test
program.

I've changed code in my test program to deal only with UTF-8 strings.
I was amazed that I was actually able to log onto my test program with
telnet :)

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

Why isn't the command I type 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 changed 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();
      }
    }

  }
}
Technical Details
Message-ID: <3D2C6CFC.B80E2196@hfx.eastlink.ca>
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-05!sn-xit-01!sn-xit-08!supernews.com!newsfeed.news2me.com!newsfeed.wirehub.nl!PeerFeed.KWeb.on.ca!dnews

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 1089d0,cb56a48db758956d,start
X-Google-Attributes: gid1089d0,public
X-Google-ArrivalTime: 2002-07-10 10:42:13 PST
Message-ID: <3D2C6CFC.B80E2196@hfx.eastlink.ca>
Date: Wed, 10 Jul 2002 13:21:00 -0400
From: Alan Shiers <ashiers@hfx.eastlink.ca>
Organization: Mustard Seed Software
X-Mailer: Mozilla 4.78 [en] (Win98; U)
X-Accept-Language: en
MIME-Version: 1.0
Newsgroups: borland.public.jbuilder.java.api
Subject: java Sockets and telnet
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
NNTP-Posting-Host: 24.222.101.2
X-Trace: dnews 1026317915 24.222.101.2 (10 Jul 2002 09:18:35 -0700)
Lines: 211
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-05!sn-xit-01!sn-xit-08!supernews.com!newsfeed.news2me.com!newsfeed.wirehub.nl!PeerFeed.KWeb.on.ca!dnews