Application Multicast Technology in Java
I have recently seen many Java distributed books, summarized here, introducing an important technology, multicast communications in distributed applications, and provide a simple chat room program that I wrote using multicast technology development.
Multicast technology: Multicast technology is not created by Java, which has provided considerable support at this layer of Internet Agreement, and any language can access this service, but Java language provides a relatively simple access to this service. Also have a standard interface that is not related to the platform. However, it is not like point-to-point connections, the recipient can easily correct a small error such as a packet loss by requiring the sender's retarded data packet, and the TCP protocol can easily correct such a small error such as a packet, most of these errors, most of these errors It is not easy to get a repair. If you have a data packet that has received a certain data packet in many receivers, you cannot transfer this package. Although the TCP protocol can provide a reliable service, it is incompetent in the case of multicasting. As for IP multicast technology, although it is also based on IP protocol, it uses another different transport protocol, UDP, and UDP protocols can send a single data packet, we call the datagram, but it does not provide any error correction means .
Multicast Communication is a special IP address (Class D IP address) These IP addresses are not associated with any host, but to retain to multicast communication, these addresses are located at 224.0.0.1 ~ 239.255.255.255 Where 224.0.0.1 to 224.0.0.255 are used as a multicast routing information. In addition, all other D IP addresses can communicate casually.
For example, the multimedia meeting on the Internet is a multicast technology application. Especially in the event of transmitting video data or audio data, occasionally data loss can be easily overcome, as in this case, when the video data is playing directly, the subsequent data complement is nothing to use. This is often the case in real-time communication, transmitting the same data to multiple recipients, using multicast technologies more efficient than using multiple point pairs, because multicast technology uses broadcast in a communication thread to transmit only once, The router itself can determine if they need to send a multicast message, and information about whether a recipient is registered as a multicast address can also be processed by the router, in addition, multicast messages usually bring a strict survival cycle, it The number of routers to pass, for example, if the voice period data is 1, then this message can only be passed inside the LAN, if this value does not have a special setting, the default is 1, so this message is limited In the local area network.
From JDK1.1, multicast sockets are supported in the java.net package. In order to send a message to a multicast group, you must create a multi-player socket for a suitable port (MulticastSocket).
Inetaddress group = inetaddress.getbyname ("226.1.3.5");
INT port = 6789;
MulticastSocket Socket = New MultiCastsocket (port);
The sender does not necessarily register into a group to send data to the multicast address. In order to do this, it creates a DataGraMpacket object, assigns it to a data cache, and uses the byte array to populate After sending out by ready socket.
Byte [] buffer = new byte [500];
DataGrampacket DataGram = New DataGrampacket (Buffer, Buffer.Length, Group, Port)
DataGram.SetData (New String ("Hello World"). GetBytes ()); socket.send (DataGram);
In order to receive data incomplete statistics, the recipient must register himself into a group, which can create multicastsocket by using the sender to specify the port number, and then call the JOINGROUP () method to complete the registration in this group.
INT port = 6789;
MulticastSocket Socket = New MultiCastsocket (port);
Inetaddress group = inetaddress.getbyname ("226.1.3.5");
Socket.joingroup (group);
Then, the receiving party can wait for the arrival of the data. In fact, in order not to block the existing application, this usually needs to create a special thread to do an infinite loop waiting, the message is transmitted through bytes, in order to be able Interpret information in the datagram, the receiver must know the information data structure coming, that is, it must be able to reconstruct and send the same data structure as the interpretation process from the byte stream.
While (true) {
Socket.Receive (DataGram);
String message = new string (datagram.getdata ());
System.out.println ("Datagram Received from" DataGram.Getaddress (). GetHostaddress () "SAYING:" Message);
}
Use multicast technology chat room code:
One point shows that using IP multicast technology does not require a server responsible for the recovery message, so only one client is needed.
Import java.net. *;
Import java.io. *;
Public class chatclient {
// GUI
// chatframe gui;
// Name
String name;
Inetaddress group;
MulticastSocket Socket;
// Set the port
INT port = 6789;
//Constructor
Public chatclient (String name) {
THIS.NAME = Name;
Try {
Socket = New MulticastSocket (port);
Group = inetaddress.getbyname ("226.1.3.5");
Socket.joingroup (group);
System.out.println ("Connected ... / N");
While (true) {
Byte [] buffer = new byte [1000];
DataGrampacket DataGram = New DataGrampacket (Buffer, Buffer.Length);
Socket.Receive (DataGram);
String message = new string (datagram.getdata ());
System.out.println (Message);
}
} catch (exception e) {
System.out.println (e);
}
}
Public void sendtexttochat (string message) {
Message = name ":" Message "/ n";
BYTE [] BUF = (Message) .getbytes ();
DataGrampacket DG = New DataGrampacket (BUF, BUF.LENGTH); TRY {
Socket.send (DG);
} catch (exception e) {
System.out.println (e);
}
}
Public void disconnect () {}
Public static void main (string args []) {
IF (args.length! = 1)
Throw New RuntimeException ("Syntax: Java chatclient
ChatClient Client = New Chatclient (args [0]);
}
}