如何模拟消息丢失或服务器不发送的网络情况?
我编写了简单的客户端-服务器应用程序并尝试测试它。
我需要编写一些代码来处理回复消息(由服务器发送)丢失并且未到达客户端的情况...
我需要知道如何模拟这种情况。但我不能。
我已经尝试过服务器的 CTRL-C
..但不确定这是否是一个好的场景.. 我从客户那里得到:
send:
0 0 0 5 0 0 1 48 25 112 -55 106 0 0 0 34 60 -72 117 -101 37 28 116 -85 -91 61 55
-126 -50 9 5 64 -87 126 -31 -62 30 13 -90 -72 -124 118 20 88 -80 -9 -36 -33 -38
123
java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(Unknown Source)
at net.Net.readMessageObject(Net.java:36)
at net.Pitcher.run(Pitcher.java:59)
at net.Pitcher.main(Pitcher.java:122)
顺便说一句,这个应用程序是用 Java 编写的。
I've wrote simple Client-Server application and try to test it.
I need to write some code to handle when the reply message (sent by server) is being lost and don't reach the client ...
I need to know how to simulate such situation. but I cannot.
I've tried the CTRL-C
the server .. but not sure if this a good scenario..
I got from the client:
send:
0 0 0 5 0 0 1 48 25 112 -55 106 0 0 0 34 60 -72 117 -101 37 28 116 -85 -91 61 55
-126 -50 9 5 64 -87 126 -31 -62 30 13 -90 -72 -124 118 20 88 -80 -9 -36 -33 -38
123
java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(Unknown Source)
at net.Net.readMessageObject(Net.java:36)
at net.Pitcher.run(Pitcher.java:59)
at net.Pitcher.main(Pitcher.java:122)
BTW, the app written in Java.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您的应用程序相当简单,您可以使用像 netcat 这样的程序连接到它,在某些时候不要发送任何内容,看看会发生什么。
否则,您可以做的是稍微重写您的服务器应用程序,以引入某种随机失败的测试。
If your application is rather simple you can use a programm like netcat to connect to it, dont send anything at some point and see what happens.
Otherwise what you could do is rewrite your server app a little bit to introduce some sort of random fails for testing.
创建模拟网络对象,以便您可以模拟各种情况,在每种情况下操作模拟网络对象,以便您可以测试应用程序是否与其适当交互。
Create a mock network object, so you can simulate various situations, in each case manipulating the mock network object so that you can test that your application interacts with it appropriately.