java实现一个简单TCPSocket聊天室功能分享

作者:小小小丑 时间:2022-06-11 20:04:36 

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

1.TCPserver.java


import java.net.*;
import java.io.*;
import java.util.*;
import java.util.concurrent.*;
public class TCPserver{
private static final int SERVERPORT = 8888;
private ServerSocket MyServer = null;
private List<Socket> Clients = new ArrayList<Socket>();
private ExecutorService mExecutorService;
public TCPserver(){
 try{
  MyServer = new ServerSocket(SERVERPORT);
  mExecutorService = Executors.newCachedThreadPool();
  System.out.println("start:");
  Socket MySocket = null;
  while(true){
  MySocket = MyServer.accept();
  Clients.add(MySocket);
  mExecutorService.execute(new ThreadServer(MySocket));
  }
 }catch(Exception e){
   e.printStackTrace();
   System.exit(0);
  }
}
class ThreadServer implements Runnable{
 private Socket msocket=null;
 private BufferedReader in= null;
 private PrintWriter out = null;
 private String mStrMSG = null;
 public ThreadServer(Socket socket) {
  try{  
  this.msocket=socket;
  in = new BufferedReader(new InputStreamReader(msocket.getInputStream(), "GB2312"));
  mStrMSG = "user:" + msocket.getInetAddress() + " come total:" + Clients.size();
  SendMassage();
  }catch(IOException e){
   System.out.println("erorr");
   System.exit(0);
  }
 }
 private void SendMassage(){
  try{
   System.out.println(mStrMSG);
   for(Socket MySocket:Clients)
   {
   out = new PrintWriter(new OutputStreamWriter(MySocket.getOutputStream(),"GB2312"),true);
   out.println(mStrMSG);
   }
  }catch(IOException e){
   System.out.println("erorr");
   System.exit(0);
  }
 }
 public void run(){
  try{
   while((mStrMSG = in.readLine())!=null){
    if(mStrMSG.trim().equals("exit")){
     Clients.remove(msocket);
     in.close();
     out.close();
     mStrMSG = "user:" + msocket.getInetAddress() + " exit tatal:" + Clients.size();      ;
     msocket.close();
     SendMassage();      
     break;
    }
    else{
     mStrMSG = msocket.getInetAddress()+":" + mStrMSG;
     SendMassage();
    }

}
  }catch(IOException e){
   System.out.println("erorr");
   System.exit(0);
  }

}
}
public static void main(String[] args){
 new TCPserver();
}
}

2.TCPclient.java


import java.net.*;
import java.io.*;
import java.util.concurrent.*;
public class TCPclient {
private static final int PORT = 8888;
private Socket Client = null;
private BufferedReader sin = null;
private ExecutorService mExecutorService;
public TCPclient(){
 try{
  Client = new Socket("120.27.126.174",PORT);
 sin = new BufferedReader(new InputStreamReader(Client.getInputStream(),"GB2312"));
 mExecutorService = Executors.newCachedThreadPool();
 mExecutorService.execute(new ThreadClient(Client));
 String msg = null;
 while((msg = sin.readLine()) != null) {
   System.out.println(msg);
  }
 }catch(IOException e){
     System.out.println(e.getMessage());
   }

}
class ThreadClient extends Thread{
 private Socket mSocket = null;
 private String msg = null;
 BufferedReader in = null;
 PrintWriter out = null;
  public ThreadClient(Socket socket){
   this.mSocket = socket;
  }
  public void run(){
   try{
    in = new BufferedReader(new InputStreamReader(System.in));
    out = new PrintWriter(new OutputStreamWriter(mSocket.getOutputStream(), "GB2312"), true);
    while(true){
    msg = in.readLine();
    out.println(msg);
     if(msg.trim().equals("exit")){
      in.close();
      out.close();
       mExecutorService.shutdownNow();    
     break;
     }
    }
   }catch(IOException e){
    System.out.println("see you");
    System.exit(0);
   }
  }
}
public static void main(String[] args){
 new TCPclient();
}
}
标签:java,TCPSocket,聊天室
0
投稿

猜你喜欢

  • 详解ViewBinding用法

    2023-07-01 21:38:41
  • Android app启动图适配方法实例

    2023-07-31 16:23:34
  • 第一次编写Java流布局图形界面

    2023-10-13 08:27:11
  • Android中文件读写(输入流和输出流)操作小结

    2023-07-23 02:53:19
  • C#特性-迭代器(上)及一些研究过程中的副产品

    2023-12-05 18:26:49
  • Java NIO和IO的区别

    2023-07-15 22:53:46
  • C#使用Unity实现剪刀石头布游戏

    2023-03-01 06:30:18
  • Android 中CheckBox多项选择当前的position信息提交的示例代码

    2022-05-15 19:13:51
  • 详解Java中restTemplate的使用

    2023-06-19 23:20:29
  • Android WebView无法弹出软键盘的原因及解决办法

    2023-07-07 18:30:00
  • JAVA与SQL 中的null与NULL解析

    2023-06-23 11:51:18
  • 零基础写Java知乎爬虫之获取知乎编辑推荐内容

    2023-11-29 04:33:39
  • Java实现在线SQL编程最新完整版

    2022-04-12 01:03:45
  • 使用开源项目JAVAE2 进行视频格式转换

    2023-11-08 07:18:24
  • android如何改变editText控件中部分文字的格式

    2023-08-24 18:58:36
  • 基于springboot i18n国际化后台多种语言设置的方式

    2022-03-28 16:02:56
  • JDK源码之PriorityQueue解析

    2022-05-15 17:17:15
  • Android 简单实现倒计时功能

    2023-06-27 11:35:40
  • Flutter 构建一个常用的页面框架

    2022-10-02 02:54:19
  • android开发教程之view组件添加边框示例

    2023-05-24 17:16:44
  • asp之家 软件编程 m.aspxhome.com