JavaSE使用socket与线程实现控制台版的聊天室功能

服务器server端

	private ServerSocket server;// 创建了ServerSocket对象
	// 存放所有客户端输出流的集合,并初始化
	private List<PrintWriter> allOut = new ArrayList<PrintWriter>();
 
	// 向集合中添加元素
	private synchronized void addOut(PrintWriter out) {
		allOut.add(out);
	}
 
	// 从集合中删除元素
	private synchronized void removeOut(PrintWriter out) {
		allOut.remove(out);
	}
 
	// 遍历集合向所有的客户端发送信息
	private synchronized void sendMessage(String msg) {
		for (PrintWriter out : allOut) {
			out.println(msg);
		}
 
	}
 
	/**
	 * 构造方法,用来初始化ServerSocket
	 * 
	 * @throws IOException
	 */
	public Server() throws IOException {
		try {
			server = new ServerSocket(8088);
		} catch (IOException e) {
			e.printStackTrace();
			throw e;
		}
	}
 
	public void start() throws IOException {
		while (true) {
			try {
				Socket socket = server.accept();
				/**
				 * 当一个客户端连接后,应该启动一个线程,来负责与客户端的交互 这样才能循环接待不同的客户端
				 */
				ClientHandler hander = new ClientHandler(socket);
				Thread thread = new Thread(hander);
				// 启动线程
				thread.start();
			} catch (IOException e) {
				e.printStackTrace();
				throw e;
			}
		}
	}
 
	public static void main(String[] args) {
		Server server = null;
		try {
			server = new Server();
			server.start();
		} catch (IOException e) {
			System.out.println("服务器启动失败");
		}
	}
 
	/**
	 * 启动线程的成员内部类
	 * 
	 * @author Admin
	 * 
	 */
 
	class ClientHandler implements Runnable {
		private Socket socket;
 
		public ClientHandler(Socket socket) {
			this.socket = socket;
		}
 
		@Override
		public void run() {
			PrintWriter writer = null;
			try {
				// 向客户端发送信息
				writer = new PrintWriter(new OutputStreamWriter(
						socket.getOutputStream(), "utf-8"), true);
				addOut(writer);
				// 读取客户端的信息
				BufferedReader reader = new BufferedReader(
						new InputStreamReader(socket.getInputStream(), "utf-8"));
				String msg = null;
 
				while ((msg = reader.readLine()) != null) {
					// 将客户端发送过来的信息转发给客户端
					sendMessage(msg);
				}
			} catch (IOException e) {
				e.printStackTrace();
			} finally {
				// 处理客户端断开连接后的工作
				// 将客户端的输出流从集合中移除
				removeOut(writer);
			}
		}
	}

客户端client端

	// 创建与服务端通讯的Socket
	private Socket socket;
 
	public Client() throws IOException {
		try {
			socket = new Socket("localhost", 8088);
		} catch (IOException e) {
			e.printStackTrace();
			throw e;
		}
	}
 
	// 客户端开始工作的方法
	public void start() {
		// 启动线程,用于读取服务端发送过来的信息
		ServerHandler handler = new ServerHandler();
		Thread thread = new Thread(handler);
		// 启动线程
		thread.start();
		/**
		 * 循环从控制台输入信息发送至服务端
		 */
		Scanner sca = new Scanner(System.in);
		try {
			PrintWriter writer = new PrintWriter(new OutputStreamWriter(
					socket.getOutputStream(), "utf-8"), true);
			String msg = null;
			while (true) {
				msg = sca.nextLine();
				writer.println(msg);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
 
	public static void main(String[] args) {
		Client c = null;
		try {
			c = new Client();
			c.start();
		} catch (IOException e) {
			System.out.println("连接失败");
		}
	}
 
	// 读取服务端发送过来的信息的线程
	class ServerHandler implements Runnable {
 
		@Override
		public void run() {
			try {
				BufferedReader reader = new BufferedReader(
						new InputStreamReader(socket.getInputStream(), "utf-8"));
				String msg = null;
				while ((msg = reader.readLine()) != null) {
					System.out.println(msg);
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

评论

  1. 2月前
    2022-7-26 10:36:41

    感谢分享,赞一个

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇