多人聊天UDP

服务端

java 复制代码
package 多人聊天;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
public class Gserver implements Runnable {
	private int Port = 9999;
	private ServerSocket SS;
	private Socket socket;
	private ArrayList clients = new ArrayList();  //保存客户端线程
	public Gserver(){
		try{			
			SS = new ServerSocket(Port);
			new Thread(this).start();
		}catch(Exception ex){		
		}		
	}
	public void run(){
		try{
			while(true){
				socket = SS.accept();
				ChatThread ct = new ChatThread(socket);
				clients.add(ct);
				ct.start();
			}			
		}catch(Exception ex){	
		}
	}
	class ChatThread extends Thread{
		private Socket s;
		private InputStream is;
		private OutputStream os;
		private BufferedReader br;
		private PrintStream ps;
		public ChatThread(Socket socket) throws Exception{
			this.s = socket;
			is = this.s.getInputStream();
			os = this.s.getOutputStream(); 
			br = new BufferedReader(new InputStreamReader(is));
			ps = new PrintStream(os);
		}
		public void run(){
			try{
				while(true){
					String str = br.readLine();	
					sendMessage(str);
				}
			}catch (Exception ex){
			}
		}
		public void sendMessage(String str){
			for(int i=0; i<clients.size(); i++){
				ChatThread ct = (ChatThread)clients.get(i);
				ct.ps.println(str);
			}
		}
	}
	public static void main(String[] args){
		new Gserver();
	}
}

客户端

java 复制代码
package 多人聊天;


import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.InetAddress;
import java.net.Socket;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class Gclient extends JFrame implements ActionListener, Runnable {
	private Socket socket;
	private int Port = 9999;
	private InetAddress ip;
	private String name;
	
	private JTextArea area = new JTextArea("以下是聊天内容:\n");
	private JTextField field = new JTextField("");
	
	public Gclient(){
		this.setTitle("客户端");
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.add(field,BorderLayout.NORTH);
		field.addActionListener(this);
		this.add(area, BorderLayout.CENTER);		
		this.setSize(240, 260);
		this.setVisible(true);
		name = JOptionPane.showInputDialog("输入昵称:");
		try{
			ip = InetAddress.getByName("Localhost");
			socket = new Socket(ip,Port);
			JOptionPane.showMessageDialog(this, "连接成功");
			this.setTitle("客户端:" + name);
			new Thread(this).start();
		}catch (Exception ex){			
		}
	}
	public void run(){
		try{
			while(true){
				InputStream is = socket.getInputStream();
				BufferedReader bf = new BufferedReader(new InputStreamReader(is));
				String str = bf.readLine();
				area.append(str + '\n');
			}
		}catch (Exception ex){	
		}
	}
	public void actionPerformed(ActionEvent e){
		try{
			OutputStream os = socket.getOutputStream();
			PrintStream ps = new PrintStream(os);
			ps.println(name + "说:" + field.getText());
			field.setText("");
		}catch (Exception ex){			
		}
	}
	public static void main(String[] args) {
		new Gclient();
	}
}

结果

相关推荐
workflower5 分钟前
基本作业-管理⾃⼰的源代码
开发语言·单元测试·软件工程·需求分析·个人开发
froginwe1114 分钟前
Pandas DataFrame:深入理解数据分析的利器
开发语言
Miraitowa_cheems15 分钟前
LeetCode算法日记 - Day 81: 最大子数组和
java·数据结构·算法·leetcode·决策树·职场和发展·深度优先
Jm_洋洋17 分钟前
【Linux系统编程】程序替换:execve(execl、execlp、execle、execv、execvp、execvpe)
linux·运维·c语言·开发语言·程序人生
CodeCraft Studio21 分钟前
国产化Word处理控件Spire.Doc教程:用Java实现TXT文本与Word互转的完整教程
java·c#·word·spire.doc·word文档转换·txt转word·word转txt
徐子童27 分钟前
数据结构---优先级队列(堆)
java·数据结构·面试题·优先级队列··topk问题
滑水滑成滑头32 分钟前
**标题:发散创新:智能交通系统的深度探究与实现**摘要:本文将详细
java·人工智能·python
冯诺依曼的锦鲤39 分钟前
算法练习:前缀和专题
开发语言·c++·算法
siriuuus1 小时前
Maven 核心概念及生命周期
java·maven
闭着眼睛学算法1 小时前
【双机位A卷】华为OD笔试之【哈希表】双机位A-跳房子I【Py/Java/C++/C/JS/Go六种语言】【欧弟算法】全网注释最详细分类最全的华子OD真题题解
java·c语言·c++·python·算法·华为od·散列表