socket互传对象以及IO流的顺序问题

UserInfo.java

package com.company.s6;

import java.io.Serializable;

public class UserInfo implements Serializable {
    private long id;
    private String username;
    private String password;

    public UserInfo() {
    }

    public UserInfo(long id, String username, String password) {
        this.id = id;
        this.username = username;
        this.password = password;
    }

    public long getId() {
        return id;
    }

    public void setId(long 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;
    }
}

Server.java

package com.company.s6;

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

public class Server {
    public static void main(String[] args) throws Exception{
        ServerSocket serverSocket=new ServerSocket(8888);
        Socket socket=serverSocket.accept();
        InputStream inputStream=socket.getInputStream();
        OutputStream outputStream=socket.getOutputStream();
        ObjectOutputStream objectOutputStream=new ObjectOutputStream(outputStream);
        ObjectInputStream objectInputStream=new ObjectInputStream(inputStream);

        for(int i=0;i<5;i++){
            UserInfo userInfo=(UserInfo) objectInputStream.readObject();
            System.out.println("在服务端打印 "+(i+1)+":"+userInfo.getId()+" "+userInfo.getUsername()+" "+userInfo.getPassword());
            UserInfo newUserInfo=new UserInfo();
            newUserInfo.setId(i+1);
            newUserInfo.setUsername("servername"+(i+1));
            newUserInfo.setPassword("serverpassord"+(i+1));
            objectOutputStream.writeObject(newUserInfo);
        }
        objectOutputStream.close();
        objectInputStream.close();

        outputStream.close();
        inputStream.close();

        socket.close();
        serverSocket.close();
    }
}

Client.java

package com.company.s6;

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

public class Client {
    public static void main(String[] args) throws Exception{
        Socket socket=new Socket("localhost",8888);
        InputStream inputStream=socket.getInputStream();
        OutputStream outputStream=socket.getOutputStream();
        ObjectInputStream objectInputStream=new ObjectInputStream(inputStream);
        ObjectOutputStream objectOutputStream=new ObjectOutputStream(outputStream);
        for(int i=0;i<5;i++){
            UserInfo newUserinfo=new UserInfo();
            newUserinfo.setId(i+1);
            newUserinfo.setUsername("clientUserName:"+(i+1));
            newUserinfo.setPassword("clientPassword:"+(i+1));

            objectOutputStream.writeObject(newUserinfo);

            UserInfo userInfo=(UserInfo)objectInputStream.readObject();
            System.out.println("在客户端打印:"+(i+1)+":"+userInfo.getId()+" "+userInfo.getUsername()+" "+userInfo.getPassword());
        }
        objectInputStream.close();
        objectOutputStream.close();

        outputStream.close();
        inputStream.close();

        socket.close();
    }
}

 

ps:如果服务端先获取objectInputStream对象,然后获取objectOutPutStream对象;

如果客户端也是同样的顺序获取对象,这样的话在运行时会在服务端的程序代码:

ObjectInputStream objectInputStream=new ObjectInputStream(inputStream);

出现阻塞的现象。

正确的写法应该是:

1:服务端先获得ObjectInputStream对象,客户端就要先获得ObjectOutPutStream对象;

2:服务端先获得ObjectOutPutStream对象,客户端就要先获得ObjectInPutStream对象;

posted @ 2019-06-30 22:00  思思博士  阅读(551)  评论(0编辑  收藏  举报