The Java(tm) Telnet Applet is a fully featured telnet/SSH program that allows users to connect and login to remote hosts via the Internet or an Intranet using only their WWW Browser.
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);