新的ObjectInputStream()块 [英] new ObjectInputStream() blocks

查看:107
本文介绍了新的ObjectInputStream()块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public class SerProg {


    static ServerSocket ser=null;
    static Socket cli=null;
    static ObjectInputStream ins=null;
    static ObjectOutputStream outs=null;


    public static void main(String[] args) {

        try {

            ser=new ServerSocket(9000,10);
            cli=ser.accept();

            System.out.println("Connected to :"+cli.getInetAddress().getHostAddress()+" At Port :"+cli.getLocalPort());

            ins=new ObjectInputStream(cli.getInputStream());
            outs=new ObjectOutputStream(cli.getOutputStream());

            String str=(String)ins.readObject();


        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


    }

}

和客户

public class SerProg {

    /**
     * @param args
     */
    static ServerSocket ser=null;
    static Socket cli=null;
    static ObjectInputStream ins=null;
    static ObjectOutputStream outs=null;


    public static void main(String[] args) {
        // TODO Auto-generated method stub

        try {

            ser=new ServerSocket(9000,10);
            cli=ser.accept();

            System.out.println("Connected to :"+cli.getInetAddress().getHostAddress()+" At Port :"+cli.getLocalPort());

            ins=new ObjectInputStream(cli.getInputStream());
            outs=new ObjectOutputStream(cli.getOutputStream());

            String str=(String)ins.readObject();


        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


    }

}

连接已成功建立,但在服务器代码行中

The Connection is established successfully but in the Server Code Line

ins=new ObjectInputStream(cli.getInputStream());

代码停止并且不继续,可能是什么问题?

the code halts and does not proceed,what might be the problem ??

推荐答案

您需要在 ObjectInputStream ObjectOutputStream c $ c>在连接的两侧(!)。当创建 ObjectInputStream 时,它会尝试从 InputStream 中读取对象流标题。因此,如果尚未创建另一侧的 ObjectOutputStream ,则没有要读取的对象流标头,并且它将无限期地阻塞。

You need to create the ObjectOutputStream before the ObjectInputStream at both sides of the connection(!). When the ObjectInputStream is created, it tries to read the object stream header from the InputStream. So if the ObjectOutputStream on the other side hasn't been created yet there is no object stream header to read, and it will block indefinitely.

或者用不同的方式表达:如果双方首先构造 ObjectInputStream ,两者都将阻止尝试读取对象流头,在创建 ObjectOutputStream 之前不会写入(在该行的另一侧);这将永远不会发生,因为双方都在 ObjectInputStream 的构造函数中被阻止。

Or phrased differently: If both sides first construct the ObjectInputStream, both will block trying to read the object stream header, which won't be written until the ObjectOutputStream has been created (on the other side of the line); which will never happen because both sides are blocked in the constructor of ObjectInputStream.

这可以从Javadoc中推断出来 ObjectInputStream(InputStream in)

This can be inferred from the Javadoc of ObjectInputStream(InputStream in):


从中读取序列化流标题流和验证。此构造函数将阻塞,直到相应的ObjectOutputStream已写入并刷新标头。

A serialization stream header is read from the stream and verified. This constructor will block until the corresponding ObjectOutputStream has written and flushed the header.

Esmond Pitt的 Java基础网络第3.6.2节对此进行了描述。

This is also described in section 3.6.2 of Fundamental networking in Java by Esmond Pitt.

这篇关于新的ObjectInputStream()块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆