在线聊天系统雏形!!!
2010-12-06 20:32
295 查看
ChatServer
ChatClient
import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.EOFException; import java.io.IOException; import java.net.ServerSocket; import java.net.Socket; import java.net.SocketException; import java.util.ArrayList; import java.util.List; /* * 聊天雏形服务端 */ public class ChatServer { private boolean started = false; private ServerSocket ss = null; private List<Client> list = new ArrayList<Client>(); public static void main(String[] args) { new ChatServer().start(); } /* * 接受连接 */ public void start() { try { ss = new ServerSocket(8888); started = true; } catch (IOException e1) { e1.printStackTrace(); } try { while (started) { Socket s = ss.accept(); Client c = new Client(s); list.add(c); new Thread(c).start(); } } catch (IOException e) { e.printStackTrace(); } finally { try { if (ss != null) { ss.close(); } } catch (IOException e) { e.printStackTrace(); } } } /* * 处理信息 */ class Client implements Runnable { private Socket s = null; private DataInputStream dis = null; private DataOutputStream dos = null; private boolean connected = false; public Client(Socket s) { this.s = s; try { dis = new DataInputStream(s.getInputStream()); dos = new DataOutputStream(s.getOutputStream()); connected = true; } catch (IOException e) { e.printStackTrace(); } } public void send(String t) { try { dos.writeUTF(t); } catch (IOException e) { System.out.println("Client is closed!"); } } public void run() { try { while (connected) { String t = dis.readUTF(); for (Client c : list) { c.send(t); } } } catch (SocketException e) { list.remove(this); } catch (EOFException e) { System.out.println("Client is closed!"); } catch (IOException e) { e.printStackTrace(); } finally { try { if (dis != null) { dis.close(); } if (dos != null) { dos.close(); } if (s != null) { s.close(); } } catch (IOException e) { e.printStackTrace(); } } } } }
ChatClient
import java.awt.BorderLayout; import java.awt.Frame; import java.awt.TextArea; import java.awt.TextField; 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.EOFException; import java.io.IOException; import java.net.Socket; import java.net.SocketException; import java.net.UnknownHostException; /* * 聊天雏形客户端 */ public class ChatClient extends Frame { private TextField tf = new TextField(); private TextArea ta = new TextArea(); private Socket s = null; private DataOutputStream dos = null; private DataInputStream dis = null; private boolean connected = false; private Thread t = new Thread(new Recv()); public static void main(String[] args) { new ChatClient().launchFrame(); } /* * 初始化窗口 */ public void launchFrame() { this.setLocation(400, 300); this.setSize(300, 300); this.add(ta, BorderLayout.NORTH); this.add(tf, BorderLayout.SOUTH); this.pack(); this.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { unconnect(); System.exit(0); } }); tf.addActionListener(new TFListener()); this.setVisible(true); this.connect(); t.start(); } /* * 连接 */ public void connect() { try { s = new Socket("127.0.0.1", 8888); dos = new DataOutputStream(s.getOutputStream()); dis = new DataInputStream(s.getInputStream()); connected = true; } catch (UnknownHostException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } /* * 关闭连接 */ public void unconnect() { try { if (dis != null) { dis.close(); } if (dos != null) { dos.close(); } if (s != null) { s.close(); } } catch (IOException e) { e.printStackTrace(); } } /* * 发送信息 */ private class TFListener implements ActionListener { public void actionPerformed(ActionEvent e) { String t = tf.getText().trim(); tf.setText(""); try { dos.writeUTF(t); dos.flush(); } catch (IOException e1) { e1.printStackTrace(); } } } /* * 接受信息 */ private class Recv implements Runnable { public void run() { try { while (connected) { String t = dis.readUTF(); ta.setText(ta.getText() + t + "/n"); } } catch (SocketException e) { System.out.println("退出了,bye!"); } catch (EOFException e) { System.out.println("Client is closed!"); } catch (IOException e) { e.printStackTrace(); } } } }
相关文章推荐
- 在线聊天系统雏形总结(续)
- 在线雏形聊天系统总结
- 基于Socekt的Unity多人在线聊天系统
- Chat聊天系统雏形
- WoLive-在线客服系统源码,网页聊天源码开源版
- 详解如何实现在线聊天系统中的实时消息获取
- 在线聊天、会议、远程教育系统开源技术方案
- 昨天做了个jsp版网页在线聊天系统
- Ext 在线实时聊天系统的开发,有源码包,实现了实时更新在线用户列表和群聊
- GatewayWorker整合web系统开发多人分组在线聊天的步骤
- Socket 通信(基础原理、实时聊天系统雏形)
- Linux网络编程(多人在线聊天系统)
- php在线非注册聊天系统的开发-初
- php在线非注册聊天系统的开发-中
- web在线聊天系统。非ajax轮询
- Socket 通信(基础原理、实时聊天系统雏形)
- 前端通信:SSE设计方案(二)--- 服务器推送技术的实践以及一些应用场景的demo(包括在线及时聊天系统以及线上缓存更新,代码热修复案例)
- 在线聊天小系统
- 简单在线聊天系统的实现(初步)