A must-see Java program

Source: Internet
Author: User
Tags getdate gettext log connect socket string thread trim
Program//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
{
Declaring a member variable

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 a message

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 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.target = connect)
{
Wanttime = 20;
Connect.disable ();
Bye.enable ();
Client =new Thread (this);
Client.start ();
if (enter ())
Tomainmenu ();
Logged =true;
}else if (e.target = = bye)
Bye ();
}
return true;
}

public void Destroy () {
}
public void Paint (Graphics g) {}
public void Start () {}
public void Stop () {
}
}




Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.