Java 无法让 ObjectInputStream 工作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9826267/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 08:11:54  来源:igfitidea点击:

Cannot get ObjectInputStream to work

javasocketsstreamnetwork-programming

提问by George Phillips

I am currently trying to get my server to create a connection with a client. I have created a thread for each connection but the server is currently not creating the Input Stream. I have tested this by printing out numbers but only 1 and 2 get printed out. Im sure this is just a small problem that im missing.

我目前正在尝试让我的服务器与客户端建立连接。我为每个连接创建了一个线程,但服务器当前没有创建输入流。我已经通过打印数字进行了测试,但只有 1 和 2 被打印出来。我确定这只是我遗漏的一个小问题。

public class ObjectHandler implements Runnable{
    Socket sock;
    ObjectInputStream ois;
    ObjectOutputStream oops;
    InputStream is;


    public ObjectHandler(Socket clientSocket) throws IOException {
        System.out.println("1");
        sock = clientSocket;

        is = sock.getInputStream();
        System.out.println("2");
        ois = new ObjectInputStream(new BufferedInputStream(is));
        System.out.println("3");

        OutputStream os = sock.getOutputStream();

        oops = new ObjectOutputStream(new BufferedOutputStream(os));
        outputSockets.add(oops);    

    }

I have now removed the throws IOException and surrounded the reader with a try catch. After the client has crashed it now prints this error:

我现在已经删除了 throws IOException 并用 try catch 包围了读者。客户端崩溃后,它现在会打印此错误:

at java.io.ObjectInputStream$PeekInputStream.readFully(ObjectInputStream.java:2280)
at java.io.ObjectInputStream$BlockDataInputStream.readShort(ObjectInputStream.java:2749)
at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:779)
at java.io.ObjectInputStream.<init>(ObjectInputStream.java:279)
at ThreadPool$ObjectHandler.<init>(ThreadPool.java:95)
at ThreadPool.addThread(ThreadPool.java:31)
at ObjectServerTest.go(ObjectServerTest.java:93)
at ObjectServerTest.main(ObjectServerTest.java:124)

采纳答案by jtahlborn

When constructing Object streams over sockets, you alwaysneed to construct the output stream firstand flushit before creating the input stream (due to how the streams are implemented).

当构建对象流通过套接字,你总是需要构建输出流第一,并刷新它创建输入流(由于流是如何实现的)前。

回答by AlexR

Remove BufferedInputStream. You do not need it. It is waiting until 4K bytes are read.

删除BufferedInputStream。你不需要它。它正在等待直到读取 4K 字节。

Edit: remove BufferedOutputStreamtoo. And make sure that you are flushing the output stream on client side.

编辑:也删除BufferedOutputStream。并确保您正在刷新客户端的输出流。

回答by tsatiz

This works for me...

这对我有用...

ObjectHandler:

对象处理程序:

public ObjectHandler(Socket clientSocket) throws IOException {
    System.out.println("1");
    sock = clientSocket;
}


@Override
public void run() {
    try {
        is = sock.getInputStream();
        System.out.println("2");
        ois = new ObjectInputStream(new BufferedInputStream(is));
        System.out.println("3"+ois.readFloat());

        OutputStream os = sock.getOutputStream();

        oops = new ObjectOutputStream(new BufferedOutputStream(os)); 
    } catch (IOException e) {
        e.printStackTrace();
    }
}

Client:

客户:

public static void main(String[] args) throws UnknownHostException, IOException {
    Socket client = new Socket(InetAddress.getByName("localhost"), 8888);
    OutputStream os = client.getOutputStream();
    ObjectOutputStream oos = new ObjectOutputStream(os);
    oos.writeFloat(new Float(10.10));
    oos.flush();
}