网络编程基本知识与实践(二)

网络编程基本知识与实践(二)

(一)中已经把大致环境搭建完成了,接下来我们需要让客户端不停访问服务器,首先思路是让这一段代码不断循环,同时streamUtils.closeStream();注释掉这一行,不关闭流:

public void connect(){
        try {

                Socket client = new Socket(this.IP,this.port);
                PrintUtil.clientPrint("服务器连接成功");
                StreamUtils streamUtils = new StreamUtils(client);
            while (true){
                streamUtils.write("Client");
                PrintUtil.clientPrint("发送完完数据");
                streamUtils.readAndprint();
                PrintUtil.clientPrint("接受信息完毕!");
            }
//                streamUtils.closeStream();


        } catch (IOException e) {
            e.printStackTrace();
        }
    }

这里写图片描述
这里写图片描述
服务器在执行Socket client = serverSocket.accept();就处于阻塞状态了。因为这个accept()方法只能接收新的套接字,所以我们可以修改客户端,每次请求一次就新建一个socket,用完就关闭流。

public void connect(){
        try {
            while (true) {
                Socket client = new Socket(this.IP, this.port);
                PrintUtil.clientPrint("服务器连接成功");
                StreamUtils streamUtils = new StreamUtils(client);

                streamUtils.write("Client");
                PrintUtil.clientPrint("发送完完数据");
                streamUtils.readAndprint();
                PrintUtil.clientPrint("接受信息完毕!");

                streamUtils.closeStream();
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

这里写图片描述
这里写图片描述
这样就解决了刚才的问题。
但这样很不安全,每次访问中间都存在间隙,这样就给坏人拦截我们信息的机会,所以我们希望我们能连上就不要断开,可以一直与服务器保持联系,这时候我们就需要把服务器上处理与发送消息的那段代码放到子线程中
服务器端:

public void start(){
        try {
            //创建对象,对象的属性和方法调用(API学习核心内容)
            ServerSocket serverSocket = new ServerSocket(this.port);
            PrintUtil.serverPrint("端口开放成功");
            while (true){
                PrintUtil.serverPrint("调用方法准备接受连接");
                //每个客户端都有一个独立的空间
                Socket client = serverSocket.accept();//接收客户端连接,一次客户端的连接套接字
                System.out.println(client.getInetAddress());
                final StreamUtils streamUtils = new StreamUtils(client);
                Thread thread = new Thread(){
                    @Override
                    public void run() {
                        try {
                            while (true) {

                                streamUtils.readAndprint();
                                PrintUtil.serverPrint("接收完数据");
                                streamUtils.write("Server");
                                PrintUtil.serverPrint("发送完数据");

                            }

                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                };
                thread.start();
//                streamUtils.closeStream();

            }

        } catch (IOException e) {
            e.printStackTrace();
            PrintUtil.serverPrint("端口开发失败");
        }
    }

客户端:

public void connect(){
        try {
            Socket client = new Socket(this.IP, this.port);
            PrintUtil.clientPrint("服务器连接成功");
            while (true) {
                StreamUtils streamUtils = new StreamUtils(client);
                streamUtils.write("Client");
                PrintUtil.clientPrint("发送完完数据");
                streamUtils.readAndprint();
                PrintUtil.clientPrint("接受信息完毕!");
//                streamUtils.closeStream();
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

就可以实现一次连接后多次收发数据。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值