Welcome 微信登录

首页 / 软件开发 / JAVA / Java实现的c/s的聊天室

Java实现的c/s的聊天室2011-01-22 iteye zhaohong根据马士兵老师聊天室程序进行优化,同时增加聊天者之间的交互。

同时增加服务端会为每一个客户端增加一个交互窗口,让服务器可以和每一个客户端交互!

服务端代码

1.import java.net.*;2.import java.util.*;3.import java.io.*;4.import java.awt.*;5.import java.awt.event.*;6.import javax.swing.*;7.8.import javax.swing.JFrame;9.10.public class ChatServer extends JFrame {11.JTextArea ta = new JTextArea();12.ServerSocket server = null;13.Collection cClient = new ArrayList();14.15.public ChatServer(int port) throws Exception {16.server = new ServerSocket(port);17.add(ta, BorderLayout.CENTER);18.setBounds(200, 200, 300, 450);19.this.addWindowListener(new WindowAdapter() {20.public void windowClosing(WindowEvent e) {21.System.exit(0);22.}23.});24.setVisible(true);25.}26.27.public void startServer() throws Exception {28.while (true) {29.Socket s = server.accept();30.cClient.add(new ClientConn(s));31.ta.append(s.getInetAddress().getHostName() + "进入" + "" + "端口号"32.+ s.getPort());33.ta.append("
" + "当前在前总人数: " + cClient.size() + "

");34.}35.}36.37.class ClientConn extends Frame implements Runnable, ActionListener {38.TextArea ta1 = null;39.TextArea ta2 = null;40.Button btn = null;41.Socket s = null;42.43.public ClientConn(Socket s) {44.ta1 = new TextArea(3, 30);45.ta2 = new TextArea(2, 15);46.btn = new Button("发送");47.this.setLayout(new BorderLayout());48.this.add(ta1, BorderLayout.CENTER);49.this.add(ta2, BorderLayout.SOUTH);50.this.add(btn, BorderLayout.EAST);51.this.setSize(300, 200);52.this.setVisible(true);53.this.setTitle("" + s.getInetAddress().getHostName() + "端口"54.+ s.getPort());55.this.s = s;56.(new Thread(this)).start();57.btn.addActionListener(this);58.}59.60.public void actionPerformed(ActionEvent e) {61.try {62.DataOutputStream dos = new DataOutputStream(s.getOutputStream());63.dos.writeUTF("服务器:
" + ta2.getText() + "
");64.ta1.append("服务器:
" + ta2.getText() + "
");65.ta2.setText("");66.} catch (IOException E) {67.68.}69.}70.71.public void send(String str, String st) throws IOException {72.DataOutputStream dos = new DataOutputStream(s.getOutputStream());73.dos.writeUTF(st + "说:
" + str);74.}75.76.public void dispose() {77.try {78.// this.setVisible(false);79.super.dispose();80.ta.append(s.getInetAddress().getHostName() + "退出" + "
");81.if (s != null)82.s.close();83.cClient.remove(this);84.ta.append("当前在线人数: " + cClient.size() + "

");85.} catch (Exception e) {86.e.printStackTrace();87.}88.}89.90.public void run() {91.try {92.93.DataInputStream dis = new DataInputStream(s.getInputStream());94.String str = dis.readUTF();95.String st = s.getInetAddress().getHostName();96.while (str != null && str.length() != 0) {97.for (Iterator it = cClient.iterator(); it.hasNext();) {98.ClientConn cc = (ClientConn) it.next();99.if (this != cc) {100.cc.send(str, st);101.}102.}103.ta1.append(st + "说:
" + str + "
");104.str = dis.readUTF();105.}106.this.dispose();107.} catch (Exception e) {108.this.dispose();109.}110.111.}112.}113.114.public static void main(String[] args) throws Exception {115.JFrame.setDefaultLookAndFeelDecorated(true);116.ChatServer cs = new ChatServer(8888);117.cs.startServer();118.}119.}