Java如何编写一个支持多人互动的聊天室程序?

2026-05-24 03:051阅读0评论SEO问题
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计1062个文字,预计阅读时间需要5分钟。

Java如何编写一个支持多人互动的聊天室程序?

原文:本文字例为大家分享了一个Java实现多人聊天室的总体代码,供大家参考。具体内容如下:

1.客户端

Java如何编写一个支持多人互动的聊天室程序?

package tk.javazhangwei.net.tcp.chat.Demo03;import java.io.BufferedReader;import java.io.DataInputStream;import java

改写后:分享一个Java多人聊天室代码示例。客户端代码如下:

1.客户端

package tk.javazhangwei.net.tcp.chat.Demo03;import java.io.BufferedReader;import java.io.DataInputStream;

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

1.客户端

package tk.javazhangwei.net.tcp.chat.Demo03;   import java.io.BufferedReader; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.Socket;   /***  * 创建客户端  发送数据+接收数据  *   * @author zw  *  */ public class Client {     public static void main(String[] args) throws IOException {         BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));         System.out.println("请输入一个喜欢的名称:");         String name = bf.readLine();         if(name.equals("")) {             return;         }                  Socket client = new Socket("localhost",1025);         //控制台输入信息         //控制台输入信息         new Thread(new Send(client,name)).start();//一条路径         new Thread(new Receive(client)).start();//一条路径 } }

2.服务端(写了个内部类:负责接收与发送多进程)

package tk.javazhangwei.net.tcp.chat.Demo03;   import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; import java.net.ServerSocket; import java.net.Socket; import java.util.ArrayList; import java.util.List;   public class Server {     List<myChannel> all = new ArrayList<myChannel>();                    public static void main(String[] args) throws IOException {         new Server().start();                }               public void start() throws IOException {         ServerSocket server = new ServerSocket(1025);         while (true) {             Socket socket = server.accept();             myChannel mc = new myChannel(socket);             Thread t = new Thread(mc);             all.add(mc);             t.start();     } }     /***  * 一个客户端 一个通路  * @author zw  *  */ class myChannel implements Runnable{     private DataInputStream dis;     private DataOutputStream dos;     private boolean isRuning=true;     private String name;          public myChannel(Socket socket) throws IOException{         try {             dis = new DataInputStream(socket.getInputStream());             dos = new DataOutputStream(socket.getOutputStream());             this.name = dis.readUTF();             send("欢迎进入聊天室");             senOthers(this.name + "进入了聊天室");         } catch (IOException e) {             // TODO Auto-generated catch block             //e.printStackTrace();                 isRuning=false;                 dos.close();                 dis.close();                          }              }     /***      * 读取数据      *       * @return      * @throws IOException      */     private String receive() throws IOException {         String msg ="";         try {             msg =dis.readUTF();         } catch (IOException e) {             // TODO Auto-generated catch block             //e.printStackTrace();             isRuning=false;             dis.close();                  }                           return msg;         }     /***      * 发送数据      * @throws IOException       */     private void send(String msg) throws IOException {         if(msg==null&& msg.equals("")) {             return;         }         try {             dos.writeUTF(msg);             dos.flush();         } catch (IOException e) {             // TODO Auto-generated catch block             //e.printStackTrace();             isRuning=false;             dos.close();             all.remove(this);         }              }     /***      * 发送给其他客户端      * @throws IOException       */     private void senOthers(String msg) throws IOException {         if (msg.startsWith("@")&&msg.indexOf(":")>-1) {// 表示为私聊             //获取name             String name = msg.substring(1, msg.indexOf(":"));             String content = msg.substring(msg.indexOf(":")+1);//获取冒号后的正文             for (myChannel others : all) {                 if(others.name.equals(name)) {                     others.send(this.name+"对您瞧瞧的说:"+content);                 }                              }                                   } else {         //遍历容器         for(myChannel others:all) {             if(others == this) {//如果是本身,就跳过                 continue;             }             others.send(this.name+"对所有人说:"+msg);         }         }     }          @Override     public void run() {         while(isRuning) {             try {                 senOthers(receive()) ;             } catch (IOException e) {                 // TODO Auto-generated catch block                 e.printStackTrace();             }                      }         } } }

3.客户端的发送与接收多进程

package tk.javazhangwei.net.tcp.chat.Demo03;   import java.io.BufferedReader; import java.io.DataOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.net.Socket;   /***  * 发送数据  *   * @author zw  *  */ public class Send implements Runnable{     //控制台输入流     private BufferedReader console;     //管道输出流     private DataOutputStream dos;     private String name;          private boolean isRuning =true;//线程是否运行          public Send() {         console =new BufferedReader(new InputStreamReader(System.in));     }          public Send(Socket client,String name) throws IOException {         this();         try {             dos = new DataOutputStream(client.getOutputStream());             this.name = name;             send(name);         } catch (IOException e) {             // TODO Auto-generated catch block             //e.printStackTrace();              isRuning =false;              dos.close();              console.close();         }     }     private String getMsgFromConsole() {         try {             return console.readLine();         } catch (IOException e) {             // TODO Auto-generated catch block             e.printStackTrace();         }         return "";     }        @Override     public void run() {                  while(isRuning) {             try {                 send(getMsgFromConsole());             } catch (IOException e) {                 // TODO Auto-generated catch block                 e.printStackTrace();             }         }     }       public void send(String msg) throws IOException {         if (msg!=null && !msg.equals("")) {             try {                 dos.writeUTF(msg);                 dos.flush();// 强制刷新             } catch (IOException e) {                 // TODO Auto-generated catch block                 // e.printStackTrace();                 isRuning = false;                 dos.close();                 console.close();               }         }     }   }

package tk.javazhangwei.net.tcp.chat.Demo03;   import java.io.DataInputStream; import java.io.IOException; import java.net.Socket;   /***  * 接收数据  * @author zw  *  */ public class Receive implements Runnable{     //客户端的输入流     private DataInputStream dis;     private boolean isRuning = true;          public Receive() {          }       public Receive(Socket client) {         super();         try {             dis = new DataInputStream(client.getInputStream());         } catch (IOException e) {             // TODO Auto-generated catch block             //e.printStackTrace();             isRuning =false;             try {                 dis.close();             } catch (IOException e1) {                 // TODO Auto-generated catch block                 e1.printStackTrace();             }         }     }     /***      * 接收数据      * @return      * @throws IOException       */     public String receive() throws IOException {         String msg = "";         try {             msg =dis.readUTF();         } catch (IOException e) {             // TODO Auto-generated catch block             e.printStackTrace();             isRuning =false;             dis.close();         }                  return msg;     }     @Override     public void run() {         while(isRuning) {             try {                 System.out.println(receive());             } catch (IOException e) {                 // TODO Auto-generated catch block                 e.printStackTrace();             }         }              }   }

4.效果

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

本文共计1062个文字,预计阅读时间需要5分钟。

Java如何编写一个支持多人互动的聊天室程序?

原文:本文字例为大家分享了一个Java实现多人聊天室的总体代码,供大家参考。具体内容如下:

1.客户端

Java如何编写一个支持多人互动的聊天室程序?

package tk.javazhangwei.net.tcp.chat.Demo03;import java.io.BufferedReader;import java.io.DataInputStream;import java

改写后:分享一个Java多人聊天室代码示例。客户端代码如下:

1.客户端

package tk.javazhangwei.net.tcp.chat.Demo03;import java.io.BufferedReader;import java.io.DataInputStream;

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

1.客户端

package tk.javazhangwei.net.tcp.chat.Demo03;   import java.io.BufferedReader; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.Socket;   /***  * 创建客户端  发送数据+接收数据  *   * @author zw  *  */ public class Client {     public static void main(String[] args) throws IOException {         BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));         System.out.println("请输入一个喜欢的名称:");         String name = bf.readLine();         if(name.equals("")) {             return;         }                  Socket client = new Socket("localhost",1025);         //控制台输入信息         //控制台输入信息         new Thread(new Send(client,name)).start();//一条路径         new Thread(new Receive(client)).start();//一条路径 } }

2.服务端(写了个内部类:负责接收与发送多进程)

package tk.javazhangwei.net.tcp.chat.Demo03;   import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; import java.net.ServerSocket; import java.net.Socket; import java.util.ArrayList; import java.util.List;   public class Server {     List<myChannel> all = new ArrayList<myChannel>();                    public static void main(String[] args) throws IOException {         new Server().start();                }               public void start() throws IOException {         ServerSocket server = new ServerSocket(1025);         while (true) {             Socket socket = server.accept();             myChannel mc = new myChannel(socket);             Thread t = new Thread(mc);             all.add(mc);             t.start();     } }     /***  * 一个客户端 一个通路  * @author zw  *  */ class myChannel implements Runnable{     private DataInputStream dis;     private DataOutputStream dos;     private boolean isRuning=true;     private String name;          public myChannel(Socket socket) throws IOException{         try {             dis = new DataInputStream(socket.getInputStream());             dos = new DataOutputStream(socket.getOutputStream());             this.name = dis.readUTF();             send("欢迎进入聊天室");             senOthers(this.name + "进入了聊天室");         } catch (IOException e) {             // TODO Auto-generated catch block             //e.printStackTrace();                 isRuning=false;                 dos.close();                 dis.close();                          }              }     /***      * 读取数据      *       * @return      * @throws IOException      */     private String receive() throws IOException {         String msg ="";         try {             msg =dis.readUTF();         } catch (IOException e) {             // TODO Auto-generated catch block             //e.printStackTrace();             isRuning=false;             dis.close();                  }                           return msg;         }     /***      * 发送数据      * @throws IOException       */     private void send(String msg) throws IOException {         if(msg==null&& msg.equals("")) {             return;         }         try {             dos.writeUTF(msg);             dos.flush();         } catch (IOException e) {             // TODO Auto-generated catch block             //e.printStackTrace();             isRuning=false;             dos.close();             all.remove(this);         }              }     /***      * 发送给其他客户端      * @throws IOException       */     private void senOthers(String msg) throws IOException {         if (msg.startsWith("@")&&msg.indexOf(":")>-1) {// 表示为私聊             //获取name             String name = msg.substring(1, msg.indexOf(":"));             String content = msg.substring(msg.indexOf(":")+1);//获取冒号后的正文             for (myChannel others : all) {                 if(others.name.equals(name)) {                     others.send(this.name+"对您瞧瞧的说:"+content);                 }                              }                                   } else {         //遍历容器         for(myChannel others:all) {             if(others == this) {//如果是本身,就跳过                 continue;             }             others.send(this.name+"对所有人说:"+msg);         }         }     }          @Override     public void run() {         while(isRuning) {             try {                 senOthers(receive()) ;             } catch (IOException e) {                 // TODO Auto-generated catch block                 e.printStackTrace();             }                      }         } } }

3.客户端的发送与接收多进程

package tk.javazhangwei.net.tcp.chat.Demo03;   import java.io.BufferedReader; import java.io.DataOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.net.Socket;   /***  * 发送数据  *   * @author zw  *  */ public class Send implements Runnable{     //控制台输入流     private BufferedReader console;     //管道输出流     private DataOutputStream dos;     private String name;          private boolean isRuning =true;//线程是否运行          public Send() {         console =new BufferedReader(new InputStreamReader(System.in));     }          public Send(Socket client,String name) throws IOException {         this();         try {             dos = new DataOutputStream(client.getOutputStream());             this.name = name;             send(name);         } catch (IOException e) {             // TODO Auto-generated catch block             //e.printStackTrace();              isRuning =false;              dos.close();              console.close();         }     }     private String getMsgFromConsole() {         try {             return console.readLine();         } catch (IOException e) {             // TODO Auto-generated catch block             e.printStackTrace();         }         return "";     }        @Override     public void run() {                  while(isRuning) {             try {                 send(getMsgFromConsole());             } catch (IOException e) {                 // TODO Auto-generated catch block                 e.printStackTrace();             }         }     }       public void send(String msg) throws IOException {         if (msg!=null && !msg.equals("")) {             try {                 dos.writeUTF(msg);                 dos.flush();// 强制刷新             } catch (IOException e) {                 // TODO Auto-generated catch block                 // e.printStackTrace();                 isRuning = false;                 dos.close();                 console.close();               }         }     }   }

package tk.javazhangwei.net.tcp.chat.Demo03;   import java.io.DataInputStream; import java.io.IOException; import java.net.Socket;   /***  * 接收数据  * @author zw  *  */ public class Receive implements Runnable{     //客户端的输入流     private DataInputStream dis;     private boolean isRuning = true;          public Receive() {          }       public Receive(Socket client) {         super();         try {             dis = new DataInputStream(client.getInputStream());         } catch (IOException e) {             // TODO Auto-generated catch block             //e.printStackTrace();             isRuning =false;             try {                 dis.close();             } catch (IOException e1) {                 // TODO Auto-generated catch block                 e1.printStackTrace();             }         }     }     /***      * 接收数据      * @return      * @throws IOException       */     public String receive() throws IOException {         String msg = "";         try {             msg =dis.readUTF();         } catch (IOException e) {             // TODO Auto-generated catch block             e.printStackTrace();             isRuning =false;             dis.close();         }                  return msg;     }     @Override     public void run() {         while(isRuning) {             try {                 System.out.println(receive());             } catch (IOException e) {                 // TODO Auto-generated catch block                 e.printStackTrace();             }         }              }   }

4.效果

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