本文实例为大家分享了Java控制台输出版多人聊天室的具体代码,供大家参考,具体内容如下
多人聊天室就要用到网络以及多线程以及输入输出流的知识
多人聊天室就是多个人同时聊天,一个发一条信息所有人都是可以接收得到.
接下来我们捋清楚一下思路
在这里面服务器充当了一个转发的作用,服务器要做的事情是,得到客户写进缓冲流里面的信息,再把得到的信息再写进其他客户端的缓冲流里面,得到其他用户的缓冲流我们可以通过集合把用户的全部Socket对象写进集合里面,客户端把客户想要发送的信息写进缓冲流里,等待客户端发来的消息,一边等由一边发送消息,这个是属于并发的一个操作,所以读与写可以单独开个线程来进行发送消息.
下面是客户端的代码
import java.io.IOException; import java.net.ServerSocket; import java.net.Socket; import java.util.ArrayList; import java.util.List; public class Server {javascript public static List<Socket> links = new ArrayList<>(); //用来存储客户端的socket对象 public static void main(String[] args) throws IOException { ServerSocket serverSocket = new ServerSocket(6666); while(true){ Socket accept = serverSocket.accept(); links.add(accept); //只要有人连接就可以把他添加进socket对象集合里 ServerThread serverThread = new ServerThread(accept); //开启多线程,实现多人聊天的功能 serverThread.start(); } } //因为频繁的对输入输出流进行资源的关闭,所以写了个关闭资源的工具类 import java.io.Closeable; import java.io.IOException; public class CloseUtil { public static void close(Closeable... closeables){ for (Closeable closeable : closeables) { if (closeable!=null) { try { closeable.close(); } catch (IOException e) { 编程 e.printStackTrace(); } } } } } //多线程的代码如下 import java.io.*; import java.net.Socket; import java.util.List; public class ServerThread extends Thread { private Socket socket; private boolean flag = true; public ServerThread(String name, Socket socket) { super(name); this.socket = socket; } @Override public void run() { while(flag){ sendother(received()); //转发功能 } } private void sendother(String str) { List<Socket> links = Server.links; //因为在写客户端的时候是socket的集合是 静态的属性,所以可以直接通过类型.属性直接获得 for (Socket link : links) { if (link!=socket) send(str,link); //遍历整个对象转发给除当前对象的其他socket对象 } } private void send(String str,Socket socket) { Bufferedwriter bw = null; try { OutputStream outputStream = socket.getOutputStream(); bw = new BufferedWriter(new OutputStreamWriter(outputStream)); bw.write(str); bw.newLine(); bw.flush(); } catch (IOException e) { flag = false; CloseUtil.close(bw,socket); } } private String received(){ BufferedReader br = null; try { InputStream inputStream = socket.getInputStream(); br = new BufferedReader(new InputStreamReader(inputStream)); return br.readLine(); } catch (IOException e) { flag = false; CloseUtil.close(br,socket); } return null; } }
下面是客户端的代码
import java.io.IOException; import java.net.Socket; import java.util.Scanner; public class Client { public static void main(String[] args) throws IOException { Scanner input = new Scanner(System.in); System.out.print("请输入您的名字:"); String name = input.next(); Socket socket = new Socket("127.0.0.1", 6666); //127.0.0.1是指的是本机 Send send = new Send(socket,name); //收发分别开两个线程,可以收发并发执行 Receive receive = n编程ew Receive("",socket); send.start(); receive.start(); } } //发线程 import java.io.BufferedWriter; import java.io.IOException; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.net.Socket; import java.util.Scanner; public class Send extends Thread { private Socket socket; private boolean flag = true; public Send(Socket socket,String name) { super(name); this.socket = socket; } @Override public void run() { while(flag){ send(); //一直循环保证客户可以一直发 } } private void send() { Scanner input = new Scanner(System.in); BufferedWriter bw= null; try { OutputStream outputStream = socket.getOutputStream(); bw = new BufferedWriter(new OutputStreamWriter(outputStream)); bw.write(this.getName()+"说:"+input.next()); // 当前线程名+在客户端在控制台输出的一并发给服务端 bw.newLine(); bw.flush(); } catch (IOException e) { flag = false; //出现异常的时候关闭循环,防止陷入死循环 CloseUtil.close(bw,socket); } } } //收线程 import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.Socket; public class Receive extends Thread { private Socket socket; private boolean flag = true; public Receive(String name, Socket socket) { super(name); this.socket = socket; } @Override public void run() { while (flag){ System.out.println(received()); //循环保证能一直接收服务器发来的消息 } } public String received(){ BufferedReader br =null; try { InputStream inputStream = socket.getInputStream(); br = new BufferedReader(new InputStreamReader(inputStream)); String line = br.readLine(); return this.getName()+line; } catch (IOException e) { flag = false; CloseUtil.close(br,socket); } return ""; } }
以上就是简易版聊天室,希望对大家的学习有所帮助,也希望大家多多支持我们。
海报
0 条评论
61
相关文章
本站已关闭游客评论,请登录或者注册后再评论吧~