gpt4 book ai didi

java实现多人聊天对话室

转载 作者:qq735679552 更新时间:2022-09-29 22:32:09 32 4
gpt4 key购买 nike

CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.

这篇CFSDN的博客文章java实现多人聊天对话室由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.

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

java实现多人聊天对话室

了解完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实现多人聊天对话室的文章就讲到这里了,如果你想了解更多关于java实现多人聊天对话室的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com