服务器之家

服务器之家 > 正文

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

时间:2020-04-18 12:10     来源/作者:小小小丑

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

1.TCPserver.java

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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聊天室功能的代码,希望对大家的学习有所帮助。

标签:

相关文章

热门资讯

沙雕群名称大全2019精选 今年最火的微信群名沙雕有创意
沙雕群名称大全2019精选 今年最火的微信群名沙雕有创意 2019-07-07
玄元剑仙肉身有什么用 玄元剑仙肉身境界等级划分
玄元剑仙肉身有什么用 玄元剑仙肉身境界等级划分 2019-06-21
男生常说24816是什么意思?女生说13579是什么意思?
男生常说24816是什么意思?女生说13579是什么意思? 2019-09-17
超A是什么意思 你好a表达的是什么
超A是什么意思 你好a表达的是什么 2019-06-06
华为nova5pro和p30pro哪个好 华为nova5pro和华为p30pro对比详情
华为nova5pro和p30pro哪个好 华为nova5pro和华为p30pro对比详情 2019-06-22
返回顶部