开发者

Java: How to clear socket bindings

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 t开发者_如何学Goo 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


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.


Don't fiddle with SO_LINGER, it just adds insecurity. The real question is why are you binding to a local port at all?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜