How to unbind DatagramSocket

Hi,

I have a problem with DatagramSocket. Though I close() it and disconnect(), I cannot reuse the port allocated by previous socket in another socket:

Caused by: java.net.BindException: Address already in use

at java.net.PlainDatagramSocketImpl.bind0(Native Method)

at java.net.PlainDatagramSocketImpl.bind(PlainDatagramSocketImpl.java:82)

at java.net.DatagramSocket.bind(DatagramSocket.java:368)

at java.net.DatagramSocket.<init>(DatagramSocket.java:210)

at java.net.DatagramSocket.<init>(DatagramSocket.java:261)

...

What is the correct way to unbind the socket?

Thanks for any help.

[659 byte] By [fordfrog2a] at [2007-11-27 11:17:08]
# 1

i dont think thereis a way 2 unbind

every application gets a port assigned and then u cant use it for oher application

even i am new 2 java so if someone has a different opinion please point out

noobs_will_rulea at 2007-7-29 14:23:09 > top of Java-index,Core,Core APIs...
# 2

I want to use that port AFTER I close it previously. btw, I found a way to make it work though the solution is not ideal.

DatagramSocket socket = new DatagramSocket(null);

socket.setReuseAddress(true);

socket.bind(new InetSocketAddress(localAddress, localPort));

fordfrog2a at 2007-7-29 14:23:09 > top of Java-index,Core,Core APIs...
# 3

That is the only solution available, in Java or any other language. Why isn't it ideal?

ejpa at 2007-7-29 14:23:09 > top of Java-index,Core,Core APIs...
# 4

The Unix Socket FAQ suggests some non-idealness, although the associated hazard (wandering duplicates) seems to have a low probability of surfacing.

http://www.faqs.org/faqs/unix-faq/socket/

sections "4.5. What exactly does SO_REUSEADDR do?"

and "2.7. Please explain the TIME_WAIT state."

sjasjaa at 2007-7-29 14:23:09 > top of Java-index,Core,Core APIs...
# 5

Not really, it just quotes somebody who thinks TIME_WAIT isn't necessary, which it is.

I'm curious as to what an 'ideal' solution to the OP's problem would look like.

ejpa at 2007-7-29 14:23:09 > top of Java-index,Core,Core APIs...
# 6

> Not really, it just quotes somebody who thinks

> TIME_WAIT isn't necessary, which it is.

It does say "The TIME_WAIT state is there for a reason; it's your friend and it's there to help you :-)"... Somewhere in there a bit that says the opposite?

> I'm curious as to what an 'ideal' solution to the

> OP's problem would look like.

Perhaps there isn't one? My experience with the common suggestion of "just put in setReuseAddress(true), dude" is with people trying to do something weird with TCP. Oftentimes fixed by not closing and re-opening a ServerSocket in a tight loop. What is the problematic DatagramSocket scenario in the first place? The OP wants to close(); disconnect(); bind(); -- what's the intent there?

sjasjaa at 2007-7-29 14:23:09 > top of Java-index,Core,Core APIs...
# 7

Well, te TIME_WAIT state is only there for TCP, but it is essential to preserve the integrity properties of TCP. Otherwise you could receive data twice, or data that isn't part of the connection, and TCP is defined not to do that.

Anyway TIME_WAIT has nothing to do with UDP.

Still waiting to hear from the OP about this.

ejpa at 2007-7-29 14:23:09 > top of Java-index,Core,Core APIs...