我们专注攀枝花网站设计 攀枝花网站制作 攀枝花网站建设
成都网站建设公司服务热线:400-028-6601

网站建设知识

十年网站开发经验 + 多家企业客户 + 靠谱的建站团队

量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决

JavaSocket通信之聊天室功能

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

创新互联专注为客户提供全方位的互联网综合服务,包含不限于成都网站制作、成都做网站、安义网络推广、微信平台小程序开发、安义网络营销、安义企业策划、安义品牌公关、搜索引擎seo、人物专访、企业宣传片、企业代运营等,从售前售中售后,我们都将竭诚为您服务,您的肯定,是我们最大的嘉奖;创新互联为所有大学生创业者提供安义建站搭建服务,24小时服务热线:028-86922220,官方网址:www.cdcxhl.com

Client.java

import java.io.*; 
import java.net.*; 
import java.util.*; 
 
public class Client { 
 public int port = 8083; 
 Socket socket = null; 
 
 public static void main(String[] args) { 
  new Client(); //开始执行 
 } 
 
 public Client() { 
 
  try { 
   socket = new Socket("127.0.0.1", port); 
   new Cthread().start(); 
 
   BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream())); 
   String msg1; 
   while ((msg1 = br.readLine()) != null) { 
    System.out.println(msg1); 
   } 
  } catch (Exception e) { 
  } 
 } 
 
 class Cthread extends Thread { 
 
  public void run() { 
   try { 
 
    BufferedReader re = new BufferedReader(new InputStreamReader(System.in)); 
    PrintWriter pw = new PrintWriter(socket.getOutputStream(), true); 
    String msg2; 
 
    while (true) { 
 
     msg2 = re.readLine(); 
     pw.println(msg2); 
    } 
   } catch (Exception e) { 
    e.printStackTrace(); 
   } 
 
  } 
 } 
 
} 

Server.java

import java.net.*; 
import java.io.*; 
import java.util.*; 
 
public class Server { 
  int port; 
  List clients; 
  ServerSocket server; 
  public Server() { 
    try { 
      port = 8083; 
      clients = new ArrayList(); 
      server = new ServerSocket(port); 
      System.out.println("服务器已启动"); 
      int co=1; 
      while (true) { 
        Socket socket = server.accept(); 
        clients.add(socket); 
        Mythread mythread = new Mythread(socket,co); 
        co++; 
        mythread.start(); 
      } 
 
    } catch (Exception ex) { 
      System.out.println("vv"); 
    } 
  } 
 
  public static void main(String[] args) { 
    new Server(); 
  } 
 
  class Mythread extends Thread { 
    Socket ssocket; 
    private int count; 
    private BufferedReader br; 
    private PrintWriter pw; 
    public String msg; 
 
    public Mythread(Socket s,int c) { 
      ssocket = s; 
      count=c; 
    } 
 
    public void run() { 
 
      try { 
        br = new BufferedReader(new InputStreamReader(ssocket.getInputStream())); 
         
        msg = "欢迎【" +"Client "+count + "】进入聊天室!当前聊天室有【" + clients.size() + "】人"; 
 
        sendMsg(); 
 
        while ((msg = br.readLine()) != null) { 
           
          msg = "【" +"Client "+count + "】说:" + msg; 
          sendMsg(); 
 
        } 
      } catch (Exception ex) { 
      } 
    } 
    public void sendMsg() { 
      try { 
        System.out.println(msg); 
 
        for (int i = clients.size() - 1; i >= 0; i--) { 
          pw = new PrintWriter(clients.get(i).getOutputStream(), true); 
          pw.println(msg); 
          pw.flush(); 
        } 
      } catch (Exception ex) { 
      } 
    } 
  } 
 
} 

运行结果:

Java Socket通信之聊天室功能

Java Socket通信之聊天室功能

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


文章标题:JavaSocket通信之聊天室功能
文章链接:http://shouzuofang.com/article/jhhoph.html

其他资讯