// Program name: telnetapp. Java
Import java. Applet .*;
Import java. AWT .*;
Import java. Io .*;
Import java.net .*;
Import java. util .*;
Public class telnetapp extends applet implements runnable
{
// Declare member variables
Thread client;
Textarea log;
Textfield hostname;
Textfield userid;
Textfield password;
Label hname;
Label uid;
Label PSD;
Button connect;
Button bye;
Int wanttime;
Boolean logged;
Socket socket = NULL;
Printstream OS;
Datainputstream is;
Public telnetapp ()
{
Resize (400,300 );
Setlayout (New borderlayout ());
Panel p1 = new Panel ();
Log = new textarea (10, 80 );
Log. seteditable (true );
P1.add (log );
Add ("North", P1 );
Panel P2 = new Panel ();
P2.add (hname = new label ("hostname "));
P2.add (hostname = new textfield (20 ));
P2.add (uid = new label ("userid :"));
P2.add (userid = new textfield (10 ));
P2.add (PSD = new label ("Password :"));
P2.add (Password = new textfield (10 ));
Password. setechocharacter ('*');
Add ("center", P2 );
Panel P3 = new Panel ();
P3.add (connect = new button ("Connect "));
P3.add (byE = new button ("bye "));
Bye. Disable ();
Add ("South", P3 );
Logged = false;
}
Public void run ()
{
String fromserver = NULL;
Byte B [] = new byte [3];
B [0] = (byte) 'n ';
While (true ){
If (fromserver = getdate ())! = NULL)
Log. appendtext (fromserver + "\ n ");
If (wanttime <0 ){
Bye ();
Break;
}
If (logged ){
Delay (60*1000 );
Log. settext ("");
Wanttime-= 1;
Senddata (B, 1 );
}
}
}
// Establish a host connection
Private Boolean connecthost (string hostname)
{
Try {
Socket = new socket (hostname, 23 );
OS = new printstream (socket. getoutputstream ());
Is = new datainputstream (socket. getinputstream ());
} Catch (unknownhostexception e ){
Log. settext ("trying to connect to unknown host:" + E );
Return false;
} Catch (exception e ){
Log. settext ("exception:" + E );
Return false;
}
Return true;
}
// Receive information
String getdate ()
{
String fromserver;
Int Len;
Byte B [] = new byte [1000];
Try {
Fromserver = "";
Len = is. Read (B );
Fromserver + = new string (B, 0 );
} Catch (exception e ){
Log. settext ("exception:" + E );
Return NULL;
}
Return fromserver;
}
// Send information
Boolean senddata (byte B [], int Len)
{
Try {
OS. Write (B, 0, Len );
OS. Flush ();
} Catch (exception e ){
Log. settext ("exception:" + E );
Return false;
}
Return true;
}
// Close the connection
Void closesocket ()
{
Try {
OS. Close ();
Is. Close ();
Socket. Close ();
Socket = NULL;
} Catch (exception e ){
Log. settext ("exception:" + E );
}
}
Void tobyte (byte [] B, string S)
{
Int I;
For (I = 0; I <S. Length (); I ++)
B [I] = (byte) S. charat (I );
B [I] = 13;
B [I + 1] = 10;
}
Void negotiate ()
{
Byte B [] = new byte [20];
B [0] =-1; B [1] =-5; B [2] = 24;
Senddata (B, 3 );
Delay (400 );
B [0] =-1; B [1] =-6; B [2] = 24;
B [3] = 0; B [4] = (byte) 'D'; B [5] = (byte) 'E ';
B [6] = (byte) 'C'; B [7] = (byte) '-'; B [8] = (byte) 'V ';
B [9] = (byte) 'T'; B [10] = (byte) '1'; B [11] = (byte) '0 ';
B [12] = (byte) '0'; B [13] = (byte)-1; B [14] =-16;
Senddata (B, 15 );
Delay (400 );
// Other options
B [0] =-1; B [1] =-3; B [2] = 1;
B [3] =-1; B [4] =-3; B [5] = 3;
B [6] =-1; B [7] =-3; B [8] = 31;
B [9] =-1; B [10] =-4; B [11] =-56;
B [12] =-1; B [13] =-5; B [14] = 1;
Senddata (B, 15 );
Delay (400 );
// Login BBS
Tobyte (B, "BBS ");
Senddata (B, 5 );
Delay (400 );
}
Void login (string userid, string password)
{
Byte B [] = new byte [20];
Tobyte (B, userid );
Senddata (B, userid. Length () + 2 );
Delay (400 );
Tobyte (B, password );
Senddata (B, password. Length () + 2 );
Delay (400 );
}
Boolean enter ()
{
If (connecthost (hostname. gettext (). Trim ()))
{
Log. settext ("connected \ n ");
Negotiate ();
Delay (400 );
Login (userid. gettext (). Trim (), password. gettext (). Trim ());
Return true;
} Else return false;
}
Void tomainmenu ()
{
Byte B [] = new byte [20];
For (INT I = 0; I <6; I ++)
{
Tobyte (B ,"");
Senddata (B, 2 );
}
For (INT I = 0; I <1; I ++)
{
B [0] = (byte) 'q ';
Senddata (B, 1 );
Delay (200 );
}
}
Void Bye ()
{
Byte B [] = new byte [20];
For (INT I = 0; I <10; I ++)
{
B [0] = (byte) 'q ';
Senddata (B, 1 );
Delay (300 );
}
B [0] = (byte) 'G ';
Senddata (B, 1 );
Delay (300 );
For (INT I = 0; I <6; I ++)
{
Tobyte (B ,"");
Senddata (B, 2 );
Delay (300 );
}
Client. Stop ();
Client = NULL;
Closesocket ();
Connect. Enable ();
Bye. Disable ();
}
Void delay (INT millisecond)
{
Try {
Thread. Sleep (millisecond );
} Catch (interruptedexception e ){
}
}
Public Boolean action (event E, object Arg)
{
Switch (E. ID ){
Case event. action_event:
If(e.tar get = Connect)
{
Wanttime = 20;
Connect. Disable ();
Bye. Enable ();
Client = new thread (this );
Client. Start ();
If (enter ())
Tomainmenu ();
Logged = true;
} Else if(e.tar get = bye)
Bye ();
}
Return true;
}
Public void destroy (){
}
Public void paint (Graphics g ){}
Public void start (){}
Public void stop (){
}
}
This is a small application of Applet. It is about the BBS logon interface. You can refer to it for reference.