“java.net.BindException:地址已被使用"尝试为负载测试快速创建和销毁 Socket 时 [英] "java.net.BindException: Address already in use" when trying to do rapid Socket creation and destruction for load testing

查看:20
本文介绍了“java.net.BindException:地址已被使用"尝试为负载测试快速创建和销毁 Socket 时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过打开到服务器的大量套接字连接、验证、关闭连接,然后重复来对 Java 服务器进行负载测试.我的应用运行了一段时间,但最终我得到了:

I'm trying to load test a Java server by opening a large number of socket connections to the server, authenticating, closing the connection, then repeating. My app runs great for awhile but eventually I get:

java.net.BindException: 地址已被使用:连接

java.net.BindException: Address already in use: connect

根据我阅读的文档,原因是关闭的套接字在调用 close() 后仍然占用分配给它们的本地地址一段时间.这取决于操作系统,但可能需要几分钟的时间.我尝试在套接字上调用 setReuseAddress(true) ,希望它的地址在 close() 被调用后可以立即重用.不幸的是,情况似乎并非如此.

According to documentation I read, the reason for this is that closed sockets still occupy the local address assigned to them for a period of time after close() was called. This is OS dependent but can be on the order of minutes. I tried calling setReuseAddress(true) on the socket with the hopes that its address would be reusable immediately after close() was called. Unfortunately this doesn't seem to be the case.

我创建套接字的代码是:

My code for socket creation is:

Socket socket = new Socket();
socket.setReuseAddress(true);
socket.connect(new InetSocketAddress(m_host, m_port));

但我仍然收到此错误:

java.net.BindException:地址已在使用:稍后连接.

java.net.BindException: Address already in use: connect after awhile.

还有其他方法可以完成我正在尝试做的事情吗?我想例如:打开 100 个套接字,将它们全部关闭,打开 200 个套接字,将它们全部关闭,打开 300 个等等,最多 2000 个左右的套接字.

Is there any other way to accomplish what I'm trying to do? I would like to for instance: open 100 sockets, close them all, open 200 sockets, close them all, open 300, etc. up to a max of 2000 or so sockets.

任何帮助将不胜感激!

推荐答案

在两分钟的 TIME_WAIT 时间内打开那么多出站套接字,正在耗尽出站端口的空间.您应该问自己的第一个问题是,这是否代表了实际的负载测试?真正的客户真的会这样做吗?如果没有,您只需修改您的测试方法.

You are exhausing the space of outbound ports by opening that many outbound sockets within the TIME_WAIT period of two minutes. The first question you should ask yourself is does this represent a realistic load test at all? Is a real client really going to do that? If not, you just need to revise your testing methodology.

顺便说一句,SO_LINGER 是 应用程序 在 close() 期间等待刷新数据的秒数.它通常为零.如果这是发出关闭的结束,则端口无论如何都会在 TIME_WAIT 间隔内停留.这不是一回事.可以滥用 SO_LINGER 选项来修补问题.然而,这也会导致对等端的异常行为,这同样不是测试的目的.

BTW SO_LINGER is the number of seconds the application will wait during close() for data to be flushed. It is normally zero. The port will hang around for the TIME_WAIT interval anyway if this is the end that issued the close. This is not the same thing. It is possible to abuse the SO_LINGER option to patch the problem. However that will also cause exceptional behaviour at the peer and again this is not the purpose of a test.

这篇关于“java.net.BindException:地址已被使用"尝试为负载测试快速创建和销毁 Socket 时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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