欢迎来到代码驿站!

JAVA代码

当前位置:首页 > 软件编程 > JAVA代码

Java Socket通信之聊天室功能

时间:2020-10-27 12:33:47|栏目:JAVA代码|点击:

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

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<Socket> clients; 
  ServerSocket server; 
  public Server() { 
    try { 
      port = 8083; 
      clients = new ArrayList<Socket>(); 
      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基础之java处理ip的工具类

栏    目:JAVA代码

下一篇:通过实例了解Java Integer类和int的区别

本文标题:Java Socket通信之聊天室功能

本文地址:http://www.codeinn.net/misctech/16334.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有