在UDP广播中,“通配符地址"是什么? [英] What is the 'wildcard address' In the context of UDP Broadcasts?

查看:162
本文介绍了在UDP广播中,“通配符地址"是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请参阅 DatagramSocket的Java 6 API文档类:

UDP广播发送始终在DatagramSocket上启用.为了 要接收广播数据包,应将 DatagramSocket绑定到 通配符地址.在一些实施方式中,广播分组也可以 将DatagramSocket绑定到更特定的地址时会被接收.

UDP broadcasts sends are always enabled on a DatagramSocket. In order to receive broadcast packets a DatagramSocket should be bound to the wildcard address. In some implementations, broadcast packets may also be received when a DatagramSocket is bound to a more specific address.

有人可以告诉我通配符地址"是什么吗?并且以下内容对于侦听UDP广播有效:

Could someone tell me what the 'wildcard address' is? And is the following valid for listening for UDP broadcasts:

MulticastSocket socket = new MulticastSocket(new InetSocketAddress(InetAddress.getByName("0.0.0.0"),4445);

推荐答案

通配符地址为0.0.0.0.请勿与广播至所有子网的地址255.255.255.255.混淆.更正确的称呼为INADDR_ANY.

The wildcard address is 0.0.0.0. Not to be confused with the broadcast-to-all-subnets address, which is 255.255.255.255. It is more correctly called the 'any' address, after INADDR_ANY.

在Java中,最容易使用的方法是提供null作为绑定地址,或者完全省略参数,例如new InetSocketAddress(null, 0)new InetSocketAddress(0).换句话说,这是绑定时的默认设置,因此暗含了良好实践".

In Java it is most easily used by supplying null as a bind-address, or omitting the parameter altogether, e.g. new InetSocketAddress(null, 0) or new InetSocketAddress(0) respectively. In other words it is the default when binding, and therefore implicitly 'good practice'.

这篇关于在UDP广播中,“通配符地址"是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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