欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

群聊源代码大全(java聊天室私聊群聊代码)

程序员文章站 2023-11-15 09:43:58
多人聊天室原理图源码工具类:该类用于关闭各种流。public class closeutil { public static void closeall(closeable... closeable)...

多人聊天室原理图

群聊源代码大全(java聊天室私聊群聊代码)

源码

工具类:

该类用于关闭各种流。

public class closeutil {
 public static void closeall(closeable... closeable){
  for(closeable c:closeable){
   if (c != null) {
    try {
     c.close();
    } catch (ioexception e) {
     e.printstacktrace();
    }
   }
  }
 }
}

服务器:

服务器端创建一个serversocket对象通过accept()方法监听是否有tcp连接,同时有一个储存socket对象的集合将连接进来的对象储存到list集合中,服务器将消息进行转发。

//服务器
public class server {
 //存储每一个连接进来的客户端
 public static list<mychannel> list=new arraylist<>();

 public static void main(string[] args) throws exception {
   //创建serversocket对象
  serversocket serversocket = new serversocket(9999);
  while (true){
   //连接进来的客户端
   socket client = serversocket.accept();
   system.out.println(client.getinetaddress()+"进入聊天室");
  mychannel mychannel = new mychannel(client);
  list.add(mychannel);
  new thread(mychannel).start();
  }
 }
}

消息转发类:

具体的消息转发实现类,将信息发给除发送消息以外的其他客户端。

//用于信息转发
public class mychannel implements runnable{
 private datainputstream dis;
 private dataoutputstream dos;
 private boolean flag=true;

 public mychannel(socket socket) {
  try{
   dis=new datainputstream(socket.getinputstream());
   dos=new dataoutputstream(socket.getoutputstream());
  }catch (ioexception e){
   flag=false;
   closeutil.closeall(dis,dos);
  }
 }
 //接收数据的方法
 private string receive(){
  string str="";
  try{
   str= dis.readutf();
  }catch (ioexception e){
   flag=false;
   closeutil.closeall(dis,dos);
   server.list.remove(this);
  }
  return str;
 }
 //发送数据的方法
 private void send(string str){
  try {
   if (str != null && str.length() != 0) {
    dos.writeutf(str);
    dos.flush();
   }
  }catch (exception exception){
   flag=false;
   closeutil.closeall(dos,dis);
   server.list.remove(this);
  }
 }
 //转发消息的方法
 private void sendtoother(){
  string str=this.receive();
  list<mychannel> list = server.list;
  for (mychannel other:list) {
   if(other==list){
    continue;//不发送信息给自己
   }
   //将消息发送给其他客户端
   other.send(str);
  }
 }

 @override
 public void run() {
  while (flag){
   sendtoother();
  }
 }
}

发送信息类:
用于从键盘上获取数据然后将数据发送出去

public class send implements runnable{
 //从键盘上获取数据
 private bufferedreader br;
 private dataoutputstream dos;
 private boolean flag=true;

 public send() {
  br=new bufferedreader(new inputstreamreader(system.in));
 }
 public send(socket socket){
  this();
  try{
   dos=new dataoutputstream(socket.getoutputstream());
  }catch (exception e){
   flag=false;
   closeutil.closeall(dos,socket);
   e.printstacktrace();
  }
 }


 private string getmessage(){
  string str="";
  try{
   str=br.readline();
  }catch (ioexception e){
   flag=false;
   closeutil.closeall(br);
  }
  return str;
 }
 private void send(string str){
  try {
   dos.writeutf(str);
   dos.flush();
  } catch (ioexception e) {
   flag=false;
   closeutil.closeall(dos);
   e.printstacktrace();
  }

 }

 @override
 public void run() {
  while (flag){
   this.send(getmessage());
  }
 }
}

信息接收类:

public class receive implements runnable{
  //接受数据流
  private datainputstream dis;
  private boolean flag=true;


  public receive(socket socket){
    try {
      dis = new datainputstream(socket.getinputstream());
    }catch (exception e){
      flag=false;
      closeutil.closeall(dis,socket);
    }
  }
  private string getmessage(){
    string str="";
    try {
      str=dis.readutf();
    } catch (ioexception e) {
      flag=false;
      closeutil.closeall(dis);
      e.printstacktrace();
    }
    return str;
  }
  @override
  public void run() {
    while (flag){
      system.out.println(this.getmessage());
    }
  }
}

客户端:

public class client {
  public static void main(string[] args) throws exception{
    socket socket = new socket(inetaddress.getlocalhost(),9999);
    send send = new send(socket);
    receive receive = new receive(socket);
    new thread(send).start();
    new thread(receive).start();
  }
}

先将服务器启动然后启动客户端:测试结果如下

群聊源代码大全(java聊天室私聊群聊代码)

有喜欢的小伙伴可以自己拿去玩,代码复制直接有效。