Clientc=clients.get(i);
c.send(str);
}
}
}catch(EOFExceptione){
System.out.println("Clientclosed!
");
}catch(IOExceptione){
e.printStackTrace();
}finally{
try{
if(dis!
=null)
dis.close();
if(dos!
=null)
dos.close();
if(s!
=null){
s.close();
}
}catch(IOExceptione1){
e1.printStackTrace();
}
}
}
}
}
代码二(ChatClient类)
packageTest07;
importjava.awt.BorderLayout;
importjava.awt.Frame;
importjava.awt.Label;
importjava.awt.TextArea;
importjava.awt.TextField;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjava.awt.event.WindowAdapter;
importjava.awt.event.WindowEvent;
importjava.io.DataInputStream;
importjava.io.DataOutputStream;
importjava.io.EOFException;
importjava.io.FileOutputStream;
importjava.io.IOException;
import.Socket;
import.SocketException;
import.UnknownHostException;
importjava.util.Date;
importjava.util.Scanner;
publicclassChatClientextendsFrame{
privateStringstr1;
Framef=newFrame();
Sockets=null;
Dated=null;
DataOutputStreamdos=null;
DataInputStreamdis=null;
FileOutputStreamfos=null;
privatebooleanbConnected=false;
TextFieldtfTxt=newTextField();
TextFieldtfTxt2=newTextField();
TextAreataContent=newTextArea();
ThreadtRecv=newThread(newRecvThread());
Scannerinput=newScanner(System.in);
//publicstaticvoidmain(String[]args)
//{
//newChatClient().launchFrame();
//}
publicvoidlaunchFrame(Stringstr){
str1=str;
f=newFrame(str);
f.setLocation(400,300);
f.setSize(300,300);
f.add(tfTxt,BorderLayout.SOUTH);
f.add(taContent,BorderLayout.NORTH);
pack();
f.addWindowListener(newWindowAdapter(){
@Override
publicvoidwindowClosing(WindowEventarg0){
disconnect();
System.exit(0);
}
});
tfTxt.addActionListener(newTFListener());
f.setVisible(true);
connect();
tRecv.start();
}
publicvoidconnect(){
try{
s=newSocket("127.0.0.1",8888);
dos=newDataOutputStream(s.getOutputStream());
dis=newDataInputStream(s.getInputStream());
fos=newFileOutputStream("E:
\\Text.txt");
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();
}
}
privateclassTFListenerimplementsActionListener{
publicvoidactionPerformed(ActionEvente){
d=newDate();
Stringstr=str1+":
"+d+"\n"+tfTxt.getText().trim()+"\n";
//taContent.setText(str);
tfTxt.setText("");
try{
//System.out.println(s);
dos.writeUTF(str);
dos.flush();
//dos.close();
}catch(IOExceptione1){
e1.printStackTrace();
}
}
}
privateclassRecvThreadimplementsRunnable{
publicvoidrun(){
try{
while(bConnected){
Stringstr=dis.readUTF();
//System.out.println(str);
taContent.setText(taContent.getText()+str+'\n');
}
}catch(SocketExceptione){
System.out.println("退出了,bye!
");
}catch(EOFExceptione){
System.out.println("推出了,bye-bye!
");
}catch(IOExceptione){
e.printStackTrace();
}
}
}
}
代码三(Test类)
packageTest07;
importjava.awt.BorderLayout;
importjava.awt.Button;
importjava.awt.Frame;
importjava.awt.GridLayout;
importjava.awt.Label;
importjava.awt.Panel;
importjava.awt.TextField;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjava.awt.event.WindowAdapter;
importjava.awt.event.WindowEvent;
publicclassTest{
privateStringstr;
Framef=newFrame();
LabelL=newLabel("请输入用户名");
LabelL2=newLabel("请输入密码");
Buttonb=newButton("登录");
TextFieldtf=newTextField(15);
TextFieldtf1=newTextField(15);
Panelp1=newPanel();
Panelp2=newPanel();
Panelp3=newPanel();
publicvoidmethod(){
p1.add(L);
p1.add(tf);
p2.add(L2);
p2.add(tf1);
p3.add(b);
f.setLocation(300,300);
f.setSize(300,200);
f.setLayout(newGridLayout(3,1));
f.add(p1);
f.add(p2);
f.add(p3);
f.addWindowListener(newWindowAdapter(){
publicvoidwindowClosing(WindowEventarg0){
System.exit(0);
}
});
b.addActionListener(newActionListener(){
@Override
publicvoidactionPerformed(ActionEvente){
str=tf.getText();
if(tf1.getText().equals("123456")){
f.setVisible(false);
newChatClient().launchFrame(str);
}
}
});
f.setVisible(true);
}
publicstaticvoidmain(String[]args){
newTest().method();
}
}
本系统是通过java语言开发,通过Eclipse工具进行编写,分成了三个部分,分别是ChatClient(客户端)、ChatServer(服务器端)、Test(测试端),用到了tcp/Ip的知识,首先系统先运行起来ChatServer端,它提供了一一个端口号供客户端连接,客户端通过本地的IP地址和ChatServer提供的端口进行连接,在这里用到了Socket知识,此类实现客户端套接字(也可以就叫“套接字”)。
套接字是两台机器间通信的端点。
也用到ServerSocket知识,此类实现服务器套接字。
服务器套接字等待请求通过网络传入。
它基于该请求执行某些操作,然后可能向请求者返回结果。
当服务端启动后ServerSocket的accept()方法就开始对客户端的Socket开始监听并一直保持阻塞状态,一旦发现有客户端的Socket链接时立马执行accept()方法,之后服务端和客户端就可以进行实时通讯了,具体如下图所示:
(1)首先必须先启动ChatServer()类;
(2)Test()类启动客户端程序;
模仿了qq输入用户名和密码;
一个客户端已经连接等待了,
接下来我们以同样的步骤再启动一个客户端让它们之间;
接下来可以开始聊天了;
过程中俩客户端之间的聊天记录都会被完全的记录下来,包括说话人和说话时的时间日期;
最后点击右上方的叉叉表示此客户端退出;
(四)总结:
通过这次聊天系统的设计和编写,让我进一步的了解了TCP/IP的知识和应用,同时也让我进一步的学会编写程序和独立测试,独立排查的能力,期间也遇到很多不懂得问题和知识,通过查阅资料上网XX以及同学之间的交流学习一一的克服,对我以后的学习和工作会有深远的影响。