Use Java production multi-point sender 2001-03-23 · Lin Jian Gang · Yesky
The IP protocol is the propagation means of all information on the Internet, the UDP (User DataGram Protocol, User Data Network Protocol) Datasheet is packaged in the IP package, sent to the appropriate machine on the network. As we all know, most IP uses a single point to send a package to another host from one host. However, the IP protocol also has a multi-point transmission ability. When you use a multi-point transmission, a message label has a set of target host addresses. When the packet is issued, the entire group can be received. To support multi-point transmission, a range of IP addresses are separately divided separately. These IP addresses are D-class addresses, which ranges from 224.0.0.0 to 239.255.255.255.
IP multi-point transmission (or multipoint broadcast) is a quite new technology that is an improvement to simple broadcast. Multipoint broadcast functions are similar to a single message to send to multiple recipients, but only send to those recipients waiting for it. Its thinking is to set a group of network addresses as multipart -p-point broadcast addresses, which range from 225.0.0.0 to 239.255.255.255. Each multipoint broadcast address is seen as a group. When the user needs to add to the group when the user needs to be added. For example, the address 225.23.32.36 may be set up for its stock quotation system as a multi-point broadcast address, and if a stock query quotation procedure must be added to the 225.23.32.36 group.
First, make a basic knowledge of multi-point transmission
It is a key to implementing this feature when multi-point transmission using Java is used. The MulticastSocket class allows users to send and receive datagrams using multi-point transmission IP. To send or receive multipoint broadcast data, you must first create a multipoint broadcast socket (multipoint broadcast socket is similar to the data report set, in fact, MulticastSocket is a subclass of the DataGramsocket). To send a datagram, you can create a multipoint broadcast socket using the default port number, or you can create a multipoint broadcast socket in the constructor to specify the port number:
Public multicastsocket () throws oException
Public MulticastSocket (int portnumber) throws oException
To join a multipoint broadcast address, you can use the JionGroup method; if you want to leave a group, use the LeaveGroup method:
Public Void Jiongroup (inetaddress multicastaddr) THROWS IOEXCEPTION
Public void LeaveGroup (inetaddress multicastaddr) THROWS IOEXCEPTION
In some systems, there may be multiple network interfaces. This may bring problems with multi-point broadcast because users need to listen on a specified interface, and select the interface used by multipoint broadcast socket by calling SETINTERFACE:
Public void setInterface (inetaddress interface) throws socketException
You can query the interface of multi-point broadcast socket by calling the GetInchrface method:
Public inetaddress GetInterface () THROWS SocketException
When the user is transmitted multi-point, use the Send method:
Public Synchronized Void Send (DataMpacket Packet, BYTE TIMET Packet, "The TTL value specifies how many networks should be across the data packet, when TTL is 0, the specified packet should stay in the local host; when the value of TTL is 1, The specified packet is sent to the local network; when the value of TTL is 32, it means that only the network of this site should only be sent; when TTL is 64, it means that the data package should be retained in the region; when the value of TTL is At 128, it means that the data should be retained in the continent; when TTL is 255, it means that the data package should be sent to all places; if the default Send method is used, the value of TTL is 1.
Second, make multi-point sending applications
Let's make a multi-point sending application using Java. The program 1 (multicastcastsender.java) is a program that sends a data to a specified multi-point transmission IP address. Use two parameters when running: The first specified multi-point sends IP address, the other specified Listen to the UDP port of the application. Where the main () method guarantees that these parameters are received, instantiate a Multicast Shender object. In addition, the constructor uses a multi-point String object, creates an inetaddress instance, and then creates a multicastSocket for the Send Denoms on the dynamically assigned port, and then the constructor enters a while loop, imports from standard input. This program wraps the first 512 bytes of each line into a DataGrampacket marked with an address and sends the datagram via MulticastSocket.
The source code of the program Multicast Shender.java is as follows:
Import java.net. *; // import package names.
Import java.io. *;
Class Multicast Shender {
Private static factory byte ttl = 1;
Private static final int datagram_bytes = 1024;
Private int mulcastport;
Private inetaddress mulcastip;
Private buffredreader input;
Private multicastsocket mulcastsocket;
Public static void main (String [] args) {
// this Must Be The Same Port and IP Address Used by the Receivers.
IF (args.length! = 2) {
System.out.print ("USAGE: MULTICASTSENDER" "/ n / t can be one of 224.x.x.x" "- 239.x.x.x / n");
System.exit (1);
}
Multicast Shender Send = New MulticastSender (ARGS);
System.exit (0);
}
Public multicastsender (String [] args) {
DataGrampacket MulcastPacket; // UDP DataGram.
String NextLine; // Line from stdin.
Byte [] MulcastBuffer; File: // Buffer for DataGram.byte [] Linedata; // the data type.
Int Sendlength; File: // Length of Line.
Input = New BufferedReader (NEW INPUTSTREAMREADER (System.in);
Try {
// CREATE A MULTICASTING SOCKET.
Mulcastip = inetaddress.getbyname (Args [0]);
Mulcastport = integer.parseint (args [1]);
mulcastsocket = new multicastsocket ();
} catch (unknownhostexception eXCPT) {
System.err.Println ("Unknown Address: EXCPT);
System.exit (1);
} catch (ioException eXCPT) {
System.err.Println ("Unable to Obtain Socket:" EXCPT);
System.exit (1);
}
Try {
File: // loop and read lines from standard input.
While (NEXTLINE = INPUT.READLINE ())! = null) {
Mulcastbuffer = new byte [datagram_bytes];
File: // if line is longer Than Your Buffer, Use the length of the buffer available.
IF (NextLine.Length ()> MulcastBuffer.Length) {
Endlength = MulcastBuffer.Length;
// OtherWise, Use the line's length.
} Else {
SendLength = NextLine.Length ();
}
// Convert the line of infut to bytes.
Linedata = nextLine.getbytes ();
// Copy The Data Into the Blank Byte Array File: // Which You Will Use to create the database of DataGrampacket.
For (INT I = 0; i MulcastBuffer [i] = linedata [i]; } Mulcastpacket = New DataGrampacket (MulcastBuffer, MulcastBuffer.length, Mulcastip, Mulcastport); // dend the datagram. Try { System.out.println ("Sending: / T" NextLine); Mulcastsocket.send (MulcastPacket, TTL); } catch (ioException eXCPT) { System.err.Println ("Unable to send packet: eXCPT); } } } catch (ioException eXCPT) { System.err.Println ("Failed I / O:" EXCPT); Mulcastsocket.close (); File: // Close The socket. } } The sender implemented the sender by receiving a multi-point transmitted data. The app has two parameters, which must correspond to the IP address and port used to activate MulticastSender. The main () method first checks the parameters of the command and then creates a MulticastReceiver object. The object's constructor creates an inetaddress and a multicastsocket on the port that activates the application. Add a multi-point sender in the address contained in the inetaddress, then enter a loop. The object's constructor receives the datagret from a socket and prints a computer and port included in the datagram, including the sending packets. The source code of the program 2 (MulticastReceiver) is as follows: Import java.net. *; // import package names. Import java.io. *; Class multicastReceiver { Private static final int datagram_bytes = 1024; Private int mulcastport; Private inetaddress mulcastip; Private multicastsocket mulcastsocket; Private Boolean KeepRecing = true; Public static void main (String [] args) { // this Must Be the Same Port and ip address used by the sender. IF (args.length! = 2) { System.out.print ("USAGE: MULTICASTRECEIVER / N / T can be one of" "224.x.x.x - 239.x.x.x / n"); System.exit (1); } MulticastReceiver Send = New MulticastRecEver (ARGS); System.exit (0); } Public multicastReceiver (string [] args) { DataGrampacket MulcastPacket; // packet to receive. Byte [] mulcastbuffer; // byte [] array buffer Inetaddress fromip; file: // dender address. INT fromport; // sender port. String mulcastmsg; // string of message. Try { // first, set up your receiving socket. Mulcastip = inetaddress.getbyname (Args [0]); Mulcastport = integer.parseint (args [1]); Mulcastsocket = New MulticastSocket (Mulcastport); // join the multicast group. Mulcastsocket.joingroup (mulcastip); } catch (unknownhostexception eXCPT) { System.err.Println ("Unknown Address: EXCPT); System.exit (1); } catch (ioException eXCPT) { System.err.Println ("Unable to Obtain Socket:" EXCPT); System.exit (1); } While (KeepReceiving) { Try { // CREATE A New DataGram. Mulcastbuffer = new byte [datagram_bytes]; Mulcastpacket = New DataGrampacket (MulcastBuffer, MulcastBuffer.length); // Receive the datagram. Mulcastsocket.Receive (Mulcastpacket); Fromip = mulcastpacket.getaddress (); Fromport = mulcastpacket.getport (); Mulcastmsg = new string (mulcastpacket.getdata ()); // Print Out the data. System.out.println ("Received from" from " " from " ": mulcastmsg); } catch (ioException eXCPT) { System.err.Println ("Failed I / O:" EXCPT); } } Try { Mulcastsocket.leavegroup (Mulcastip); File: // Leave the group. } catch (ioException eXCPT) { System.err.Println ("Socket Problem Leaving Group:" EXCPT); } Mulcastsocket.close (); // close the socket. } Public void stop () { KeepReceiving { KeepRecing = false; } } } To use the application, compile MultiCastSender and MulticastReceiver, then transfer MulticastReceiver to other machines to demonstrate that multiple participants receive packets, and finally the Java interpreter runs the application. For example: To send a multi-point send message to the port 1111 to the 225.2.32.6 group, it should be performed as follows: - / Class -> Java MulticastSender225.2.32.6 1111 Here Is Just to Test Multicast Message. SENDING: Here Is Just To Test Multicast Message. Do You ReceiveD IT? Sending: Do you receive it? To receive these packets, you should run the MulticastRecEact application on one or more systems, which should join the same multi-point transmission group 225.2.32.6, and listen on the same port 1111: - / Class -> Java MulticastRecEternal 225.2.32.6 1111 Received from 225.106.36.32 on port 32911: Here Is Just to test multicast message.received from 225.106.36.32 on port 32911: do you received it?