Java Reference
In-Depth Information
dgs.receive(dgp);
System.out.println(new String(dgp.getData()));
}
catch (IOException ioe)
{
System.err.println("i/o
error:
"+ioe.getMessage());
}
}
}
Listing9-5 issimilar to Listing9-4 , butthere'sonebigdifference.Iusethe Data-
gramPacket(byte[] buf, int length, InetAddress address, int
port) constructortospecifytheserver'sdestination,whichhappenstobeport10000
onthelocalhost,inthedatagrampacket.The send() methodcallroutesthepacketto
this destination.
Compile Listing 9-5 ( javac DGClient.java ) and run the application ( java
DGClient ). Assuming that DGServer is also running, you should observe the fol-
lowing output in DGClient 's command window (and the last line of this output in
DGServer 's command window):
client is starting
Send me a datagram
MulticastSocket describesasocketfortheclientorserversideofaUDP-based
multicasting session. Two commonly used constructors are MulticastSocket()
(createamulticastsocketnotboundtoaport)and MulticastSocket(int port)
(create a multicast socket bound to the specified port ).
WHAT IS MULTICASTING?
Previousexampleshavedemonstrated unicasting ,whichoccurswhenaserversends
a message to a single client. However, it's also possible to broadcast the same mes-
sage to multiple clients (e.g., transmit a “school closed due to bad weather” an-
nouncementtoallmembersofagroupofparentswhohaveregisteredwithanonline
program to receive this announcement); this activity is known as multicasting .
A server multicasts by sending a sequence of datagram packets to a special IP ad-
dress,whichisknownasa multicast group address ,andaspecificport(asspecified
Search WWH ::




Custom Search