简体   繁体   English

有没有办法模拟 Java 中的套接字故障?

[英]Is there a way to simulate socket failure in Java?

I was wondering if there was a way to simulate socket failure in TCP Java, without actually shutting down a socket/force-shutting down that socket side.我想知道是否有一种方法可以模拟 TCP Java 中的套接字故障,而无需实际关闭套接字/强制关闭该套接字端。

This would be using TCP, and the program would be part of a larger app, where we attempt to demonstrate some failsafe ability (ie, after we shutdown the client, we'd recover state somehow.这将使用 TCP,该程序将成为更大应用程序的一部分,我们试图在其中展示一些故障保护能力(即,在我们关闭客户端后,我们将以某种方式恢复 state。

For example, if we have a simple Echo Server/Client pair;例如,如果我们有一个简单的 Echo 服务器/客户端对; is there a way to do this?有没有办法做到这一点?

I know that lots of time past since 2015 - maybe I'll help someone else我知道自 2015 年以来已经过去了很多时间 - 也许我会帮助别人

if your process waiting on the accept() You can set a timeout on the socket如果您的进程等待 accept() 您可以在套接字上设置超时

socket.setSoTimeout(<timeout in milliseconds>);

This would trigger SocketTimeoutException when the timeout would expire这将在超时到期时触发 SocketTimeoutException

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

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