美文网首页
利用socket传输对象

利用socket传输对象

作者: 幽澜先生 | 来源:发表于2019-01-25 15:57 被阅读0次

    一、创建用户类User;

    package socket;

    import java.io.Serializable;

    /**

    * 用户类

    */

    public class User implements Serializable {

    /**

    *

    */

    private static final long serialVersionUID = 7965470946723147485L;

    private String id;

    private String username;

    private String password;

    public User() {

    }

    public User(String id, String username, String password) {

    this.id = id;

    this.username = username;

    this.password = password;

    }

    public String getId() {

    return id;

    }

    public void setId(String id) {

    this.id = id;

    }

    public String getUsername() {

    return username;

    }

    public void setUsername(String username) {

    this.username = username;

    }

    public String getPassword() {

    return password;

    }

    public void setPassword(String password) {

    this.password = password;

    }

    }

    二、创建服务端ServerSocket;

    package socket;

    import java.io.IOException;

    import java.io.InputStream;

    import java.io.InputStreamReader;

    import java.io.ObjectInputStream;

    import java.io.OutputStream;

    import java.net.ServerSocket;

    import java.net.Socket;

    import java.net.UnknownHostException;

    /**

    * 基于TCP协议的Socket通信,传输对象

    * 服务器端

    */

    public class Server {

    static ServerSocket serverSocket ;

    static Socket socket;

    static InputStream is;

    static StringBuffer sb;

    static InputStreamReader isr;

    static OutputStream os;

    public static void main(String[] args) {

    try {

    //1、创建一个服务器端Socket,即ServerSocket,指定绑定的端口,并监听此端口

    serverSocket = new ServerSocket(9999);

    //2、调用accept()方法开始监听,等待客户端的连接

    socket = serverSocket.accept();

    //3、获取输入流,并读取客户端信息

    is = socket.getInputStream();

    //4、转化为对象输入流

    ObjectInputStream ois = new ObjectInputStream(is);

    User user = (User) ois.readObject();

    //5、返回消息给客户端

    os = socket.getOutputStream();

    if(user.getUsername().equals("admin")){

    System.out.println("我是服务器,客户端当前登录用户是:"+user.getUsername());

    //返回登录成功的信息给客户端

    os.write( ("欢迎你," + user.getUsername()).getBytes() );

    os.flush();

    }else{

    //返回登录失败的信息给客户端

    System.out.println("登录失败");

    os.write( (user.getUsername() + "登录失败").getBytes() );

    os.flush();

    }

    //关闭输入流

    //socket.shutdownInput();

    } catch (UnknownHostException e) {

    e.printStackTrace();

    } catch (IOException e) {

    e.printStackTrace();

    } catch (ClassNotFoundException e) {

    e.printStackTrace();

    }finally{

    try {

    if( os != null){

    os.close();

    }

    if(isr != null){

    isr.close();

    }

    if(is != null){

    is.close();

    }

    if(socket != null){

    socket.close();

    }

    if(serverSocket != null){

    serverSocket.close();

    }

    } catch (IOException e) {

    e.printStackTrace();

    }

    }

    }

    }

    三、创建客户端Socket;

    package socket;

    import java.io.BufferedReader;

    import java.io.IOException;

    import java.io.InputStream;

    import java.io.InputStreamReader;

    import java.io.ObjectOutputStream;

    import java.io.OutputStream;

    import java.net.Socket;

    import java.net.UnknownHostException;

    public class Client {

    static Socket socket;

    static OutputStream os;

    static InputStream is ;

    static InputStreamReader isr;

    static BufferedReader br;

    public static void main(String[] args) {

    try {

    //1、创建客户端Socket,指定服务器地址和端口

    socket = new Socket("127.0.0.1", 9999);

    //2、获取输出流,向服务器端发送信息

    os =socket.getOutputStream();

    ObjectOutputStream oos = new ObjectOutputStream(os);

    oos.writeObject(new User("1","admin","admin"));

    oos.flush();

    //3、获取服务器端返回的信息

    is = socket.getInputStream();

    isr = new InputStreamReader(is);

    br = new BufferedReader(isr);

    String data = null;

    StringBuffer sb = new StringBuffer();

    while( (data = br.readLine()) != null ){

    sb.append(data);

    }

    System.out.println("服务器端返回的信息是:" + sb);

    } catch (UnknownHostException e) {

    e.printStackTrace();

    } catch (IOException e) {

    e.printStackTrace();

    }finally{

    try {

    if(br != null)

    br.close();

    if(isr != null)

    isr.close();

    if(os != null)

    os.close();

    if(is != null)

    is.close();

    if(socket != null)

    socket.close();

    } catch (IOException e) {

    e.printStackTrace();

    }

    }

    }

    }

    四、测试结果;

    客户端:服务器端返回的信息是:欢迎你,admin

    服务端:我是服务器,客户端当前登录用户是:admin

    五、总结

    1、对于同一个socket,如果关闭了输出流,则与该输出流关联的socket也会被关闭,所以一般不用关闭流,直接关闭socket即可;

    2、使用TCP协议传输对象时,对象必须序列化;

    相关文章

      网友评论

          本文标题:利用socket传输对象

          本文链接:https://www.haomeiwen.com/subject/qqxvjqtx.html