Java Bind Exception

前端 未结 3 393
长情又很酷
长情又很酷 2021-01-22 04:11

What would cause a TCP socket to throw \"java.net.BindException: Address already in use\" even when reuse address is set to true? This only occurs if the application is quickly

3条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-22 04:47

    This kinda explains it:

    http://www.beej.us/guide/bgnet/output/html/singlepage/bgnet.html#bind

    Sometimes, you might notice, you try to rerun a server and bind() fails, claiming "Address already in use." What does that mean? Well, a little bit of a socket that was connected is still hanging around in the kernel, and it's hogging the port. You can either wait for it to clear (a minute or so), or add code to your program allowing it to reuse the port, like this

    (provides C code)

    Basically, in C, you call a function called setsockopt(), and one of the parameters is called SO_REUSEADDR, which lets you reuse that port.

    I found some brief links on google which should get you started figuring out how to set the equivalent option in Java:

    http://java.sun.com/j2se/1.4.2/docs/guide/net/socketOpt.html

    http://java.sun.com/j2se/1.4.2/docs/api/java/net/SocketOptions.html

提交回复
热议问题