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


本文实例为大家分享了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聊天室功能的代码,希望对大家的学习有所帮助。



相关阅读:
使用C语言详解霍夫曼树数据结构
asp.net判断字符串是否是中文的方法
MySQL中distinct与group by语句的一些比较及用法讲解
Android程序开发通过HttpURLConnection上传文件到服务器
PHP中类的继承和用法实例分析
各种AJAX方法的使用比较详解
win7升级win8系统后鼠标间歇性失灵如何解决
php微信支付之APP支付方法
win10预览版10061系统主题颜色怎么更改
Jquery实现仿腾讯微博发表广播
PHP 抽象方法与抽象类abstract关键字介绍及应用
使用配置类定义Codeigniter全局变量
WordPress主题制作之模板文件的引入方法
如何解决mysqlimport: Error: 13, Can't get stat of 的问题
快速导航

Copyright © 2016 phpStudy |