收藏 分销(赏)

简易聊天室JAVA代码.doc

上传人:丰**** 文档编号:4770506 上传时间:2024-10-12 格式:DOC 页数:9 大小:13.78KB
下载 相关 举报
简易聊天室JAVA代码.doc_第1页
第1页 / 共9页
简易聊天室JAVA代码.doc_第2页
第2页 / 共9页
点击查看更多>>
资源描述
import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; import .*; public class ChatClient extends Frame { private boolean connected = false; private DataOutputStream dos = null; private DataInputStream dis = null; private static final long serialVersionUID = 1L; private Socket s = null; private TextField tfWords = null; private TextArea taShow = null; public static void main(String args[]) { ChatClient cc = new ChatClient(); cc.launchFrame(); cc.Listen(); } public void launchFrame() { try { s = new Socket(InetAddress.getByName("localhost"), 8888); connected = true; dos = new DataOutputStream(s.getOutputStream()); dis = new DataInputStream(s.getInputStream()); } catch (IOException ioe) { System.out.println("服务器连接失败!"); ioe.printStackTrace(); } this.tfWords = new TextField(); this.taShow = new TextArea(); this.setTitle("Chatroom Client"); this.setLocation(700, 50); this.add(tfWords, BorderLayout.SOUTH); this.add(taShow, BorderLayout.NORTH); this.setSize(200, 250); this.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { try { connected = false; dos.writeUTF("###Exit###"); dos.flush(); close(); System.exit(0); } catch (IOException ioe) { System.out.println("消息发送出错!"); ioe.printStackTrace(); } } }); this.setVisible(true); this.tfWords.addActionListener(new TFWordsListener()); } public void Listen() { new Thread(new ListenThread()).start(); } public void close() { try { if (dos != null) dos.close(); if (s != null) s.close(); } catch (IOException ioe) { ioe.printStackTrace(); System.out.println("客户端断开时出错!"); System.exit(-1); } } public class ListenThread implements Runnable{ public void run(){ while (connected) { try { String back = dis.readUTF(); //阻塞IO taShow.append(back); }catch(SocketException se){ System.out.println("谢谢使用!"); }catch (IOException e) { System.out.println("数据丢失!"); e.printStackTrace(); } } } } private class TFWordsListener implements ActionListener { public void actionPerformed(ActionEvent e) { String words = tfWords.getText(); tfWords.setText(""); try { dos.writeUTF(words+ "\n"); } catch (IOException ioe) { System.out.println("发送出错!"); ioe.printStackTrace(); } } } } import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; import .*; import java.util.List; import java.util.ArrayList; public class ChatServer { private List<Client> clients = new ArrayList<Client>(); private boolean started = false; private ServerSocket ss = null; private Client c = null; public ChatServer() { try { ss = new ServerSocket(8888); started = true; } catch (IOException ioe) { System.out.println("对不起,服务器不能启动!"); ioe.printStackTrace(); System.exit(-1); } } public void start() { try { Socket s = this.getSs().accept(); System.out.println("Here comes a client! "); if (s != null) { c = this.new Client(s, true); this.clients.add(c); new Thread(c).start(); // 线程始终没能停止!!!? c = null; } } catch (IOException e) { System.out.println("连接错误!"); e.printStackTrace(); } } public boolean isStarted() { return started; } public void setStarted(boolean started) { this.started = started; } public ServerSocket getSs() { return ss; } public void setSs(ServerSocket ss) { this.ss = ss; } public List<Client> getClients() { return clients; } public void setClients(List<Client> clients) { this.clients = clients; } public static void main(String args[]) { ChatServer cs = new ChatServer(); while (cs.isStarted()) { cs.start(); } } private class Client implements Runnable { private boolean connected = false; private Socket s = null; private DataInputStream dis = null; private DataOutputStream dos = null; public Client(Socket s, boolean connected) { this.s = s; this.connected = connected; } public void run() { InetAddress ip = null; int port = 9999; try { if (connected) { dis = new DataInputStream(s.getInputStream()); dos = new DataOutputStream(s.getOutputStream()); ip = s.getInetAddress(); port = s.getPort(); } while (connected) { String line = dis.readUTF(); if (line.equals("###Exit###")) { getClients().remove(this); // 当接收到推出的消息,移除客户端记录 line = "Bye-Bye!"; connected = false; } System.out.println("From: (" + ip.toString() + ":" + port + "): " + line); for (int i = 0; i < getClients().size(); i++) { if (this.equals(getClients().get(i))) { continue; } else { getClients().get(i).dos.writeUTF("From: (" + ip.toString() + ":" + port + "): " + line + "/n"); } } } } catch (IOException ioe) { System.out.println("客户端连接出错!"); ioe.printStackTrace(); } } } }
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传
相似文档                                   自信AI助手自信AI助手

当前位置:首页 > 包罗万象 > 大杂烩

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2025 宁波自信网络信息技术有限公司  版权所有

客服电话:4009-655-100  投诉/维权电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服