Java网络通讯案例——即时通讯(控制台版)

一、需求分析

  • 用户与用户之间1-1或1-n通讯

二、技术分析
(一)客户端

  • 客户端的功能有两个:发消息和接消息
  • 发消息:使用Socket技术的流式输出,配合打印流封装发送。
  • 接信息:使用读取专用线程,搭配字符缓冲流读取。

(二)服务端

  • 服务端的功能:转发消息。
  • 实现转发功能:
    • 1、创建服务通道ServerSocket
    • 2、建立线程池ExecutorService->ThreadPoolExecutor
    • 3、等待连接
    • 4、向集合添加通道对象
    • 5、添加转发任务到线程池任务队列
    • 6、转发线程类的重写
      • 第 1 部分:接收信息: 字符缓冲流读取接收的信息。
      • 第 2 部分:转发信息: 遍历所有通道,获取输出流输出数据。

三、实现代码
客户端

class Client1{
   
    public static void main(String[] args) {
   
        try {
   
            //建立通道
            Socket sender = new Socket(InetAddress.getLocalHost().getHostAddress(),7777);

            new ClientServiceThread(sender).start();

            Scanner in = new Scanner(System.in);

            if (sender.isConnected()) System.out.println(sender.getRemoteSocketAddress() + " 已连接!");

            //输出流
            PrintStream outStream = new PrintStream(sender.getOutputStream(),true);
            String message;
            while(!(message = in.nextLine()).equals("exit")){
   
                outStream.println(message);
            }

        } catch (IOException e) {
   
            System.out.println("连接已断开");
        }
    }
}

class Client2{
   
    public static 
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Zain_horse

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值