欢迎来到代码驿站!

JAVA代码

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

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

时间:2020-11-30 12:54:12|栏目:JAVA代码|点击:

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

上一篇:过滤器 和 拦截器的 6个区别(别再傻傻分不清了)

栏    目:JAVA代码

下一篇:SpringBoot从yml配置文件中读常用参数值实例方法

本文标题:java实现一个简单TCPSocket聊天室功能分享

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

推荐教程

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

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

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

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

Copyright © 2020 代码驿站 版权所有