java ipv4和ipv6通信,如何支持IPv4和& Java上的IPv6

One of our Java program when started, it only listen on IPv6 (8080)

e.g.

# netstat -ntpl

Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name

tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN -

tcp6 0 0 :::8080 :::* LISTEN -

tcp6 0 0 :::22 :::* LISTEN -

The problem is it is not accessible from outside (except localhost), to solve this, I have this manually add

-Djava.net.preferIPv4Stack=true

But this make the program is only for IPv4 network.

Is it possible to do something like the sshd as above, both support IPv4 and IPv6?

解决方案

I suspect it's less a Java programming issue than an OS networking stack/OS network configuration issue:

On some OSes, a single native TCP socket can listen to a port on both

IPv4 and IPv6 simultaneously. It is able to accept connections from

remote IPv4 and from remote IPv6 clients. On other OSes (such as WinXP)

an OS native socket CANNOT do that, but can only accept from IPv4 or

IPv6, not both. On those OSes, it is necessary to have two listen

sockets in order to be able to accept connections from both remote IPv4

and IPv6 clients, one socket to listen for IPv4 connections and one for

IPv6.

Windows 7 and Windows Server 2008 handle dual stacks just fine; Windows XP not so much :)

You seem to be on Linux - most modern Linux desktops and servers also handle dual ipv4 ipv6 with no problem.

Here's a good article on interoperability:

You know how you can "turn off" IPV6 for your Java application: -Djava.net.preferIPv4Stack=true

You can also force your server to use IPV6 like this: echo 0 > /proc/sys/net/ipv6/bindv6only

This is arguably your best source:

You should absolutely be able to accomplish what you want (at least at the Java programming level), unless you're limited by external network issues:

Nodes) V4 Only V4/V6 V6 Only

------- ----- -------

V4 Only x x

V4/V6 x x x

V6 Only x x

PS:

Here's one more good link, which explains what's happening at the socket level. It's not Java (it's C), but exactly the sample principles apply:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值