使用对象流传输Student类

上一篇博客已经介绍了如何通过定义原始的字节流来传输Student类,我们接下来继续练习如何通过对象流传输Student类。
因为通过对象流传输的对象以及所包含的变量都必须要实现Serializable的接口,所以为了方便,我把上篇博客的Student类的Image变量改成了二维数组。

Student类

为了便于观察二维数组的传输是否成功,重写toString方法时,输出二维数组的两个数据。

package RPC.v2.myTest01;

import java.io.Serializable;

public class Student implements Serializable {
    int id;
    String name;
    long liveing;
    int[][] image;

    public Student(int id, String name, long liveing, int[][] image) {
        this.id = id;
        this.name = name;
        this.liveing = liveing;
        this.image = image;
    }

    public Student() {
    }

    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", liveing=" + liveing +
                ", image[0][0]=" + image[0][0] + "  image[0][1]=" + image[0][1] +
                '}';
    }
}


客户端

每隔1s新建一个Student对象,输出到控制台,再向服务端发送

package RPC.v2.myTest01;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.Socket;
import java.util.Random;

import static java.lang.Thread.sleep;

/**
* @author liTianLu
* @Date 2022/8/5 16:57
* @purpose 自定义协议,客户端给服务端发送一个Student对象
*/
public class Client {
    public static void main(String[] args) {
        try {
            Socket client = new Socket("127.0.0.1",9999);
            System.out.println("已连接到服务端端口9999...");
            OutputStream ous = client.getOutputStream();
            InputStream ins = client.getInputStream();
            ObjectOutputStream oous = new ObjectOutputStream(ous);
            //初始化image二维数组
            int w = 3; int h = 4;
            int[][] img = new int[w][h];
            Random ran = new Random();
            int id = 1;
            for (int i = 0; i < w; i++) {
                for (int j = 0; j < h; j++) {
                    img[i][j] = ran.nextInt(10000);
                }
            }

            while(true){


                Student stu = new Student(id++,"李天路",2100000000,img);
                // System.out.println("创建Student对象成功!");
                oous.writeObject(stu);
                 System.out.println("成功发送对象stu: " + stu);
                stu.id++;

                sleep(1000);
            }

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

    
}

服务端

接收客户端传来的Student对象,并输出,观察其与客户端输出是否一致

package RPC.v2.myTest01;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

public class Server {
    public static void main(String[] args) {

        try {
            ServerSocket serverSocket = new ServerSocket(9999);
            System.out.println("端口9999正在监听中。。。");
            Socket client = serverSocket.accept();
            System.out.println("客户端端口"+client.getPort()+"已经连接");
            OutputStream ous = client.getOutputStream();
            InputStream ins = client.getInputStream();
            ObjectInputStream oins = new ObjectInputStream(ins);

            while(true){
                Student stu = (Student) oins.readObject();
                System.out.println("收到来自"+client.getPort()+"的stu对象:" + stu );
            }
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

}

运行结果:

客户端

image

服务端

image

posted @ 2022-08-07 09:46  classic123  阅读(51)  评论(0编辑  收藏  举报