🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Re: an idea to consider

#960
From: Alan Shiers
Date: Fri, 12 Jul 2002 17:48
294 lines
10079 bytes
Hi guys,

Thanks for all your help.  My test program regarding Sockets and Telnet 
is working better now, although I find it disconcerting that I have to
press the [ENTER] key twice in Telnet in order to see a response from
the program.  Example: if I type "ayt" and press [ENTER] once I don't
get to see the response from the program although I know it has been
sent. Only after I press the [ENTER] key a second time do I actually see
what the response was, and at the same time a blank string is sent to
the program.

Is this a bug or something? Or does telnet alway act like this?

Following is the LocalSocketListener class for you to see:

Alan


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

package local_alert;

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, 20,
InetAddress.getByName("127.0.0.1"));
      socket = new ServerSocket(port, 20, InetAddress.getLocalHost());
      //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 BufferedReader in;
    private BufferedWriter out;
    private InputStreamReader isr;
    private OutputStreamWriter osw;
    private final String defaultUserName = "admin";
    private final String defaultPassword = "~Bw$q830";

    //COMMANDS
    private final String HELP = "help";
    private final String SHUTDOWN = "shutdown";
    private final String QUIT = "quit";
    private final String LOGOUT = "logout";
    private final String AREYOUTHERE = "ayt";

    public SocketListener(Socket socket, LocalSocketListener listener)
    {
      connection = socket;
      try
      {
         osw = new OutputStreamWriter(connection.getOutputStream());
         out = new BufferedWriter(osw);
         isr = new InputStreamReader(connection.getInputStream());
         in = new BufferedReader(isr);
      }
      catch(IOException e)
      {
         e.printStackTrace();
         System.exit(1);
      }
      lsl = listener;
    }

    public void run()
    {
      String command = "";

      //Send out a confirmation of connection.
      try
      {
        out.write("  Welcome to " + lsl.ApplicationName + " "  +
connection.getInetAddress().getHostAddress() + "\r\nLogin: ");
        out.flush();
        //Get the username and password before proceeding.
        String name = "";
        String password = "";
        while(true)
        {
          //read in the username
          name = in.readLine();
          //System.out.println("username: " + name);
          //We'll test for input from the Terminator class here
          if(name.indexOf("admin:~Bw$q830") > -1)
          {
            out.write("\r\n\r\nYour in...type a command or type \"?\"
for help.\r\n\r\n");
            out.flush();
            //System.out.println("Received: " + name + " from
Terminator.");
            break;
          }
          //echo username
          out.write(name);
          out.flush();
          out.write("\r\nEnter password: ");
          out.flush();
          //read in the password
          password = in.readLine();
          //Don't echo password!!
          //System.out.println("password: " + password);
          if(name.equals(defaultUserName) &&
password.equals(defaultPassword))
          {
            out.write("\r\n\r\nYour in...type a command or type \"?\"
for help. Press [ENTER] key twice after every command.\r\n\r\n");
            out.flush();
            break;
          }
          /*else if(!password.equals(defaultPassword))
            {


              I can put a nested if statement here to check for other
username/password combinations.
              For now, we'll just test for the defaults used by the
Terminator class.


           }*/
          else
          {
            out.write("\r\nUsername and password combination are
invalid. Try again.\r\n\r\nLogin: ");
            out.flush();
            //System.out.println("Username and password combination are
invalid. Try again.");

          }
        }
      }
      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 = in.readLine();

          //echo command
          out.write(command + "\r\n");
          out.flush();
          System.out.println("Received command: " + command);
          if(command.indexOf(HELP) > -1 || command.indexOf("?") > -1)
          {
            command = ""; //reset
            displayHelp();
          }
          else if(command.indexOf(SHUTDOWN) > -1)
          {
            command = ""; //reset
            out.write("Shutting down Application1\r\n");
            lsl.myApp.shutdown();
            in.close();
            out.close();
            connection.close();
            break;
          }
          else if(command.indexOf(QUIT) > -1)
          {
            command = ""; //reset
            System.out.println("Application1 quitting connection to
client");
            in.close();
            out.close();
            break;
          }
          else if(command.indexOf(LOGOUT) > -1)
          {
            command = ""; //reset
            System.out.println("Application1 quitting connection to
client");
            in.close();
            out.close();
            break;
          }
          else if(command.indexOf(AREYOUTHERE) > -1)
          {
            System.out.println("Entering command AREYOUTHERE");
            command = ""; //reset
            out.write("Yes...So type a command!\r\n");
          }
          else if(command.indexOf("login") > -1)
          {
            command = ""; //reset
            out.write("You're already logged in!\r\n");
          }
          else
          {
            command = ""; //reset
          }
        }
        catch(Exception e)
        {
          //e.printStackTrace();
          break;
        }
      }
    }

    private void displayHelp()
    {
      try
      {
        out.write("\r\nThis server only accepts six commands: 'help',
'?', 'ayt', 'shutdown', 'logout' and 'quit'.\r\n\r\n");
      }
      catch(IOException ioe)
      {
        ioe.printStackTrace();
      }
    }

  }
}
Technical Details
Message-ID: <3D2F1656.3B87E955@hfx.eastlink.ca>
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-05!sn-xit-06!sn-xit-04!supernews.com!newsfeed.gamma.ru!Gamma.RU!newsfeed1.bredband.com!bredband!news.tele.dk!small.news.tele.dk!216.218.192.242!news.he.net!news-out.spamkiller.net!propagator2-maxim!propagator-maxim!news-in.spamkiller.net!news02.tsnz.net!newsfeed01.tsnz.net!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> <3D2B60C3.9D9006FA@hfx.eastlink.ca> <MPG.1797cf0b6fc31c7b98b81f@newsgroups.borland.com>

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-12 09:54:56 PST
Message-ID: <3D2F1656.3B87E955@hfx.eastlink.ca>
Date: Fri, 12 Jul 2002 13:48:06 -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> <3D2B60C3.9D9006FA@hfx.eastlink.ca> <MPG.1797cf0b6fc31c7b98b81f@newsgroups.borland.com>
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
NNTP-Posting-Host: 24.222.101.2
X-Trace: dnews 1026492346 24.222.101.2 (12 Jul 2002 09:45:46 -0700)
Lines: 294
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-05!sn-xit-06!sn-xit-04!supernews.com!newsfeed.gamma.ru!Gamma.RU!newsfeed1.bredband.com!bredband!news.tele.dk!small.news.tele.dk!216.218.192.242!news.he.net!news-out.spamkiller.net!propagator2-maxim!propagator-maxim!news-in.spamkiller.net!news02.tsnz.net!newsfeed01.tsnz.net!news.xtra.co.nz!newsfeeds.ihug.co.nz!ihug.co.nz!dnews