会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132575个问题
WEB前端全系列/第十九阶段:Vue2知识体系(旧)/Vue基础知识 26371楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/手写服务器项目(旧) 26372楼
JAVA 全系列/第一阶段:JAVA 快速入门/控制语句、方法、递归算法 26373楼

老师好,以下是我的代码和实现的效果:

package com.lichen.client;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

public class Test {
    public static void main(String[] args) throws IOException {
        System.out.println("----------客户端已启动--------------");
        //1.创建Socket对象
        Socket client = new Socket("localhost", 9999);
        //2.创建输出流
        OutputStream os = client.getOutputStream();
        os.write("Do you copy?".getBytes());
        //3.获取输入流
        InputStream is = client.getInputStream();
        //中转站
        byte[] buf = new byte[1024];
        int len = 0;
        while((len = is.read(buf)) != -1) {
            System.out.println(new String(buf, 0, len));
        }
        //4.关闭流

        if (is != null) {
            is.close();
        }
        if (os != null) {
            os.close();
        }
        if (client != null) {
            client.close();
        }
        System.out.println("----------客户端已关闭--------------");
    }
}
package com.lichen.server;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class Test {
    public static void main(String[] args) throws IOException {
        System.out.println("----------服务端已启动--------------");
        //1.创建ServerSocket对象
        ServerSocket sever = new ServerSocket(9999);
        //2.监听是否有客户端来请求链接
        Socket client = sever.accept();
        //3.获取输入流
        InputStream is = client.getInputStream();
        //中转站
        byte[] buf = new byte[1024];
        int len = 0;
        while((len = is.read(buf)) != -1) {
            System.out.println(new String(buf, 0, len));
            if(len < buf.length) {
                break;
            }
        }
        //4.创建输出流
        //"copy that."
        OutputStream os = client.getOutputStream();
        os.write("Copy that.".getBytes());
        //5.关闭流,关闭Socket
        if(os != null) {
            os.close();
        }
        if(is != null) {
            is.close();
        }
        if(client != null) {
            client.close();
        }
        System.out.println("----------服务端已关闭--------------");
    }
}

我的问题是:为什么服务端在读客户端发来的数据时,要加

            if(len < buf.length) {
                break;
            }

这段代码,而客户端在读服务端的时候就不需要呢?

JAVA 全系列/第二阶段:JAVA 基础深化和提高/网络编程(旧) 26374楼
Python 全系列/第一阶段:Python入门/序列 26377楼
Python 全系列/第五阶段:数据库编程/mysql的使用 26378楼

image.png


public class Z1 {
    public static void main(String[] args) {
        BiaoGe op0 = new BiaoGe(1, "百战牌鼠标", "BZ_001", 99.21, 0.9);
        BiaoGe op1 = new BiaoGe(2, "键盘侠玩偶", "WO_102", 403, 0.7);
        BiaoGe op2 = new BiaoGe(3, "实战java程序设计", "BK_001", 89.00, 0.8);
        BiaoGe op3 = new BiaoGe(4, "蔡幸牌西装", "GQ_XF_12", 700, 0.5);
        BiaoGe op4 = new BiaoGe(5, "大米牌手机", "DM_PH_13", 900, 0.3);

        BiaoGe[] ops =new BiaoGe[5];
        ops[0]=op0;
        ops[1]=op1;
        ops[2]=op2;
        ops[3]=op3;
        ops[4]=op4;
        for (int a=0;a<ops.length;a++){
            System.out.println(ops[a].getId()+"\t"+ops[a].getName()+"\t"+ops[a].getModelNumber()+"\t"+ops[a].getPrice()+"\t"+ops[a].getDiscount());
        }

        System.out.println("下面是静态初始化");
        BiaoGe[] ops1={op0,op1,op2,op3,op4};
        for (int b=0;b<ops.length;b++){
            System.out.println(ops1[b].getId()+"\t"+ops1[b].getName()+"\t"+ops1[b].getModelNumber()+"\t"+ops1[b].getPrice()+"\t"+ops1[b].getDiscount());
        }

        //在创建一个方法,遍历改写toString()方法打印;

        System.out.println("在创建一个方法,遍历改写toString()方法打印;");
        BiaoGe[] s1 ={op0,op1,op2,op3,op4};
        for (int i=0;i< s1.length;i++) {
            System.out.println(s1[i]);
        }


    }
}

    class BiaoGe{
        private int id;
        private String name;
        private String ModelNumber;
        private double price;
        private double discount;


        BiaoGe(){

        }


        public BiaoGe(int id, String name, String modelNumber, double price, double discount) {
            this.id = id;
            this.name = name;
            this.ModelNumber = modelNumber;
            this.price = price;
            this.discount = discount;

        }

        @Override
        public String toString() {
            return "ID:"+id+"\t名称:"+name+"\t 型号:"+ModelNumber+"\t价格:"+price+"\t折扣"+discount;
        }

        public int getId() {
            return id;
        }

        public void setId(int id) {
            this.id = id;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getModelNumber() {
            return ModelNumber;
        }

        public void setModelNumber(String modelNumber) {
            ModelNumber = modelNumber;
        }

        public double getPrice() {
            return price;
        }

        public void setPrice(double price) {
            this.price = price;
        }

        public double getDiscount() {
            return discount;
        }

        public void setDiscount(double discount) {
            this.discount = discount;
        }
    }

一维数组怎么用嵌套循环打印?????写了半天发现打出来不对,,,,空白

JAVA 全系列/第一阶段:JAVA 快速入门/数组和数据存储 26379楼
JAVA 全系列/第一阶段:JAVA 快速入门/JAVA入门和背景知识 26381楼
Python 全系列/第二阶段:Python 深入与提高/模块 26382楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/IO 流技术(旧) 26385楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

©2014-2025百战汇智(北京)科技有限公司 All Rights Reserved 北京亦庄经济开发区科创十四街 赛蒂国际工业园
网站维护:百战汇智(北京)科技有限公司
京公网安备 11011402011233号    京ICP备18060230号-3    营业执照    经营许可证:京B2-20212637