ObjectOutputStream永远挂起 [英] ObjectOutputStream hanging forever

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

问题描述

我有一个使用SSLSocket连接到服务器的客户端。接下来,我尝试使用 ObjectOutputStream创建一个OOS oos = new ObjectOutputStream(sslsocket.getOutputStream());



如果一切都在服务器端运行良好,这很好。但是,我想在客户端尝试创建ObjectOutputStream,但如果在60秒内没有发生,请记录错误并继续处理。我没有看到任何超时选项。有关如何做到这一点的任何例子?

  SSLSocket sslsocket; 
try {
System.setProperty(javax.net.ssl.trustStore,< myKeystore>);
System.setProperty(javax.net.ssl.trustStorePassword,< myPW>);
SSLSocketFactory sslsocketfactory =(SSLSocketFactory)SSLSocketFactory.getDefault();
sslsocket =(SSLSocket)sslsocketfactory.createSocket(InetAddress.getLocalHost(),< myPort>);

} catch(Throwable e){
logger.logError(e.getMessage());
返回null;
}

//这是它永远挂起的地方
ObjectOutputStream oos = new ObjectOutputStream(sslsocket.getOutputStream());
oos.flush(); //永远不会到这里
oos.writeObject(orders);

ObjectInputStream ois = new ObjectInputStream(sslsocket.getInputStream());


解决方案

您必须在ObjectInputStream之前创建ObjectOutputStream。 / p>

编辑:请参阅下面的评论。真正的问题是慢速SSL握手,它发生在套接字上的第一个I / O中,并且在此期间执行读取以及写入。因此解决方案是设置读取超时。


I have a client that connects to a server using SSLSocket. Next, I try to create an OOS with ObjectOutputStream oos = new ObjectOutputStream(sslsocket.getOutputStream());

If everything is working well on the server side, this is fine. However, I would like to put something in place on my client side that tries to create the ObjectOutputStream, but if it hasn't happened in 60 seconds, log the error and continue processing. I don't see any timeout options for this. Any examples of how this could be done?

     SSLSocket sslsocket;
     try {
            System.setProperty("javax.net.ssl.trustStore", <myKeystore>);
            System.setProperty("javax.net.ssl.trustStorePassword", <myPW>);
            SSLSocketFactory sslsocketfactory = (SSLSocketFactory) SSLSocketFactory.getDefault();
            sslsocket = (SSLSocket) sslsocketfactory.createSocket(InetAddress.getLocalHost(), <myPort>);

     } catch (Throwable e) {
            logger.logError(e.getMessage());
            return null;
     }

      // This is where it hangs forever
      ObjectOutputStream oos = new ObjectOutputStream(sslsocket.getOutputStream());
      oos.flush(); // never gets here
      oos.writeObject(orders);

      ObjectInputStream ois = new ObjectInputStream(sslsocket.getInputStream());

解决方案

You have to create the ObjectOutputStream before the ObjectInputStream.

EDIT: See comments below. The real problem was a slow SSL handshake, which happens in the first I/O on the socket, and during which reads are performed as well as writes. So the solution was to set a read timeout.

这篇关于ObjectOutputStream永远挂起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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