简体   繁体   English

在OutputStream上阻止写操作

[英]Blocking write operation on OutputStream

Communication between two servers: 两台服务器之间的通信:

Server 1 sends a request to Server 2 Server 2 writes response on the stream by using below api 服务器1向服务器2发送请求服务器2使用以下api在流上写入响应

ObjectOutputStream oos = new ObjectOutputStream(servResp.getOutputStream());
oos.writeObject(dataObj)

When server 1 receives the data as 当服务器1收到数据时

InputStream res = connection.getInputStream();
if(res.available() > 0) {
    ObjectInputStream ois = new ObjectInputStream(res);
    Object object = ois.readObject();
    dataObj = (DataObject)object;
}

Here, res.isavailable() returns zero bytes, It will be resolved by using blocking read on Server 2, But Is there any way to handle it on Server 1? 在这里,res.isavailable()返回零字节,这将通过在服务器2上使用阻塞读取来解决,但是有没有办法在服务器1上进行处理?

Any implementation of available() is free to return zero, and there is nothing in the Javadoc about it returning the number of bytes in the stream: on the contrary, there is a specific denial that it does any such thing. 任何available()实现都available()自由地返回零,并且Javadoc中没有任何关于它返回流中字节数的信息:相反,有一个特定的否认它可以执行任何此类操作。 Your receiver should just block in readObject() . 您的接收器应该只是阻塞readObject() There is nothing that the sender can do to help other than send. 除了发送外,发送者无法做任何帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM