🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Re: an idea to consider

#954
From: Alan Shiers
Date: Tue, 09 Jul 2002 20:34
189 lines
6196 bytes
Hi,

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

Then, in the test program, I enter in 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

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

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.
    String address = "127.0.0.1";
    try
    {
      socket = new ServerSocket(port, 10,
InetAddress.getByName(address));
    }
    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\nYeah...What do ya
want?\r\n\r\n");
      }
      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;
          }

        }
        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: <3D2B48C6.A859C6AD@hfx.eastlink.ca>
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-06!supernews.com!news.cs.utwente.nl!newshunter!cosy.sbg.ac.at!newsrouter.chello.at!news01.chello.no!Norway.EU.net!uio.no!logbridge.uoregon.edu!newsfeed.wirehub.nl!PeerFeed.KWeb.on.ca!dnews
References: <3D279D25.59AA52E@hfx.eastlink.ca> <3d27a0b2_2@dnews> <3D2861BB.63220DF5@hfx.eastlink.ca> <MPG.17923b35889aef9498b7f9@newsgroups.borland.com> <3D28A22A.89CB73DC@hfx.eastlink.ca> <MPG.1793793e26a87ab98b7fd@newsgroups.borland.com> <3D29FE4E.C3EEF5B5@hfx.eastlink.ca> <MPG.1793eed4e5e42ee798b80b@newsgroups.borland.com> <3D2A48B8.29314379@hfx.eastlink.ca> <MPG.17946a495a7d5f5e98b812@newsgroups.borland.com> <3D2B05DA.EE3D2DC2@hfx.eastlink.ca> <3d2b2be9_2@dnews>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 1089d0,4157c064c52214d4
X-Google-Attributes: gid1089d0,public
X-Google-ArrivalTime: 2002-07-09 12:37:04 PST
Message-ID: <3D2B48C6.A859C6AD@hfx.eastlink.ca>
Date: Tue, 09 Jul 2002 16:34:14 -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: Re: an idea to consider
References: <3D279D25.59AA52E@hfx.eastlink.ca> <3d27a0b2_2@dnews> <3D2861BB.63220DF5@hfx.eastlink.ca> <MPG.17923b35889aef9498b7f9@newsgroups.borland.com> <3D28A22A.89CB73DC@hfx.eastlink.ca> <MPG.1793793e26a87ab98b7fd@newsgroups.borland.com> <3D29FE4E.C3EEF5B5@hfx.eastlink.ca> <MPG.1793eed4e5e42ee798b80b@newsgroups.borland.com> <3D2A48B8.29314379@hfx.eastlink.ca> <MPG.17946a495a7d5f5e98b812@newsgroups.borland.com> <3D2B05DA.EE3D2DC2@hfx.eastlink.ca> <3d2b2be9_2@dnews>
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
NNTP-Posting-Host: 24.222.101.2
X-Trace: dnews 1026243106 24.222.101.2 (9 Jul 2002 12:31:46 -0700)
Lines: 189
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-06!supernews.com!news.cs.utwente.nl!newshunter!cosy.sbg.ac.at!newsrouter.chello.at!news01.chello.no!Norway.EU.net!uio.no!logbridge.uoregon.edu!newsfeed.wirehub.nl!PeerFeed.KWeb.on.ca!dnews
X-Received-Date: Tue, 09 Jul 2002 21:34:45 MET DST (news01.chello.no)