文章标题

来源:互联网 发布:程序员的输入法 编辑:程序博客网 时间:2024/06/18 12:41

1 .需求:
基于TCP协议实现 用户发送一个用户对象给服务器,服务器接受并显示用户信息 ,同时返回给客户端 “数据已收到”
提示 : 对象流 序列化

package com.qf.demo;import java.io.Serializable;public class User implements Serializable{    /**     *      */    private static final long serialVersionUID = 1L;    private String name;    private int age;    transient boolean sex;// 暂态     不能够被序列化    public User(String name, int age, boolean sex) {        super();        this.name = name;        this.age = age;        this.sex = sex;    }    public User() {        super();    }    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 boolean isSex() {        return sex;    }    public void setSex(boolean sex) {        this.sex = sex;    }    @Override    public String toString() {        return "User [name=" + name + ", age=" + age + "]";    }}

服务器:

package com.qf.demo;import java.io.IOException;import java.io.InputStream;import java.io.ObjectInputStream;import java.net.ServerSocket;import java.net.Socket;public class Server {    public static void main(String[] args) {        // 1         ServerSocket serverSocket = null;        Socket socket =     null;        InputStream is = null;        ObjectInputStream ois =null;        try {             serverSocket = new ServerSocket(8899);             socket =   serverSocket.accept();             is = socket.getInputStream();             ois = new ObjectInputStream(is);            User user = (User) ois.readObject();            System.out.println(user);        } catch (IOException e) {            // TODO Auto-generated catch block            e.printStackTrace();        } catch (ClassNotFoundException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }finally {            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();                }            }            if(socket!=null){                try {                    socket.close();                } catch (IOException e) {                    // TODO Auto-generated catch block                    e.printStackTrace();                }            }            if(serverSocket!=null){                try {                    serverSocket.close();                } catch (IOException e) {                    // TODO Auto-generated catch block                    e.printStackTrace();                }            }        }    }}

客户端:

package com.qf.demo;import java.io.IOException;import java.io.ObjectOutputStream;import java.io.OutputStream;import java.net.Socket;import java.net.UnknownHostException;/** *  * @author Administrator * */public class Test {    public static void main(String[] args) {        Socket socket = null;        OutputStream os =null;        ObjectOutputStream oos =null;        //1         try {             socket = new Socket("localhost", 8899);             os = socket.getOutputStream();            //2             User user = new User("zhangsan",5,true);             oos = new ObjectOutputStream(os);            // 3 写            oos.writeObject(user);            oos.flush();        } catch (UnknownHostException e) {            // TODO Auto-generated catch block            e.printStackTrace();        } catch (IOException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }finally {//          客户端发完数据以后一定要关流 关socket            if(os!=null){                try {                    os.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();                }            }            if(socket!=null){                try {                    socket.close();                } catch (IOException e) {                    // TODO Auto-generated catch block                    e.printStackTrace();                }            }        }    }}
原创粉丝点击