-
-
- package com.mulThread;
-
- import java.awt.*;
- import java.awt.event.*;
- import javax.swing.*;
- import java.net.*;
- import java.io.*;
- import java.util.*;
-
-
-
-
-
-
-
-
-
-
-
-
- public class Server extends JFrame {
- JPanel contentPane;
-
- JMenuBar jMenuBar1 = new JMenuBar();
-
- JMenu jMenuFile = new JMenu();
-
- JMenuItem jMenuFileExit = new JMenuItem();
-
- JMenu jMenuHelp = new JMenu();
-
- JMenuItem jMenuHelpAbout = new JMenuItem();
-
- JLabel statusBar = new JLabel();
-
- BorderLayout borderLayout1 = new BorderLayout();
-
- JPanel jPanel1 = new JPanel();
-
- BorderLayout borderLayout2 = new BorderLayout();
-
- JLabel jLabel1 = new JLabel();
-
- static java.awt.List jList1 = new java.awt.List(13);
-
- JScrollPane scrollpane = new JScrollPane(jList1);
-
-
- static Vector clients = new Vector(10);
-
- static ServerSocket server = null;
-
- static int active_connects = 0;
-
- static Socket socket = null;
-
-
- public static void main(String[] args) {
- try {
- UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
- } catch (Exception e) {
- e.printStackTrace();
- }
- Server chatServer1 = new Server();
- chatServer1.show();
- System.out.println("Server starting ...");
-
- try {
- server = new ServerSocket(2525);
- } catch (IOException e) {
- System.out.println("Error:" + e);
- }
- while (true) {
-
- if (clients.size() < 5)
- {
- try {
- socket = server.accept();
- if (socket != null) {
- System.out.println(socket + "连接");
-
- }
- } catch (IOException e) {
- System.out.println("Error:" + e);
- }
- int i = 0;
-
- do {
-
- Client c = new Client(socket);
- clients.addElement(c);
- if (checkName(c))
- {
- int connum = ++chatServer1.active_connects;
- String constr = "目前有" + connum + "客户相连";
- chatServer1.statusBar.setText(constr);
- Client listdata = (Client) clients.elementAt(i);
-
- chatServer1.jList1.addItem(listdata.ip + "连接", i);
- c.start();
- notifyRoom();
-
-
- } else {
-
- c.ps.println("TAKEN");
-
- disconnect(c);
-
- }
- i++;
- break;
-
- } while (i < clients.size());
-
- } else
- {
- try {
- Thread.sleep(200);
- } catch (InterruptedException e) {
- }
- }
- }
- }
-
-
- public Server()
- {
- enableEvents(AWTEvent.WINDOW_EVENT_MASK);
- try {
- jbInit();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
-
-
- private void jbInit() throws Exception {
-
-
- contentPane = (JPanel) this.getContentPane();
- contentPane.setLayout(borderLayout1);
- this.setSize(new Dimension(400, 300));
- this.setTitle("简易聊天服务器端");
- statusBar.setText("目前的连接数为:");
-
- jMenuFile.setText("File");
- jMenuFileExit.setText("Exit");
- jMenuFileExit.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- jMenuFileExit_actionPerformed(e);
- }
- });
- jMenuHelp.setText("Help");
- jMenuHelpAbout.setText("About");
- jMenuHelpAbout.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- jMenuHelpAbout_actionPerformed(e);
- }
- });
-
- jPanel1.setLayout(borderLayout2);
- jLabel1.setText("服务器端连接客户");
- jMenuFile.add(jMenuFileExit);
- jMenuHelp.add(jMenuHelpAbout);
- jMenuBar1.add(jMenuFile);
- jMenuBar1.add(jMenuHelp);
- this.setJMenuBar(jMenuBar1);
- contentPane.add(statusBar, BorderLayout.SOUTH);
- contentPane.add(jPanel1, BorderLayout.CENTER);
- jPanel1.add(jLabel1, BorderLayout.NORTH);
- jPanel1.add(scrollpane, BorderLayout.CENTER);
-
- }
-
-
- public void jMenuFileExit_actionPerformed(ActionEvent e)
- {
- sendClients(new StringBuffer("QUIT"));
- closeAll();
- System.exit(0);
- }
-
-
- public void jMenuHelpAbout_actionPerformed(ActionEvent e)
- {
- AboutChat dlg = new AboutChat(this);
- Dimension dlgSize = dlg.getPreferredSize();
- Dimension frmSize = getSize();
- Point loc = getLocation();
- dlg.setLocation((frmSize.width - dlgSize.width) / 2 + loc.x,
- (frmSize.height - dlgSize.height) / 2 + loc.y);
- dlg.setModal(true);
- dlg.show();
- }
-
-
- protected void processWindowEvent(WindowEvent e)
- {
- super.processWindowEvent(e);
- if (e.getID() == WindowEvent.WINDOW_CLOSING) {
- jMenuFileExit_actionPerformed(null);
- }
- }
-
-
- public static void notifyRoom()
- {
- StringBuffer people = new StringBuffer("PEOPLE");
- for (int i = 0; i < clients.size(); i++) {
- Client c = (Client) clients.elementAt(i);
- people.append(":" + c.name);
-
- }
- sendClients(people);
- }
-
- public static synchronized void sendClients(StringBuffer msg)
- {
- for (int i = 0; i < clients.size(); i++) {
- Client c = (Client) clients.elementAt(i);
- c.send(msg);
- }
- }
-
- public static void closeAll()
- {
- while (clients.size() > 0)
- {
- Client c = (Client) clients.firstElement();
- try {
- c.socket.close();
- } catch (IOException e) {
- System.out.println("Error:" + e);
- } finally {
- clients.removeElement(c);
- }
- }
- }
-
- public static boolean checkName(Client newclient)
- {
- for (int i = 0; i < clients.size(); i++) {
- Client c = (Client) clients.elementAt(i);
- if ((c != newclient) && c.equals(newclient.name))
- return false;
- }
- return (true);
- }
-
- public static synchronized void disconnect(Client c)
- {
- try {
- jList1.addItem(c.ip + "断开连接");
-
- Server.active_connects--;
- c.send(new StringBuffer("QUIT"));
- c.socket.close();
-
- } catch (IOException e) {
- System.out.println("Error:" + e);
- } finally {
- clients.removeElement(c);
- }
- }
-
- }
-
- class Client extends Thread
- {
- Socket socket;
-
- String name;
-
- String ip;
-
- DataInputStream dis;
-
- PrintStream ps;
-
- public void send(StringBuffer msg)
- {
- ps.println(msg);
- ps.flush();
- }
-
- public Client(Socket s)
- {
- socket = s;
- try {
- dis = new DataInputStream(s.getInputStream());
- ps = new PrintStream(s.getOutputStream());
- String info = dis.readLine();
-
- StringTokenizer stinfo = new StringTokenizer(info, ":");
- String head = stinfo.nextToken();
- if (stinfo.hasMoreTokens())
- name = stinfo.nextToken();
- if (stinfo.hasMoreTokens())
- ip = stinfo.nextToken();
- System.out.println(head);
- } catch (IOException e) {
- System.out.println("Error:" + e);
- }
- }
-
- public void run()
- {
- while (true) {
- String line = null;
- try {
- line = dis.readLine();
-
- } catch (IOException e) {
- System.out.println("Error" + e);
- Server.disconnect(this);
- Server.notifyRoom();
- return;
- }
-
- if (line == null)
- {
- Server.disconnect(this);
- Server.notifyRoom();
- return;
- }
-
- StringTokenizer st = new StringTokenizer(line, ":");
- String keyword = st.nextToken();
-
- if (keyword.equals("MSG"))
- {
- StringBuffer msg = new StringBuffer("MSG:");
- msg.append(name);
- msg.append(st.nextToken("\0"));
- Server.sendClients(msg);
- } else if (keyword.equals("QUIT"))
- {
-
- Server.disconnect(this);
- Server.notifyRoom();
- this.stop();
- }
- }
- }
- }
-
-
-
- package com.mulThread;
-
- import java.awt.*;
- import java.awt.event.*;
- import java.applet.*;
- import java.net.*;
- import java.io.*;
- import java.util.*;
-
-
-
-
-
-
-
-
-
-
-
-
- public class ClientChat extends Applet {
-
- Panel panel1 = new Panel();
-
- BorderLayout borderLayout1 = new BorderLayout();
-
- Panel panel2 = new Panel();
-
- Panel panel3 = new Panel();
-
- FlowLayout flowLayout1 = new FlowLayout();
-
- FlowLayout flowLayout2 = new FlowLayout();
-
- Label label1 = new Label();
-
- TextField name_txt = new TextField(15);
-
- Button button1 = new Button();
-
- Button button2 = new Button();
-
- TextArea chat_txt = new TextArea(15, 30);
-
- Label label2 = new Label();
-
- Button button3 = new Button();
-
- TextField msg_txt = new TextField(20);
-
- java.awt.List list1 = new java.awt.List(13);
-
-
- Socket soc = null;
-
- PrintStream ps = null;
-
- Listen listen = null;
-
- public void init()
- {
- resize(475, 350);
- this.setLayout(borderLayout1);
- panel2.setLayout(flowLayout1);
- panel3.setLayout(flowLayout2);
- label1.setText("用户名:");
-
- button1.setLabel("连接");
- button2.setLabel("断开连接");
-
- chat_txt.setEditable(false);
- panel2.setBackground(Color.cyan);
- panel1.setBackground(Color.cyan);
- label2.setText("聊天信息:");
- button3.setLabel("发送");
- msg_txt.setText("请输入聊天信息");
- panel3.setBackground(Color.cyan);
- this.add(panel1, BorderLayout.NORTH);
- panel1.add(label1, null);
- panel1.add(name_txt, null);
- panel1.add(button1, null);
- panel1.add(button2, null);
- this.add(panel2, BorderLayout.CENTER);
- panel2.add(chat_txt, null);
- panel2.add(list1, null);
- this.add(panel3, BorderLayout.SOUTH);
- panel3.add(label2, null);
- panel3.add(msg_txt, null);
- panel3.add(button3, null);
- }
-
- public boolean action(Event evt, Object obj)
- {
- if (evt.target instanceof Button) {
- String label = (String) obj;
- if (label.equals("连接"))
- {
- if (soc == null) {
- try {
- soc = new Socket(InetAddress.getLocalHost(), 2525);
- System.out.println(soc);
- ps = new PrintStream(soc.getOutputStream());
- StringBuffer info = new StringBuffer("INFO: ");
-
-
-
- String userinfo = name_txt.getText() + ":"
- + InetAddress.getLocalHost().toString();
- ps.println(info.append(userinfo));
-
- ps.flush();
- listen = new Listen(this, name_txt.getText(), soc);
- listen.start();
- } catch (IOException e) {
- System.out.println("Error:" + e);
- disconnect();
- }
- }
- }
- else if (label.equals("断开连接"))
- {
- disconnect();
- } else if (label.equals("发送"))
- {
- if (soc != null) {
- StringBuffer msg = new StringBuffer("MSG: ");
-
- try {
- String msgtxt = new String(msg_txt.getText());
- } catch (Exception e) {
- }
-
- ps.println(msg.append(msg_txt.getText()));
- ps.flush();
- }
- }
- }
- return true;
- }
-
- public void disconnect()
- {
- if (soc != null) {
- try {
- listen.suspend();
- ps.println("QUIT");
- ps.flush();
- soc.close();
- } catch (IOException e) {
- System.out.println("Error:" + e);
- } finally {
-
- }
- }
- }
-
- class Listen extends Thread
- {
- String name = null;
-
- DataInputStream dis = null;
-
- PrintStream ps = null;
-
- Socket socket = null;
-
- ClientChat parent = null;
-
- public Listen(ClientChat p, String n, Socket s)
- {
-
- parent = p;
- name = n;
- socket = s;
-
- try {
-
- dis = new DataInputStream(s.getInputStream());
- ps = new PrintStream(s.getOutputStream());
-
- } catch (IOException e) {
- System.out.println("Error:" + e);
- parent.disconnect();
- }
- }
-
- public void run()
- {
- String msg = null;
- while (true) {
- try {
- msg = dis.readLine();
- }
- catch (IOException e) {
- System.out.println("Error:" + e);
- parent.disconnect();
- }
- if (msg == null)
- {
- parent.listen = null;
- parent.soc = null;
- parent.list1.clear();
- return;
- }
- StringTokenizer st = new StringTokenizer(msg, ":");
- String keyword = st.nextToken();
-
- if (keyword.equals("PEOPLE"))
-
- {
- parent.list1.clear();
- while (st.hasMoreTokens())
- {
- String str = st.nextToken();
- parent.list1.addItem(str);
- }
- } else if (keyword.equals("MSG"))
-
- {
- String usr = st.nextToken();
- parent.chat_txt.appendText(usr);
- parent.chat_txt.appendText(st.nextToken("\0"));
- parent.chat_txt.appendText("\n\n");
- } else if (keyword.equals("QUIT"))
-
- {
- System.out.println("Quit");
- try {
- parent.listen.stop();
- parent.listen = null;
- parent.soc.close();
- parent.soc = null;
- } catch (IOException e) {
- System.out.println("Error:" + e);
- }
- parent.list1.clear();
-
- return;
- }
- }
-
- }
- }
-
- }
-
-
-
- package com.mulThread;
-
- import java.awt.*;
- import java.awt.event.*;
- import javax.swing.*;
- import javax.swing.border.*;
-
-
-
-
-
-
-
-
-
-
-
-
- public class AboutChat extends JDialog implements ActionListener {
-
- JPanel panel1 = new JPanel();
-
- JPanel panel2 = new JPanel();
-
- JPanel insetsPanel1 = new JPanel();
-
- JPanel insetsPanel2 = new JPanel();
-
- JPanel insetsPanel3 = new JPanel();
-
- JButton button1 = new JButton();
-
- JLabel imageLabel = new JLabel();
-
- JLabel label1 = new JLabel();
-
- JLabel label2 = new JLabel();
-
- JLabel label3 = new JLabel();
-
- JLabel label4 = new JLabel();
-
- BorderLayout borderLayout1 = new BorderLayout();
-
- BorderLayout borderLayout2 = new BorderLayout();
-
- FlowLayout flowLayout1 = new FlowLayout();
-
- GridLayout gridLayout1 = new GridLayout();
-
- String product = "product:少爷精简版--聊天服务器端";
-
- String author = "author:young master " +
- "any questions,please reply to me for deeping improvements";
-
- String email = "@E-mail:youngmaster.fly@gmail.com";
-
- String comments = "本聊天室服务器端实现了多线程客户连接和显示连接信息";
-
- public AboutChat(Frame parent) {
- super(parent);
- enableEvents(AWTEvent.WINDOW_EVENT_MASK);
- try {
- jbInit();
- } catch (Exception e) {
- e.printStackTrace();
- }
- pack();
- }
-
- private void jbInit() throws Exception {
-
- this.setTitle("少爷精简版--聊天服务器端");
- setResizable(false);
- panel1.setLayout(borderLayout1);
- panel2.setLayout(borderLayout2);
- insetsPanel1.setLayout(flowLayout1);
- insetsPanel2.setLayout(flowLayout1);
- insetsPanel2.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
- gridLayout1.setRows(4);
- gridLayout1.setColumns(1);
- label1.setText(product);
- label2.setText(author);
- label3.setText(email);
- label4.setText(comments);
- insetsPanel3.setLayout(gridLayout1);
- insetsPanel3.setBorder(BorderFactory.createEmptyBorder(10, 60, 10, 10));
- button1.setText("Ok");
- button1.addActionListener(this);
- insetsPanel2.add(imageLabel, null);
- panel2.add(insetsPanel2, BorderLayout.WEST);
- this.getContentPane().add(panel1, null);
- insetsPanel3.add(label1, null);
- insetsPanel3.add(label2, null);
- insetsPanel3.add(label3, null);
- insetsPanel3.add(label4, null);
- panel2.add(insetsPanel3, BorderLayout.CENTER);
- insetsPanel1.add(button1, null);
- panel1.add(insetsPanel1, BorderLayout.SOUTH);
- panel1.add(panel2, BorderLayout.NORTH);
- }
-
-
- protected void processWindowEvent(WindowEvent e) {
- if (e.getID() == WindowEvent.WINDOW_CLOSING) {
- cancel();
- }
- super.processWindowEvent(e);
- }
-
-
- void cancel() {
- dispose();
- }
-
-
- public void actionPerformed(ActionEvent e) {
- if (e.getSource() == button1) {
- cancel();
- }
- }
- }
|