程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> java完成一個簡略TCPSocket聊天室功效分享

java完成一個簡略TCPSocket聊天室功效分享

編輯:關於JAVA

java完成一個簡略TCPSocket聊天室功效分享。本站提示廣大學習愛好者:(java完成一個簡略TCPSocket聊天室功效分享)文章只能為提供參考,不一定能成為您想要的結果。以下是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聊天室功效的代碼,願望對年夜家的進修有所贊助。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved