好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

java实现多人聊天对话室

使用ServerSocket和Socket实现服务器端和客户端的Socket通信。

了解完socket通信步骤后可以发现本实验需要写两个类:Server和Client,并且要先运行Server再运行Client。

先构造服务器端

?

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

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

import java.net.ServerSocket;

import java.net.Socket;

import java.util.concurrent.ExecutorService;

import java.util.concurrent.ScheduledThreadPoolExecutor;

 

/**

  * @author: Ren

  * @date: 2020-08-03  15:23

  * @description:

  */

public class TcpserverC2 {

     public static void main(String[] args) throws IOException {

//        定义多线程,让多个用户都可以参与到聊天室

         ExecutorService pool = new ScheduledThreadPoolExecutor( 10 );

//        绑定端口

         ServerSocket serverSocket = new ServerSocket( 8888 );

//        利用循环一直来读取新的socket

         while ( true ) {

//            开始serversocket侦听请求,这方法会阻塞等待tcp请求的到来,一旦到来,就返回

             Socket accept = serverSocket.accept();

             pool.execute( new Runnable() {

                 Socket socket = accept;

//                定义字节数组来读取输入流

                 byte [] buf = new byte [ 1024 ];

                 @Override

                 public void run() {

//                    定义输入输出流

                     InputStream in = null ;

                     OutputStream out = null ;

                     try {

                         in = socket.getInputStream();

                         out = socket.getOutputStream();

                         while ( true ) {

                             int len = in.read(buf);

                             System.out.println( "服务器收到:" + new String(buf, 0 , len, ( "utf-8" )));

                             // 聊天室服务端一般不会参与对话,所以一般不加这个功能

//                            String xitongshuohua = scanner.nextLine();

//                            out.write("谢谢".getBytes(Charset.forName("utf-8")));

                             out.write( "谢谢" .getBytes(( "utf-8" )));

 

                         }

                     } catch (IOException e) {

                         e.printStackTrace();

                     }

                 }

             });

         }

     }

}

再构建客户端

?

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

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

import java.net.Socket;

import java.util.Scanner;

 

/**

  * @author: Ren

  * @date: 2020-08-03  15:23

  * @description:

  */

public class TcpClientC2 {

     public static void main(String[] args) throws IOException {

//        目标地址,目标端口

         Socket socket = new Socket( "127.0.0.1" , 8888 );

         InputStream in = socket.getInputStream();

         OutputStream out = socket.getOutputStream();

         Scanner scanner = new Scanner(System.in);

         byte [] buf = new byte [ 1024 ];

         while ( true ) {

             String word = scanner.nextLine();

             out.write(word.getBytes(( "utf-8" )));

             int lrn = in.read(buf);

             System.out.println( "服务端回复:" + new String(buf, 0 ,lrn,( "utf-8" )));

         }

     }

}

服务器端构建在一个主机上,然后在多台电脑创建客户端,并访问服务器端所在的主机就可以构成聊天室的效果,当然前提是在同一个局域网下。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

原文链接:https://blog.csdn.net/weixin_44281922/article/details/107770997

查看更多关于java实现多人聊天对话室的详细内容...

  阅读:12次