博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
网络编程应用:基于TCP协议【实现对象传输】--练习
阅读量:4357 次
发布时间:2019-06-07

本文共 5562 字,大约阅读时间需要 18 分钟。

要求:

基于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

你可能感兴趣的文章
Beanutils
查看>>
FastJson
查看>>
excel4j
查看>>
Thread
查看>>
HtmlEmail
查看>>
ThreadLocal
查看>>
线程池
查看>>
XMAL 中x名称控件的Auttribute
查看>>
java笔记11-内部类
查看>>
给年轻程序员的几句话
查看>>
ionic如何uglify和minify你的js,css,image,png....
查看>>
[LeetCode]Minimum Depth of Binary Tree
查看>>
jboss初体验
查看>>
Python列表、元组练习
查看>>
angular页面刷新
查看>>
Leetcode:7- Reverse Integer
查看>>
C6表单(方成eform)---自定义流程标题格式
查看>>
GridView下DropDownList 的选择方法onselectedindexchanged 实现方法
查看>>
Python之set集合
查看>>
Generic Repository Pattern - Entity Framework, ASP.NET MVC and Unit Testing Triangle
查看>>