本文介绍: 【代码】多人聊天Java。

服务端


  1. import java.io.*;

  2. import java.net.*;

  3. import java.util.ArrayList;

  4. public class Server{

  5. public static ServerSocket server_socket;

  6. public static ArrayList<Socket> socketList=new ArrayList<Socket>();

  7. public static void main(String []args){

  8. try{

  9. server_socket = new ServerSocket(5000);

  10. while(true){

  11. Socket socket = server_socket.accept();

  12. socketList.add(socket); //把sock对象加入sock集合

  13. ServerBO_Thread st=new ServerBO_Thread(socket,socketList); //初始化多线程

  14. st.start();//启动多线程

  15. }

  16. }catch(Exception ex){

  17. ex.printStackTrace();

  18. }finally{

  19. try{

  20. if(server_socket!=null){

  21. server_socket.close();

  22. }

  23. }catch(Exception ex){

  24. ex.printStackTrace();

  25. }

  26. }

  27. }

  28. public void encryptWrite(String src,DataOutputStream output)throws IOException{

  29. //将一个字符串转化为字符数组

  30. //System.out.println(src);

  31. char[] char_arr = src.toCharArray();

  32. //加密操作

  33. for(int i = 0;i<char_arr.length;i++){

  34. output.writeChar(char_arr[i]+13);

  35. }

  36. //用作结束标志符

  37. output.writeChar(2333);

  38. output.flush();

  39. }

  40. //读取并解密

  41. public String readDecrypt(DataInputStream input)throws IOException{

  42. String rtn="";

  43. while(true){

  44. int char_src =input.readChar();

  45. if(char_src!=2333){

  46. rtn=rtn+(char)(char_src-13);

  47. }else{

  48. break;

  49. }

  50. }

  51. return rtn;

  52. }

  53. }

  54. class ServerBO_Thread extends Thread{

  55. Socket client = null;

  56. ArrayList<Socket> clients;

  57. ServerBO_Thread(Socket s,ArrayList<Socket> ss){//初始化

  58. client=s;

  59. clients=ss;

  60. }

  61. public void run(){

  62. DataInputStream input = null;

  63. DataOutputStream output =null;

  64. try{

  65. input = new DataInputStream(client.getInputStream());

  66. Server bo = new Server();

  67. String receive=null;

  68. String send=null;

  69. while(true){//监视当前客户端有没有发来消息

  70. if(!client.isClosed()){

  71. receive=bo.readDecrypt(input);

  72. clients.trimToSize();

  73. String[] param = receive.split("&");

  74. if(")start".equals(param[1])){ //分析客户端发来的内容

  75. send = param[0]+"进入聊天室";

  76. }else{

  77. send = param[0]+"说: "+param[1];

  78. }

  79. if(!("3333".equals(param[1]))){//3333为退出聊天室信号

  80. for(Socket socket:clients){ //遍历socke集合

  81. //把读取到的消息发送给各个客户端

  82. if(!socket.isClosed()){

  83. output = new DataOutputStream(socket.getOutputStream());

  84. bo.encryptWrite(send,output);

  85. }

  86. }

  87. }else{//如果有客户端退出

  88. for(Socket socket:clients){ //遍历socke集合

  89. if(socket!=client){//告诉其他人此人退出聊天室

  90. if(!(socket.isClosed())){

  91. output = new DataOutputStream(socket.getOutputStream());

  92. bo.encryptWrite(param[0]+"已退出聊天室",output);

  93. }

  94. }

  95. }

  96. output = new DataOutputStream(client.getOutputStream());

  97. bo.encryptWrite("3333",output);//返回信号给要退出的客户端,然后关闭线程

  98. client.close();

  99. input.close();

  100. output.close();

  101. }

  102. }else{

  103. break;

  104. }

  105. }

  106. }catch(Exception ex){

  107. ex.printStackTrace();

  108. }

  109. }

  110. }

客户端


  1. import java.io.IOException;

  2. import java.util.Scanner;

  3. import java.net.*;

  4. import java.io.*;

  5. import java.io.InputStreamReader;

  6. import java.io.OutputStreamWriter;

  7. import java.io.BufferedWriter;

  8. import java.io.BufferedReader;

  9. import java.io.DataOutputStream;

  10. public class People{

  11. //服务端ip

  12. public String ip = "127.0.0.1";

  13. //服务端端口

  14. public int port = 5000;

  15. public DataOutputStream output = null;

  16. public Socket socket = null;

  17. public DataInputStream input = null;

  18. public Scanner sc =new Scanner (System.in);

  19. public String send ;

  20. public String receive;

  21. public String name;

  22. public String sd = null;

  23. public static void main(String[]aa){

  24. People po = new People();

  25. po.start();

  26. }

  27. public void start(){

  28. try{

  29. System.out.println("*******欢迎使用匿名聊天室!**********");

  30. System.out.println("请输入你将要使用的昵称:");

  31. name=sc.nextLine();//获取昵称

  32. socket = new Socket(ip,port);

  33. output=new DataOutputStream(socket.getOutputStream());

  34. input = new DataInputStream(socket.getInputStream());

  35. send = name+"&)start";//把昵称发送到server 告诉所有人有新成员加入聊天室

  36. System.out.println("(如果要退出聊天室请输入“3333”!)");

  37. System.out.println("*******成功进入匿名聊天室!**********");

  38. System.out.println("");

  39. encryptWrite(send,output);

  40. Out out=new Out(output,name,input,socket);

  41. out.start();//启动发送聊天内容的多线程

  42. while(true){

  43. String receive = readDecrypt(input);

  44. if("3333".equals(receive)){//如果收到“3333”则退出聊天室

  45. System.out.println("*******成功退出匿名聊天室!**********");

  46. input.close();

  47. output.close();

  48. socket.close();

  49. System.exit(0);

  50. }

  51. System.out.println(receive);

  52. }

  53. }catch(Exception ex){

  54. ex.printStackTrace();

  55. }finally{

  56. try{

  57. if(socket!=null) socket.close();

  58. input.close();

  59. output.close();

  60. }catch(Exception ex){

  61. ex.printStackTrace();

  62. }

  63. }

  64. }

  65. public void encryptWrite(String src,DataOutputStream output)throws IOException{

  66. //将一个字符串转化为字符数组

  67. char[] char_arr = src.toCharArray();

  68. //加密操作

  69. for(int i = 0;i<char_arr.length;i++){

  70. output.writeChar(char_arr[i]+13);

  71. }

  72. //用作结束标志符

  73. output.writeChar(2333);

  74. output.flush();

  75. }

  76. //读取并解密

  77. public String readDecrypt(DataInputStream input)throws IOException{

  78. String rtn="";

  79. while(true){

  80. int char_src =input.readChar();

  81. if(char_src!=2333){

  82. rtn=rtn+(char)(char_src-13);

  83. }else{

  84. break;

  85. }

  86. }

  87. return rtn;

  88. }

  89. }

  90. class Out extends Thread {

  91. public DataOutputStream output;

  92. public DataInputStream input;

  93. public static String name;

  94. public Socket socket;

  95. public Scanner sc =new Scanner (System.in);

  96. Out(DataOutputStream ot,String n,DataInputStream it,Socket socket){

  97. output=ot;

  98. input=it;

  99. name=n;

  100. }

  101. public void run(){

  102. People po = new People();

  103. try{

  104. while(true){

  105. String send=sc.nextLine();//获取用户输入

  106. String send2=name+"&"+send;//把聊天内容打包成约定形式

  107. po.encryptWrite(send2,output);

  108. }

  109. }catch(Exception ex){

  110. ex.printStackTrace();

  111. }finally{

  112. System.out.println("sfef");

  113. }

  114. }

  115. }

运行结果如下:

原文地址:https://blog.csdn.net/ycq8888/article/details/134852467

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。

如若转载,请注明出处:http://www.7code.cn/show_51109.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除!

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注