基于C/S模式的简单聊天程序(服务器篇)

package server;

import java.awt.*;
import java.io.*;
import java.net.*;
import java.util.*;
import java.awt.event.*;

import javax.swing.*;

public class Server extends JFrame {

	JTextArea textShow;
	JButton start;
	Vector socketsss = new Vector();//这里用到了变长对象数组,用来存储来自客户端的socket对象
	ServerSocket server = null;
	Socket clients;

	Server() { // 服务器的构造函数,并且初始化
		init();
		setVisible(true);
		setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		setBounds(450, 150, 340, 455);
		setTitle("好好学习天天向上聊天室服务器");
		setResizable(false);
	}

	void init() { // 设置布局和事件监视器
		setLayout(new FlowLayout());
		getContentPane().setBackground(new Color(20, 85, 237));
		textShow = new JTextArea(21, 29);
		textShow.setBackground(new Color(45, 210, 209));
		start = new JButton("              启动服务器              ");
		start.setBackground(new Color(236, 134, 21));
		add(start);
		add(new JScrollPane(textShow));
		textShow.setEditable(false);
		start.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {   //在这里启动监听的线程
				Listen listen = new Listen();
				Thread go = new Thread(listen);
				go.start();
			}
		});

		addWindowListener(new WindowAdapter() { // 响应关闭按钮功能
			public void windowClosing(WindowEvent e) {
				int option = JOptionPane
						.showConfirmDialog(null, "亲爱的你真的要离开聊天室么?",
								" 好好学习天天向上聊天室", JOptionPane.YES_NO_OPTION,
								JOptionPane.QUESTION_MESSAGE);
				if (option == JOptionPane.YES_OPTION)
					System.exit(0);

			}
		});

	} // init()结束

	class ServerThread extends Thread { // 服务器消息处理的线程
		Socket socket;
		DataOutputStream out = null;
		DataInputStream in = null;
		String s = null;
		Vector sockets = new Vector();
		int j = 0;

		ServerThread(Socket t, Vector socketss) {
			socket = t;
			sockets = socketss;
			try { 
				in = new DataInputStream(socket.getInputStream());
			} catch (IOException e) {
			}
		}

		public void run() {
			while (true) {
				try {
					String r = in.readUTF();// 堵塞状态,除非读取到信息
					for (int j = 0; j < sockets.size(); j++) {
						out = new DataOutputStream(
								((Socket) sockets.get(j)).getOutputStream()); // 对于每个数组内的socket对象都建立输出流
						out.writeUTF(r);
					}
				} catch (IOException e) {
					textShow.append("有一个逗比离开了
");
					return;
				}
			}
		}
	}

	class Listen implements Runnable { // 服务器监听线程
		ServerSocket server;
		Socket clients;

		public void run() {
			while (true) {
				try {
					server = new ServerSocket(8888);
					textShow.append(new java.text.SimpleDateFormat(
							"yy-MM-dd HH:mm:ss").format(new Date())
							+ "服务器已开启
");
				} catch (IOException e1) {
					textShow.append("正在监听
"); // ServerSocket对象不能重复创建
				}
				try {
					textShow.append(new java.text.SimpleDateFormat(
							"yy-MM-dd HH:mm:ss").format(new Date())
							+ " 等待用户连接......
");

					clients = server.accept();
					socketsss.add(clients);
					ServerThread handlers = new ServerThread(clients, socketsss);
					handlers.start(); // 为每个用户创建单独的消息处理线程
					textShow.append(new java.text.SimpleDateFormat(
							"yy-MM-dd HH:mm:ss").format(new Date())
							+ "有用户连接,用户的地址:" + clients.getInetAddress() + "
");
				} catch (IOException e1) {
					textShow.append(new java.text.SimpleDateFormat(
							"yy-MM-dd HH:mm:ss").format(new Date())
							+ "正在等待逗比来临......
");
				}
			}
		}
	}

	public static void main(String args[]) {
		Server server = new Server();
	}
}
经验分享 程序员 微信小程序 职场和发展