网络编程应用:基于TCP协议【实现对象传输】--练习

要求:

基于TCP协议实现,客服端向服务器发送一个对象
服务器接受并显示用户信息 ,同时返回给客户端 "数据已收到"

建一个Student类,属性:name age

Student类

package Homework3;

import java.io.Serializable;

public class Student implements Serializable {
    private static final long serialVersionUID = 1L;
    private String name;
    private int age;
    private String sex;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }
    @Override
    public String toString() {
        return "Student [name=" + name + ", age=" + age + ", sex=" + sex + "]";
    }
    public Student(String name, int age, String sex) {
        super();
        this.name = name;
        this.age = age;
        this.sex = sex;
    }
    public Student() {
        super();
    }

}

客户端:

package Homework3;

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

public class Client {
    public static void main(String[] args) {
        System.out.println("客户端已启动!");

        //1.创建要传输的Student对象
        Student student=new Student("吴德利", 30, "女");

        Socket socket=null;
        OutputStream os=null;
        InputStream is=null;
        ObjectOutputStream oos=null;
        byte[] b=new byte[1024];

        try {
            //2.创建Socket对象,并得到相应的输出流----以及创建对象流
            socket=new Socket("localhost", 9999);
            os=socket.getOutputStream();
            oos=new ObjectOutputStream(os);

            //3.将对象发送给服务器
            oos.writeObject(student);

            //4.接收服务器端传输完毕,返回的回复
            is=socket.getInputStream();
            is.read(b);
            String string=new String(b);
            System.out.println(string);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            if(os!=null){
                try {
                    os.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            if(is!=null){
                try {
                    is.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            if(socket!=null){
                try {
                    socket.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            if(oos!=null){
                try {
                    oos.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }

    }
}

服务器端:

package Homework3;

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

public class Server {
    public static void main(String[] args) {
        System.out.println("服务器端已启动!");

        ServerSocket serverSocket=null;
        Socket socket=null;
        ObjectInputStream ois=null;
        OutputStream os=null;
        InputStream is=null;
        try {
            //1.创建ServerSocket对象,获得Socket对象以及输入
            serverSocket=new ServerSocket(9999);
            socket=serverSocket.accept();
            is=socket.getInputStream();

            //2.创建对象流
            ois=new ObjectInputStream(is);

            //3.接收数据,并将接收的对象转为原来的对象类型
            Student student=(Student)ois.readObject();
            System.out.println(student);

            //4.返回给客户端,"数据已收到"
            os=socket.getOutputStream();
            os.write("数据已收到".getBytes());
            os.flush();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
            if(serverSocket!=null){
                try {
                    serverSocket.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            if(socket!=null){
                try {
                    socket.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            if(os!=null){
                try {
                    os.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            if(is!=null){
                try {
                    is.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            if(ois!=null){
                try {
                    ois.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }
}

运行结果:

客户端:
这里写图片描述


服务器端:

这里写图片描述

原文地址:https://www.cnblogs.com/TCB-Java/p/6809617.html