Forum Stats

  • 3,752,054 Users
  • 2,250,452 Discussions
  • 7,867,702 Comments

Discussions

How to unbind DatagramSocket

843790
843790 Member Posts: 32,458
edited Jul 24, 2007 8:49PM in Networking
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.

Comments

  • 843790
    843790 Member Posts: 32,458
    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
  • 843790
    843790 Member Posts: 32,458
    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));
  • EJP
    EJP Member Posts: 32,920 Gold Crown
    That is the only solution available, in Java or any other language. Why isn't it ideal?
  • 843790
    843790 Member Posts: 32,458
    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."
  • EJP
    EJP Member Posts: 32,920 Gold Crown
    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.
  • 843790
    843790 Member Posts: 32,458
    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?
  • EJP
    EJP Member Posts: 32,920 Gold Crown
    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.
This discussion has been closed.