多人聊天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();
	}
}

结果

相关推荐
墨染点香11 分钟前
LeetCode Hot100【6. Z 字形变换】
java·算法·leetcode
心情好的小球藻29 分钟前
Python应用进阶DAY9--类型注解Type Hinting
开发语言·python
ldj202034 分钟前
SpringBoot为什么使用new RuntimeException() 来获取调用栈?
java·spring boot·后端
超龄超能程序猿35 分钟前
Spring 应用中 Swagger 2.0 迁移 OpenAPI 3.0 详解:配置、注解与实践
java·spring boot·后端·spring·spring cloud
惜.己41 分钟前
使用python读取json数据,简单的处理成元组数组
开发语言·python·测试工具·json
风象南1 小时前
SpringBoot配置属性热更新的轻量级实现
java·spring boot·后端
洛阳泰山1 小时前
Spring Boot 整合 Nacos 实战教程:服务注册发现与配置中心详解
java·spring boot·后端·nacos
Y4090011 小时前
C语言转Java语言,相同与相异之处
java·c语言·开发语言·笔记
YuTaoShao1 小时前
【LeetCode 热题 100】994. 腐烂的橘子——BFS
java·linux·算法·leetcode·宽度优先
布朗克1681 小时前
java常见的jvm内存分析工具
java·jvm·数据库