文档详情

(完整)聊天系统(客户端、服务器端) java版 完整代码(word文档良心出品).pdf

发布:2024-07-28约7.3千字共6页下载文档
文本预览下载声明

客户端:

importjava.awt.*;

importjava.awt.event.*;

importjava.io.*;

importjava.net.*;

publicclassChatClientextendsFrame{

Sockets=null;

DataOutputStreamdos=null;

DataInputStreamdis=null;

privatebooleanbConnected=false;

TextFieldtfTxt=newTextField();

TextAreataContent=newTextArea();

ThreadtRecv=newThread(newRecvThread());

publicstaticvoidmain(String[]args){

newChatClient().launchFrame();

}

publicvoidlaunchFrame(){

setLocation(400,300);

this.setSize(300,300);

add(tfTxt,BorderLayout.SOUTH);

add(taContent,BorderLayout.NORTH);

pack();

this.addWindowListener(newWindowAdapter(){

@Override

publicvoidwindowClosing(WindowEventarg0){

disconnect();

System.exit(0);

}

});

tfTxt.addActionListener(newTFListener());

setVisible(true);

connect();

tRecv.start();

}

publicvoidconnect(){

try{

s=newSocket(127.0.0.1,8888);

dos=newDataOutputStream(s.getOutputStream());

dis=newDataInputStream(s.getInputStream());

System.out.println(connected!);

bConnected=true;

}catch(UnknownHostExceptione){

e.printStackTrace();

}catch(IOExceptione){

e.printStackTrace();

}

}

publicvoiddisconnect(){

try{

dos.close();

dis.close();

s.close();

}catch(IOExceptione){

e.printStackTrace();

}

/*

try{

bConnected=false;

tRecv.join();

}catch(InterruptedEx

显示全部
相似文档