即使重用地址设置为 true,什么也会导致 TCP 套接字抛出“java.net.BindException:地址已在使用中”?仅当应用程序快速重新启动时才会发生这种情况。在 CentOS 5 Linux 操作系统上运行。
最佳答案
这有点解释它:
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)
基本上,在 C 语言中,您调用一个名为 setsockopt() 的函数,其中一个参数称为 SO_REUSEADDR,它允许您重用该端口。
我在 google 上找到了一些简短的链接,这些链接应该可以帮助您开始了解如何在 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
关于Java 绑定(bind)异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/846861/