Tcp实现对象传输

服务器处理用户连接线程类:

public class ServerThread extends Thread {
    private Socket socket=null;
    public ServerThread(Socket socket){
        this.socket=socket;
    }
    @Override
    public void run() {
        //3、获取输入流,并读取客户端信息
        InputStream is= null;
        ObjectInputStream ois=null;
        ObjectOutputStream oos=null;
        OutputStream os= null;
        try {
                is = socket.getInputStream();
                ois=new ObjectInputStream(is);
                Student stu=(Student)ois.readObject();
                //ois.close();//不能加,可能会造成socket过早关闭
                System.out.println(stu);
                //socket.shutdownInput();//关闭输入流
                //4、获取输出流,响应客户端的请求
                os = socket.getOutputStream();
                oos=new ObjectOutputStream(os);
                Student student=new Student(1,"服务器","中",20);
                oos.writeObject(student);
                oos.flush();
                oos.close();
                TimeUnit.MILLISECONDS.sleep(100);
            //socket.shutdownOutput();//关闭输出流
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }catch (InterruptedException e) {
            e.printStackTrace();
        }finally {
            //5、关闭资源
            try {
                if(is!=null)
                    is.close();
                if(os!=null)
                    os.close();
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}
服务端:

public class Server {
    public static void main(String[] args){
        try {
            //1、创建一个服务器端Socket,即ServerSocket,指定绑定的端口
            ServerSocket serverSocket=new ServerSocket(8989);
            int num=0;//统计客户端的数量
            //2、调用accept()方法开始坚挺,等待客户端的连接
            System.out.println("***服务器即将启动,等待客户端的连接***");
            Socket socket=null;
            while (true){
                socket=serverSocket.accept();
                ServerThread serverThread=new ServerThread(socket);
                serverThread.start();
                num++;
                System.out.println("当前客户端的数量:"+num);
                InetAddress inetAddress=socket.getInetAddress();
                System.out.println("客户端的IP地址为:"+inetAddress.getHostAddress());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
客户端:

public class Client {
    public static void main(String[] args){
        try {
                //1.创建客户端Socket,指定服务器地址和端口
                Socket socket=new Socket("localhost",8989);
                //2.获取输入流,像服务器发送信息
                OutputStream os=socket.getOutputStream();//字节输入流
                ObjectOutputStream oos=new ObjectOutputStream(os);
                Student student=new Student(2,"张三","男",12);
                oos.writeObject(student);
                oos.flush();
                //oos.close();
                //socket.shutdownOutput();//关闭输出流
                //3.获取输入流,并读取服务器端的响应信息
                InputStream is=socket.getInputStream();
                if(is!=null) {
                    ObjectInputStream ois = new ObjectInputStream(is);
                    student = (Student) ois.readObject();
                    System.out.println(student);
                    ois.close();
                }
                //socket.shutdownInput();//关闭输入流
                //4/关闭资源
                oos.close();
                is.close();
                os.close();
                socket.close();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }catch (IOException e) {
            e.printStackTrace();
        }
    }
}
***服务器即将启动,等待客户端的连接***
当前客户端的数量:1
客户端的IP地址为:127.0.0.1
[2,张三,男,12]
当前客户端的数量:2
客户端的IP地址为:127.0.0.1
[2,张三,男,12]
当前客户端的数量:3
客户端的IP地址为:127.0.0.1
[2,张三,男,12]

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值