Java Reference
In-Depth Information
broadcast address for the network with the local address 192.168.254.*. UDP broadā
casting is often used for protocols such as DHCP that need to communicate with servers
on the local net whose addresses are not known in advance. This option is controlled
with these two methods:
public
void
setBroadcast
(
boolean
on
)
throws
SocketException
public
boolean
getBroadcast
()
throws
SocketException
Routers and gateways do not normally forward broadcast messages, but they can still
kick up a lot of traffic on the local network. This option is turned on by default, but if
you like you can disable it thusly:
socket
.
setBroadcast
(
false
);
This option can be changed after the socket has been bound.
On some implementations, sockets bound to a specific address do not
receive broadcast packets. In other words, you should use the
Data
gramPacket(int port)
constructor, not the
DatagramPacket(InetAd
dress address, int port)
constructor to listen to broadcasts. This
is necessary in addition to setting the SO_BROADCAST option to true.
IP_TOS
Because the traffic class is determined by the value of the IP_TOS field in each IP packet
header, it is essentially the same for UDP as it is for TCP. After all, packets are actually
routed and prioritized according to IP, which both TCP and UDP sit on top of. There's
really no difference between the
setTrafficClass()
and
getTrafficClass()
methods
in
DatagramSocket
and those in
Socket
. They just have to be repeated here because
DatagramSocket
and
Socket
don't have a common superclass. These two methods let
you inspect and set the class of service for a socket using these two methods:
public
int
getTrafficClass
()
throws
SocketException
public
void
setTrafficClass
(
int
trafficClass
)
throws
SocketException
The traffic class is given as an int between 0 and 255. Because this value is copied to an
eight-bit field in the TCP header, only the low-order byte of this
int
is used; and values
outside this range cause
IllegalArgumentException
s.
The JavaDoc for these options is severely out of date, and describes a
quality of service scheme based on bit fields for four traffic classes: low
cost, high reliability, maximum throughput, and minimum delay. This
scheme was never widely implemented and probably hasn't been used
in this century.