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

查看:97
本文介绍了“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:地址已在使用中:connect

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个等等,最多可达200​​0个左右的插座。

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.

任何帮助都将不胜感激!

Any help would be greatly appreciated!

推荐答案

通过打开多个出站套接字,您正在耗尽出站端口的空间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.

BTW 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天全站免登陆