利用对象流实现网络中传输对象

一、服务端代码

public class MyServer {
    
    
    public static void main(String[] args) {
    
    
        ServerSocket serverSocket = null;
        Socket socket = null;
        InputStream inputStream = null;
        ObjectInputStream objectInputStream = null;
        try {
    
    
            serverSocket = new ServerSocket(8888);
            System.out.println("服务启动成功!");
            socket = serverSocket.accept();

            // 获取对象流
            inputStream = socket.getInputStream();
            objectInputStream = new ObjectInputStream(inputStream);
            // 接收
            Student student = (Student) objectInputStream.readObject();
            System.out.println(student.toString());
        } catch (IOException e) {
    
    
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
    
    
            e.printStackTrace();
        } finally {
    
    
            try {
    
    
                if (objectInputStream!=null) objectInputStream.close();
                if (inputStream!=null) inputStream.close();
                if (socket!=null) socket.close();
                if (serverSocket!=null) serverSocket.close();
            } catch (IOException e) {
    
    
                e.printStackTrace();
            }
        }
    }
}

二、客户端代码

public class MyClient {
    
    
    public static void main(String[] args) {
    
    
        Socket socket = null;
        OutputStream outputStream = null;
        ObjectOutputStream objectOutputStream =null;
        try {
    
    
            // 创建连接
            socket = new Socket("localhost", 8888);
            Student student = new Student(1001, "zs", 23);
            // 对象流
            outputStream = socket.getOutputStream();
            objectOutputStream = new ObjectOutputStream(outputStream);
            // 发送
            objectOutputStream.writeObject(student);
            socket.shutdownOutput();
        } catch (IOException e) {
    
    
            e.printStackTrace();
        } finally {
    
    
            try {
    
    
                if (objectOutputStream!=null) objectOutputStream.close();
                if (outputStream!=null) outputStream.close();
                if (socket!=null) socket.close();
            } catch (IOException e) {
    
    
                e.printStackTrace();
            }
        }
    }
}

三、实体类

public class Student implements Serializable {
    
    
    private int sid;
    private String sname;
    private int age;

    public Student(){
    
    }

    public Student(int sid, String sname, int age) {
    
    
        this.sid = sid;
        this.sname = sname;
        this.age = age;
    }

    public int getSid() {
    
    
        return sid;
    }

    public void setSid(int sid) {
    
    
        this.sid = sid;
    }

    public String getSname() {
    
    
        return sname;
    }

    public void setSname(String sname) {
    
    
        this.sname = sname;
    }

    public int getAge() {
    
    
        return age;
    }

    public void setAge(int age) {
    
    
        this.age = age;
    }

    @Override
    public String toString() {
    
    
        return "Student{" +
                "sid=" + sid +
                ", sname='" + sname + '\'' +
                ", age=" + age +
                '}';
    }
}

猜你喜欢

转载自blog.csdn.net/m0_46218511/article/details/107719440