java自学day2-网络编程-使用tcp,多线程实现多人聊天室(经典案例)

注:以下代码均为自己学习时编写,bug较多,如有疏漏也请谅解,仅做我自己参考纪念,有兴趣的兄弟完整无bug版(别人写的)附在我另一篇博客中,链接:https://blog.csdn.net/WHUT_XQH/article/details/109112787。

多线程实现客户端的接收功能:

public class ClientReceive implements Runnable {
	private Socket client;
	private String s;
	private DataInputStream br;
	private boolean flag=true;

	
	public ClientReceive(Socket client) {
		this.client=client;
		try {
			br = new DataInputStream(client.getInputStream());
		} catch (IOException e) {
			try {
				br.close();
			} catch (IOException e1) {
				e1.printStackTrace();
			}
			e.printStackTrace();
		}
	}
	@Override
	public void run() {
		while(flag) {
			try {
				s = br.readUTF();
				System.out.println(s);
				if(s.equals("bye")) {
					flag=false;
					System.out.println("准备关闭服务...");
				}
			} catch (IOException e) {
				e.printStackTrace();
			}

		}
		try {
			br.close();
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		try {
			client.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

多线程实现客户端发送功能:

public class Request implements Runnable{
	private Socket client;
	private String s;
	private DataOutputStream bos;
	private BufferedReader br;
	private boolean flag=true;
	private int len;
	private String name;
	
	public Request(Socket client,String name) {
		this.client=client;
		this.name=name;
		try {
			bos = new DataOutputStream(client.getOutputStream());
			 br = new BufferedReader(new InputStreamReader(System.in));
		} catch (IOException e) {
			try {
				bos.close();
			} catch (IOException e1) {
				e1.printStackTrace();
			}
			e.printStackTrace();
		}
	}
	@Override
	public void run() {
		while(flag) {
			try {
				s = br.readLine();
				String ns=name+"说:"+s;
				bos.writeUTF(ns);				
				System.out.println("发送成功!");
				if(s.equals("bye")) {
					System.out.println("准备断开和服务器的连接...");
					flag=false;
				}
				
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		try {
			bos.close();
			br.close();
			client.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

客户端代码:

public class Client {
	public static void main(String[] args) throws Exception {
		Socket s = new Socket("localhost",9999);
		new Thread(new Request(s,"学生3")).start();//更改名字实现:“XXX:说”。
		new Thread(new ClientReceive(s)).start();
	}
}

服务端:

public class Server {
	private static volatile List<Socket> outlist = new ArrayList<>();
	private boolean flag = true;
	ServerSocket ss;
	
	public static List<Socket> getList() {
		return outlist;
	}
	public void server() throws Exception {
		ss = new ServerSocket(9999);
		while(flag){
			Socket client = ss.accept();
			System.out.println("一个客户端连接了...");
			new Thread(new Response(client)).start();
			outlist.add(client);
		}
	}
	public static void main(String[] args) throws Exception {
		new Server().server();
	}
}

服务端转发功能:

public class Response implements Runnable{
	private Socket client;
	private DataInputStream dis; 
	private boolean flag=true;
	private String s;
	private List<Socket> l=null;
	private DataOutputStream bos=null;
	
	public Response(Socket client) throws Exception {
		this.client = client;
		dis = new DataInputStream(client.getInputStream());
	}
	@Override
	public void run() {	
		while(flag) {
			try {		
				s= dis.readUTF();
				l = Server.getList();
				System.out.println(l.size());
				for(int i=0;i<l.size();i++) {
					bos = new DataOutputStream(l.get(i).getOutputStream());
					bos.writeUTF(s);
					bos.flush();
				}
//				for(Socket so:l){
//					System.out.println("开始转发...");
//					bos = new DataOutputStream(so.getOutputStream());
//					bos.writeUTF(s);
//					bos.close();
//				}
				if(s.equals("bye")) {
					flag = false;
				}
				
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
		try {
			dis.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			client.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值