🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Re: an idea to consider

#955
From: Alan Shiers
Date: Tue, 09 Jul 2002 22:16
213 lines
6909 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

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

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

/**
 * Title:
 * Description:
 * Copyright:    Copyright (c) 2000
 * Company:
 * @author
 * @version 1.0
 */

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: <3D2B60C3.9D9006FA@hfx.eastlink.ca>
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-06!supernews.com!64.152.100.70.MISMATCH!ps01-sjc1!news.webusenet.com!newsfeed1.cidera.com!Cidera!cyclone.socal.rr.com!newsfeed.news2me.com!newsfeed2.earthlink.net!newsfeed.earthlink.net!lsanca1-snf1!news.gtei.net!enterprise!news.xtra.co.nz!newsfeeds.ihug.co.nz!ihug.co.nz!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 14:37:21 PST
Message-ID: <3D2B60C3.9D9006FA@hfx.eastlink.ca>
Date: Tue, 09 Jul 2002 18:16:35 -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 1026249247 24.222.101.2 (9 Jul 2002 14:14:07 -0700)
Lines: 213
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-06!supernews.com!64.152.100.70.MISMATCH!ps01-sjc1!news.webusenet.com!newsfeed1.cidera.com!Cidera!cyclone.socal.rr.com!newsfeed.news2me.com!newsfeed2.earthlink.net!newsfeed.earthlink.net!lsanca1-snf1!news.gtei.net!enterprise!news.xtra.co.nz!newsfeeds.ihug.co.nz!ihug.co.nz!dnews