|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
j2EE和asp比较,其实也没什么比的,原因和我上面说那些比较差不了多少,也是稳定性,安全性,J2EE比asp高,速度上比不过asp,asp也是延续着它的拖拽控件的方法,提高速度。一个很复杂的局域网谈天工具的服务器端
/*
*Createdon2005-3-18
*
*TODOTochangethetemplateforthisgeneratedfilegoto
*Window-Preferences-Java-CodeStyle-CodeTemplates
*/
packagenetChat;
importjavax.swing.*;
importjava.io.*;
importjava.net.*;
importjava.awt.*;
importjava.awt.event.*;
/**
*@authorAppleTian
*
*TODOTochangethetemplateforthisgeneratedtypecommentgoto
*Window-Preferences-Java-CodeStyle-CodeTemplates
*/
publicclassserverextendsJFrame{
privateJTextFieldmessageBox;
privateJTextAreainfBox;
privateObjectInputStreaminput;
privateObjectOutputStreamoutput;
privateServerSocketserver;
privateSocketserverSocket;
publicserver(){
super("server");
Containerc=getContentPane();
messageBox=newJTextField("");
messageBox.setEnabled(false);
messageBox.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEvente){
sendData(e.getActionCommand());
}
});
c.add(messageBox,BorderLayout.NORTH);
infBox=newJTextArea();
infBox.setFont(newFont("Serif",Font.BOLD,12));
c.add(newJScrollPane(infBox),BorderLayout.CENTER);
setSize(500,400);
setVisible(true);
}
/**
*@paramactionCommand
*/
protectedvoidsendData(StringactionCommand){
try{
output.writeObject(actionCommand);
output.flush();
infBox.append("
send<<"+actionCommand+">>toclient...
");
}
catch(IOExceptione){
infBox.append("
ExceptionIOInSendDataFunction...");
}
}
publicvoidrunServer(){
try{
server=newServerSocket(5000,100);
while(true){
waitForConnection();
getStreams();
processConnection();
closeConnection();
}
}
catch(EOFExceptione){
infBox.append("
ErrorinrunServerFuction");
}
catch(IOExceptione){
e.printStackTrace();
}
}
/**
*
*/
privatevoidcloseConnection()throwsIOException{
infBox.append("
closeconnection...");
messageBox.setEnabled(false);
output.close();
input.close();
serverSocket.close();
}
/**
*
*/
privatevoidprocessConnection()throwsIOException{
Stringmg="
connectionsucceful";
messageBox.setEnabled(true);
output.writeObject(mg);
output.flush();
do{
try{
mg=(String)input.readObject();
infBox.append("
sendtoclient:"+mg);
infBox.setCaretPosition(infBox.getText().length());
}
catch(ClassNotFoundExceptione){
infBox.append("
Unkownclassreceived...");
}
}while(mg!="end");
}
/**
*
*/
privatevoidgetStreams()throwsIOException{
output=newObjectOutputStream(serverSocket.getOutputStream());
output.flush();
input=newObjectInputStream(serverSocket.getInputStream());
infBox.append("
GetStreams...");
}
/**
*
*/
privatevoidwaitForConnection()throwsIOException{
infBox.append("waitforaClient...");
serverSocket=server.accept();
infBox.append("connectedby"+serverSocket.getInetAddress().getHostName());
}
publicstaticvoidmain(String[]args){
servermy=newserver();
my.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
my.runServer();
}
}
客户端
/*
*Createdon2005-3-18
*
*TODOTochangethetemplateforthisgeneratedfilegoto
*Window-Preferences-Java-CodeStyle-CodeTemplates
*/
packagenetChat;
importjavax.swing.*;
importjava.io.*;
importjava.net.*;
importjava.awt.*;
importjava.awt.event.*;
/**
*@authorAppleTian
*
*TODOTochangethetemplateforthisgeneratedtypecommentgoto
*Window-Preferences-Java-CodeStyle-CodeTemplates
*/
publicclassclientextendsJFrame{
privateJTextFieldmessageBox;
privateJTextAreainfBox;
privateObjectInputStreaminput;
privateObjectOutputStreamoutput;
privateStringmg="";
privateStringchatServer;
privateSocketclient;
publicclient(Stringhost){
super("client....");
chatServer=host;
Containerc=getContentPane();
messageBox=newJTextField();
messageBox.setEnabled(false);
messageBox.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEvente){
sendData(e.getActionCommand());
}
});
c.add(messageBox,BorderLayout.NORTH);
infBox=newJTextArea();
c.add(infBox,BorderLayout.CENTER);
setSize(500,400);
setVisible(true);
}
/**
*@paramactionCommand
*/
protectedvoidsendData(StringactionCommand){
try{
mg=messageBox.getText();
output.writeObject(mg);
output.flush();
infBox.append("
sendtoserver:"+mg);
}catch(IOExceptione){
infBox.append("
IOExceptioninsendData..");
}
}
publicvoidrunClient(){
try{
connectToServer();
getStreams();
processConnection();
closeConnection();
}catch(IOExceptione){
infBox.append(e.toString());
}
}
/**
*
*/
privatevoidcloseConnection()throwsIOException{
//TODOAuto-generatedmethodstub
infBox.append("
close...");
output.close();
input.close();
client.close();
}
/**
*
*/
privatevoidprocessConnection()throwsIOException{
//TODOAuto-generatedmethodstub
messageBox.setEnabled(true);
do{
try{
mg=(String)input.readObject();
infBox.append("
Fromserver:"+mg);
infBox.setCaretPosition(infBox.getText().length());
}catch(ClassNotFoundExceptione){
infBox.append("classNotFound...
");
}
}while(mg!="end");
}
/**
*
*/
privatevoidgetStreams()throwsIOException{
//TODOAuto-generatedmethodstub
output=newObjectOutputStream(client.getOutputStream());
output.flush();
input=newObjectInputStream(client.getInputStream());
infBox.append("gettheIOStream..
");
}
/**
*
*/
privatevoidconnectToServer()throwsIOException{
//TODOAuto-generatedmethodstub
infBox.append("tryconnectingtoserver...
");
client=newSocket(InetAddress.getByName(chatServer),5000);
infBox.append("connecttoserver:"+client.getInetAddress().getHostName()+"
");
}
publicstaticvoidmain(String[]args){
clientp;
if(args.length==0)
p=newclient("127.0.0.1");
else
p=newclient(args[0]);
p.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
p.runClient();
}
}
认真的记,感觉很紧张根本就没有时间和能力,来对技术知识点进行思考。这样课下就只能对知识进行简单的理解,其实简单的理解就是记忆课堂上讲的知识点, |
|