Java绑定异常 [英] Java Bind Exception

查看:208
本文介绍了Java绑定异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

即使重用地址设置为true,什么会导致TCP套接字抛出java.net.BindException:Address already use?这只有在应用程序快速重新启动时才会发生。在CentOS 5 linux操作系统上运行。

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 restarted. Running on CentOS 5 linux OS.

推荐答案

这样解释一下:

http://www.beej.us/指导/ 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

(提供C代码)

基本上,在C ,你调用一个名为setsockopt()的函数,其中一个参数叫做SO_REUSEADDR,可以重用该端口。

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

我在google上找到了一些简短的链接让您开始了解如何在Java中设置等效选项:

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

这篇关于Java绑定异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆