java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > java多人聊天室

java编程实现多人聊天室功能

作者:奕起学习

这篇文章主要为大家详细介绍了java编程实现多人聊天室功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了java实现多人聊天室的具体代码,供大家参考,具体内容如下

程序源代码及运行截图:

server.java

//server.java
package Socket;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class server implements Runnable {// 服务端
  static List<Socket> socketList=new ArrayList<Socket>();
// 读取 In
  static Socket socket = null;
  static ServerSocket serverSocket = null;
  public server() {// 构造方法
    try {
      serverSocket = new ServerSocket(9999);
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
  public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    System.out.println("************服务端*************");
    server t = new server();
    int count = 0;
    while (true) {     
      try {
//       System.out.println("端口9999等待被连接......");
        socket = serverSocket.accept();
        count++;
        System.out.println("第" + count + "个客户已连接");
        socketList.add(socket);
      } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
      Print p = new Print(socket);
      Thread read = new Thread(t);
      Thread print = new Thread(p);
      read.start();
      print.start();
    }
  }
  @Override
  public void run() {
    // 重写run方法
    try {
      Thread.sleep(1000);
      BufferedReader in = new BufferedReader(new InputStreamReader(socket
          .getInputStream()));
      while (true) {
        String jieshou = in.readLine();
        System.out.println( jieshou);
        for (int i = 0; i < socketList.size(); i++) {
          Socket socket=socketList.get(i);
          PrintWriter out = new PrintWriter(socket.getOutputStream());
          if (socket!=this.socket) {
            out.println(jieshou);
          }else{
            out.println("(你)"+jieshou);
          }
          out.flush();
        }
      }
    } catch (Exception e) {

      e.printStackTrace();
    }
  }
}
class Print implements Runnable {
  static List<Socket> socketList=new ArrayList<Socket>();
  Scanner input = new Scanner(System.in);
  public Print(Socket s) {// 构造方法
    try {
      socketList.add(s);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
  @Override
  public void run() {
    try {
      Thread.sleep(1000);
      while (true) {
        String msg = input.next();
      for (int i = 0; i < socketList.size(); i++) {
        Socket socket=socketList.get(i);
        PrintWriter out = new PrintWriter(socket.getOutputStream());
        // System.out.println("对客户端说:");
        out.println("服务端说:"+msg);
        out.flush();
      }
      }
    } catch (Exception e) {
      // TODO: handle exception
      e.printStackTrace();
    }
  }
}

client.java

//client.java
package Socket;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;
public class client implements Runnable {// 客户端
  static Socket socket = null;
  Scanner input = new Scanner(System.in);
  static String name=null;
  public static void main(String[] args) {
    int x=(int)(Math.random()*100);
    client.name="client"+x;
    System.out.println("************客户端"+x+"*************");
    try {
      socket = new Socket("127.0.0.1", 9999);
      System.out.println("已经连上服务器了");
    } catch (Exception e) {
      e.printStackTrace();
    }
    client t = new client();
    Read r = new Read(socket);
    Thread print = new Thread(t);
    Thread read = new Thread(r);
    print.start();
    read.start();
  }
  @Override
  public void run() {
    try {
      Thread.sleep(1000);     
      PrintWriter out = new PrintWriter(socket.getOutputStream());
      while (true) {
        String msg = input.next();
        out.println(name+"说:"+msg);
        out.flush();
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}
class Read implements Runnable {
  static Socket socket = null;
  public Read(Socket socket) {
    this.socket = socket;
  }
  @Override
  public void run() {
    try {
      Thread.sleep(1000);
      BufferedReader in = new BufferedReader(new InputStreamReader(socket
          .getInputStream()));
      while (true) {
        System.out.println( in.readLine());
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}

测试数据一:

服务端程序运行截图:

客户端1程序运行截图:

客户端2程序运行截图:

由测试数据一可以看出:程序可以实现多人聊天,并且效果和性能还算可以。而且每个客户端连接进服务器时,都会随机产生一个随机数作为自身的标志,避免通话过程中,分辨不清彼此,而导致交流效果不好。

测试数据二:

服务端运行截图:

客户端1程序运行截图:

客户端2程序运行截图:

客户端3程序运行截图:

客户端4程序运行截图:

客户端5程序运行截图:

客户端6程序运行截图:

客户端7程序运行截图:

 客户端8程序运行截图:

客户端9程序运行截图:

客户端10程序运行截图:

测试二一共连入了10个客户端进行程序稳定性的测试,程序运行结果稳定,符合实验预期结果,充分说明了程序的鲁棒性较强。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

您可能感兴趣的文章:
阅读全文