//程序名 :telnetapp.java
import java.applet.*;
import java.awt.*;
import java.io.*;
import java.net.*;
import java.util.*;
public class telnetapp extends applet implements runnable
{
//声明成员变量
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);
}
}
}
//建立主机连接
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;
}
// 接收信息
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;
}
//发送信息
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;
}
//关闭连接
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);
//其他选项
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(){
}
}