版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、java聊天程序源代码,简单点,尽量带注释,注释越多越好谢谢悬赏分:20 - 解决时间:2010-8-2 16:12 哥哥姐姐们尽量带多点的注释,我是初学者,谢谢了 提问者: 无伤一号 - 二级最佳答案服务端:import java.io.*;import .*;import java.util.*;public class ChatServer boolean stat = false; ServerSocket ss = null; List<Client> clients = new ArrayList<Client>();/用于存客户端 public stati
2、c void main(String args) new ChatServer().start(); public void start() try ss = new ServerSocket(8888); stat = true; catch(BindException e) /Sever端已经运行,当重复运行时抛异常 System.out.println("端口正在使用中。"); System.out.println("请关掉相关程序并重新运行服务器!"); /还会抛别的异常,所以直接关闭窗口 System.exit(0); catch(IOExce
3、ption e) e.printStackTrace(); try while(stat) Socket s = ss.accept();System.out.println("a client connected!" ); /测试语句写在最左边,以后没用可以删除或注掉 Client c = new Client(s); /每建立一个客户端,就new一个客户端对象,启动一个线程 new Thread(c).start(); clients.add(c); /勿忘写,将每个客户端加入到容器里 catch (IOException e) e.printStackTrace();
4、 finally try ss.close(); catch (IOException e) e.printStackTrace(); class Client implements Runnable private Socket s; private DataInputStream dis; private DataOutputStream dos; private boolean cont = false; public Client(Socket s) this.s = s; try dis = new DataInputStream(s.getInputStream();/初始化 do
5、s = new DataOutputStream(s.getOutputStream(); cont = true; catch (IOException e) e.printStackTrace(); public void send(String str) /用于发送给客户端 try dos.writeUTF(str); catch (IOException e) clients.remove(this); /移除那个退出的对象 System.out.println("一个客户退出了"); /e.printStackTrace(); public void run()
6、try while(cont) String str = dis.readUTF(); /阻塞式方法System.out.println(str); for(int i=0; i<clients.size(); i+) Client c = clients.get(i); /取客户端 c.send(str); /* 另外两种方法,但不适用,它会锁定服务端 for(Iterator<Client> it = clients.iterator(); it.hasNext();) Client c = it.next(); c.send(str); Iterator<Clie
7、nt> it = clients.iterator(); while(it.hasNext() Client c = it.next(); c.send(str); */ catch (EOFException e) /readUTF()阻塞式方法,所以关闭客户端会抛异常 System.out.println("Client closed!"); catch (IOException e) e.printStackTrace(); finally try if(dis != null) dis.close(); if(dos != null) dos.close();
8、 if(s != null) s.close(); s = null;/更严格的方法,等于空就没人去用了,垃圾收集器就回收走 catch (IOException e) e.printStackTrace(); 客户端:import java.awt.*;import java.awt.event.*;import java.io.*;import .*;public class ChatClient extends Frame Socket s = null; DataOutputStream dos = null; DataInputStream dis = null; private b
9、oolean cont = false; TextField tfTxt = new TextField(); TextArea taContent = new TextArea(); Thread tRecv = new Thread(new RecvThread(); public static void main(String args) new ChatClient().launchFrame(); public void launchFrame() setLocation(400, 300); this.setSize(300, 300); add(tfTxt,BorderLayou
10、t.SOUTH); add(taContent,BorderLayout.NORTH); pack(); /包在一起,去掉中间空着的 this.addWindowListener(new WindowAdapter() public void windowClosing(WindowEvent e) disconnect(); System.exit(0); ); tfTxt.addActionListener(new TfListent(); setVisible(true); connect(); tRecv.start(); /启动线程 public void connect() try
11、 s = new Socket("127.0.0.1",8888);/注意不要定义成Socket s,这就成了局部变量而不是成员变量了System.out.println("connected!"); dos = new DataOutputStream(s.getOutputStream(); dis = new DataInputStream(s.getInputStream(); cont = true; catch (UnknownHostException e) e.printStackTrace(); catch (IOException e
12、) e.printStackTrace(); public void disconnect() try dos.close(); dis.close(); s.close(); catch (IOException e) e.printStackTrace(); /*/无法解决readUTF阻塞式方法 try cont = false; /关闭线程 tRecv.join(); /合并线程,彻底让他停止 catch (InterruptedException e) e.printStackTrace(); finally try dos.close(); /线程停止之后才能关流,不然抛Socke
13、tException异常 dis.close(); s.close(); catch (IOException e) e.printStackTrace(); */ private class TfListent implements ActionListener public void actionPerformed(ActionEvent e) String str = tfTxt.getText().trim(); tfTxt.setText(""); try dos.writeUTF(str); dos.flush(); catch (IOException e1) e1.printStackTrace(); private class RecvThread implements Runnable public void run() try while(cont) String str = dis.readUTF(); taContent.setText(taContent.getText() + str + &
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 数据统计员考核制度
- 工程部月度考核制度
- 环保目标责任考核制度
- fdt 绩效考核制度
- 美容师绩效考核制度
- 机关双百分考核制度
- 景区票务中心考核制度
- 工程施工队考核制度
- 社团团支部考核制度
- 托管员工培训考核制度
- 2026及未来5年中国抽纱刺绣工艺品行业竞争现状及投资前景趋势报告
- 2025动物防疫专员试题及答案
- 单元复习:解码中国-我国区域差异的深度整合与素养提升
- 心肺复苏术护理配合要点
- 中医特色护理在精神科的应用
- 风力发电运输合同范本
- 重难点22 立体几何中的外接球、内切球问题(举一反三专项训练)(全国通.用)(原卷版)-2026年高考数学一轮复习举一反三系列
- 高二生物DNA的复制一节教案(2025-2026学年)
- 法律合规风险评估检查表
- 福建省莆田市擢英中学2026届九年级英语第一学期期末联考试题含解析
- 小儿急性呼吸衰竭课件
评论
0/150
提交评论