不能实例化ObjectInputStream来读取用户的输入

时间:2017-04-03 18:39:24

标签: java sockets objectinputstream objectoutputstream

我无法添加ObjectInputStream来读取用户的输入,它始终会阻止此点。如果我删除了应该从用户读取输入的服务器中的ObjectInputStream,然后发送硬编码的字符串,则此代码可以正常工作。幕后发生了什么?我知道当创建ObjectOutputStream时,它会发送一个标头,当创建ObjectInputStream时,它会读取该标头。在尝试实例化System之前,是否需要在oOISUser中刷新内容?

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class Server {
    public Server() {
        ServerSocket oSS = null;
        Socket oS = null;
        ObjectOutputStream oOOS = null; // to write to socket
        ObjectInputStream oOIS = null; // to read from socket
        ObjectInputStream oOISUser = null; // to read input from user

        try {
            oSS = new ServerSocket(1025);
            oS = oSS.accept();
            oOOS = new ObjectOutputStream(oS.getOutputStream());
            oOIS = new ObjectInputStream(oS.getInputStream());
            oOISUser = new ObjectInputStream(System.in);`// doesn't get past this

            String sToSend = (String) oOISUser.readObject();
            System.out.println("server says: " + sToSend);
            oOOS.writeObject(sToSend);
            oOOS.flush();
            System.out.println("server receives: " + (String) oOIS.readObject());
        } catch (IOException e) {
            System.out.println(e.getMessage());
        } catch (ClassNotFoundException e) {
            System.out.println(e.getMessage());
        } finally {
            try {
                if (oSS != null) oSS.close();
                if (oS != null) oS.close();
                if (oOOS != null) oOOS.close();
                if (oOIS != null) oOIS.close();
                if (oOISUser != null) oOISUser.close();
            } catch (IOException e) {
                System.out.println(e.getMessage());
            }
        }
    }

    public static void main(String[] args) {
        Server s = new Server();
    }
}

这是客户的代码:

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;

public class Client {
    public Client() {
        Socket oS = null;
        ObjectOutputStream oOOS = null;
        ObjectInputStream oOIS = null;

        try {
            oS = new Socket("127.0.0.1", 1025);
            oOOS = new ObjectOutputStream(oS.getOutputStream());
            oOIS = new ObjectInputStream(oS.getInputStream());

            System.out.println("client receives: " + (String) oOIS.readObject());
            String sToSend = "hello from client";
            System.out.println("client says: " + sToSend);
            oOOS.writeObject(sToSend);
            oOOS.flush();

        } catch (IOException e) {
            System.out.println(e.getMessage());
        } catch (ClassNotFoundException e) {
            System.out.println(e.getMessage());
        } finally {
            try {
                if (oS != null) oS.close();
                if (oOOS != null) oOOS.close();
                if (oOIS != null) oOIS.close();
            } catch (IOException e) {
                System.out.println(e.getMessage());
            }
        }
    }
    public static void main(String[] args) {
        Client c = new Client();
    }

}

1 个答案:

答案 0 :(得分:1)

Class User extends CI_Model
{
    function populateUsers()
    {
        $this->db->select('username, accessLevel, fullName');
        $this->db->from('users');

        $userquery = $this->db->get();

        if($userquery->num_rows() >= 1)
        {
            return $userquery->result();
        }
        else
        {
            return false;
        }

    }

}

你自己在问题中说过:

  

创建ObjectInputStream时,它会读取该标题

因此,您有效地等待用户在控制台中输入ObjectInputStream标头。这有一个非常小的机会发生(除非文件通过管道输送到System.in)。从System.in读取序列化Java对象几乎没有意义。用户无法在控制台中键入有效的序列化Java对象。不过,他/她可以输入文字。所以使用Reader或扫描仪。