|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
你通过从书的数量和开发周期及运行速度来证明:net和ruby要比java简单。程序//程序名:TelnetApp.Java
importjava.applet.*;
importjava.awt.*;
importjava.io.*;
importjava.net.*;
importjava.util.*;
publicclassTelnetAppextendsAppletimplementsRunnable
{
//声明成员变量
Threadclient;
TextArealog;
TextFieldhostname;
TextFielduserid;
TextFieldpassword;
Labelhname;
Labeluid;
Labelpsd;
Buttonconnect;
Buttonbye;
intwantTime;
booleanlogged;
Socketsocket=null;
PrintStreamos;
DataInputStreamis;
publicTelnetApp()
{
resize(400,300);
setLayout(newBorderLayout());
Panelp1=newPanel();
log=newTextArea(10,80);
log.setEditable(true);
p1.add(log);
add("North",p1);
Panelp2=newPanel();
p2.add(hname=newLabel("Hostname"));
p2.add(hostname=newTextField(20));
p2.add(uid=newLabel("Userid:"));
p2.add(userid=newTextField(10));
p2.add(psd=newLabel("Password:"));
p2.add(password=newTextField(10));
password.setEchoCharacter(*);
add("Center",p2);
Panelp3=newPanel();
p3.add(connect=newButton("Connect"));
p3.add(bye=newButton("Bye"));
bye.disable();
add("South",p3);
logged=false;
}
publicvoidrun()
{
StringfromServer=null;
byteb[]=newbyte[3];
b[0]=(byte)n;
while(true){
if((fromServer=getDate())!=null)
log.appendText(fromServer+"
");
if(wantTime<0){
bye();
break;
}
if(logged){
delay(60*1000);
log.setText("");
wantTime-=1;
sendData(b,1);
}
}
}
//创建主机毗连
privatebooleanconnectHost(StringhostName)
{
try{
socket=newSocket(hostName,23);
os=newPrintStream(socket.getOutputStream());
is=newDataInputStream(socket.getInputStream());
}catch(UnknownHostExceptione){
log.setText("Tryingtoconnecttounknownhost:"+e);
returnfalse;
}catch(Exceptione){
log.setText("Exception:"+e);
returnfalse;
}
returntrue;
}
//吸收信息
StringgetDate()
{
StringfromServer;
intlen;
byteb[]=newbyte[1000];
try{
fromServer="";
len=is.read(b);
fromServer+=newString(b,0);
}catch(Exceptione){
log.setText("Exception:"+e);
returnnull;
}
returnfromServer;
}
//发送信息
booleansendData(byteb[],intlen)
{
try{
os.write(b,0,len);
os.flush();
}catch(Exceptione){
log.setText("Exception:"+e);
returnfalse;
}
returntrue;
}
//封闭毗连
voidcloseSocket()
{
try{
os.close();
is.close();
socket.close();
socket=null;
}catch(Exceptione){
log.setText("Exception:"+e);
}
}
voidtoByte(byte[]b,Strings)
{
inti;
for(i=0;i<s.length();i++)
b[i]=(byte)s.charAt(i);
b[i]=13;
b[i+1]=10;
}
voidnegotiate()
{
byteb[]=newbyte[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);
//loginbbs
toByte(b,"bbs");
sendData(b,5);
delay(400);
}
voidlogin(Stringuserid,Stringpassword)
{
byteb[]=newbyte[20];
toByte(b,userid);
sendData(b,userid.length()+2);
delay(400);
toByte(b,password);
sendData(b,password.length()+2);
delay(400);
}
booleanenter()
{
if(connectHost(hostname.getText().trim()))
{
log.setText("connected
");
negotiate();
delay(400);
login(userid.getText().trim(),password.getText().trim());
returntrue;
}elsereturnfalse;
}
voidtoMainmenu()
{
byteb[]=newbyte[20];
for(inti=0;i<6;i++)
{
toByte(b,"");
sendData(b,2);
}
for(inti=0;i<1;i++)
{
b[0]=(byte)q;
sendData(b,1);
delay(200);
}
}
voidbye()
{
byteb[]=newbyte[20];
for(inti=0;i<10;i++)
{
b[0]=(byte)q;
sendData(b,1);
delay(300);
}
b[0]=(byte)g;
sendData(b,1);
delay(300);
for(inti=0;i<6;i++)
{
toByte(b,"");
sendData(b,2);
delay(300);
}
client.stop();
client=null;
closeSocket();
connect.enable();
bye.disable();
}
voiddelay(intmillisecond)
{
try{
Thread.sleep(millisecond);
}catch(InterruptedExceptione){
}
}
publicbooleanaction(Evente,Objectarg)
{
switch(e.id){
caseEvent.ACTION_EVENT:
if(e.target==connect)
{
wantTime=20;
connect.disable();
bye.enable();
client=newThread(this);
client.start();
if(enter())
toMainmenu();
logged=true;
}elseif(e.target==bye)
bye();
}
returntrue;
}
publicvoiddestroy(){
}
publicvoidpaint(Graphicsg){}
publicvoidstart(){}
publicvoidstop(){
}
}
其实你不用Struts,spring这些工具,直接用jsp,servlet能够很方便地写出来,而且,可以根据个人的水平、爱好,有很多方案。而struts,spring这些工具的出来。 |
|