//程序名: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(){}
public void init(){
resize(400, 300);
setLayout(new BorderLayout());
Panel p1 = new Panel();
log = new TextArea(10, 80);
log.setEditable(false);
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 = getData())!=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 getData(){
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]=-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]=-5;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);
// 在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());
delay(400);
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(){}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
《JAVA程序设计(第二版)》教学课件 (130个子文件)
Customer.java 1KB
Bool.java 516B
URShift.java 278B
InstanceOfDemo.java 271B
Test.java 132B
Java程序设计技术.ppt 2.79MB
第2章.ppt 2.7MB
第3章.ppt 2.41MB
第9章 Java Applet.ppt 2.3MB
第4章 数组、向量和字符串.ppt 2.24MB
第5章 进一步讨论对象和类.ppt 1.64MB
第八章 SWING组件.ppt 1.63MB
第七章 Java的图形用户界面设计.ppt 1.51MB
第10章 Java数据流.ppt 1.4MB
第11章 线程.ppt 1.36MB
第6章 Java语言中的异常.ppt 1.35MB
第12章 Java的网络功能.ppt 1.33MB
12-7.txt 5KB
8-3.txt 5KB
8-11.txt 3KB
8-7.txt 3KB
8-14.txt 3KB
8-9.txt 2KB
8-15.txt 2KB
8-5.txt 2KB
8-12.txt 2KB
7-8.txt 2KB
8-2.txt 2KB
8-10.txt 2KB
8-17.txt 2KB
11-12.txt 2KB
11-5.txt 2KB
8-4.txt 1KB
8-16.txt 1KB
5-2.txt 1KB
7-16.txt 1KB
10-8.txt 1KB
8-8.txt 1KB
11-6.txt 1KB
7-11.txt 1KB
3-4.txt 1KB
8-6.txt 1KB
10-10.txt 1KB
7-9.txt 1KB
4-4.txt 1KB
8-1.txt 1KB
5-14.txt 1KB
2-3.txt 1KB
5-3.txt 990B
7-10.txt 985B
9-9.txt 980B
11-3.txt 951B
10-9.txt 920B
8-13.txt 893B
7-6.txt 878B
12-5.txt 826B
7-7.txt 823B
5-13.txt 786B
7-12.txt 784B
12-3.txt 744B
7-15.txt 743B
12-2.txt 725B
5-11eg.txt 715B
7-5.txt 686B
9-8.txt 657B
7-2.txt 652B
7-14.txt 649B
4-1.txt 624B
5-12.txt 623B
11-2.txt 621B
5-16.txt 593B
6-4eg.txt 587B
6-3.txt 570B
6-2.txt 568B
4-2.txt 567B
10-2.txt 558B
10-3.txt 552B
12-6.txt 547B
11-4.txt 546B
5-10.txt 541B
5-9.txt 530B
5-1.txt 507B
10-1.txt 506B
5-7.txt 500B
3-3.txt 498B
12-4.txt 488B
5-15.txt 482B
10-6.txt 471B
9-3.txt 470B
4-6.txt 458B
5-5.txt 458B
10-5.txt 454B
2-2.txt 444B
7-1.txt 441B
6-3eg.txt 436B
9-7.txt 411B
10-4.txt 394B
3-1.txt 377B
5-6.txt 375B
5-4.txt 357B
共 130 条
- 1
- 2
资源评论
jiningyaojunming
- 粉丝: 0
- 资源: 3
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 卷积神经网络CNN 代码解析.docx
- androidx.media3.exoplayer,libffmpeg库
- androidx.media3.exoplayer,libgav1库
- androidx.media3.exoplayer,libopus库
- androidx.media3.exoplayer,libvp9(libvpx)库
- 机械设计双边工作自动裁断机非常好的设计图纸100%好用.zip
- 2023-04-06-项目笔记 - 第一百二十三阶段 - 4.4.2.121全局变量的作用域-121 -2024.05.04
- vmware官方下载地址
- CNN网络中关于数据预处理的三种方法
- yolov5网络结构图
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功