Java:如何清除套接字绑定
我的 Java SIP 客户端中的套接字遇到一些问题。当我绑定到地址和端口时,如果出现问题,我必须尝试重新连接,通常是在我停止并重新启动该过程之后。问题是端口被绑定,我被迫增加本地端口。
在绑定到目标端口之前,如何删除对它的绑定?
如果这是不可能的,那么我如何在进程结束之前捕获该进程,以便我可以找到套接字绑定并手动关闭它?
@Jason - Jason,但在这种情况下,我正在编写客户端并且无法访问服务器,我指的端口位于客户端上并且是本地的。有没有办法在尝试连接之前刷新端口绑定?如果没有,有没有办法捕获进程中断,就像在perl中有一种方法可以捕获“die”信号并进行一些后处理,Java有这个吗?如果是这样我可以在套接字连接上调用 close()
I am having a few issues with sockets within my Java SIP client. When I bind to an address and port, if something goes wrong I have to attempt to reconnect, usually after I've stopped and restarted the process. Problem with that is then the port is bound and I am forced to increment the local port.
How can I remove the binding to the port I am targeting before binding to it?
If that isnt possible, then how can I trap the process just before it ends so that I can locate the socket binding and close it manually?
@Jason - Jason, but in this case I am writing the Client and have no access to the server, the port I am referring to is on the client and is local. Is there a way to flush the port binding before attempting to connect? If not is there a way to trap the process interrupt, as in perl there is a way to trap a 'die' signal and do some post processing, does Java have this? If so I could call close() on the socket connection
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
根据我的经验,此类问题十有八九的答案是“查找 SO_LINGER”。
如果您拔掉客户端的插头(字面意思),服务器乐观地希望它会回来收集您已经在该套接字上发送的数据。因此它会保留该数据和端口,直到缓冲区清除。
通常在服务器上,由于您刚刚发现的 DOS 攻击(有意或无意),您可能会怀着极大的偏见杀死这些缓冲区。
In my experience 9 times out of 10, the answer to this class of problem is, "Look up SO_LINGER".
If you pull the plug (literally) on a client, the server optimistically hopes it will come back to collect the data you already sent on that socket. So it holds onto that data, and the port, until the buffers clear.
Usually on the server you want to kill these buffers with extreme prejudice, due to the sort of DOS attack (intentional or accidental) you just discovered.
不要摆弄 SO_LINGER,它只会增加不安全性。真正的问题是你为什么要绑定到本地端口?
Don't fiddle with SO_LINGER, it just adds insecurity. The real question is why are you binding to a local port at all?
好的 - 我通过在线阅读本教程找到了一种捕获 Java 信号的方法 - http ://www.ibm.com/developerworks/java/library/i-signalhandling/
这样就可以捕获 die 信号并关闭连接。
Ok - I found a way to trap Java signals by reading this tutorial online - http://www.ibm.com/developerworks/java/library/i-signalhandling/
This way one can trap the die signal and close the connection.