Multi-Point Transmission

Source: Internet
Author: User
IP multi-point transmission multicastsocket class IP multi-point transmission (Multicast delivery) is for point-to-point transmission and broadcast transmission. It refers to the broadcast of its members in a certain group, is a finite broadcast. Information sent by a member in the group, which can be received by all other members in the group. It is a branch of UDP sockets.
IP multi-point transfer is particularly suitable for applications with high bandwidth, such as sending videos and audio over the network. With the continuous improvement of network bandwidth and the continuous improvement of network communication quality, IP multi-point transmission will also be widely used in online chat and online meetings, distributed data storage, online transaction processing, interactive games. In addition, multi-point transmission can also be used by the client to find the corresponding server on the network. The client sends a multi-point transmission request, and any listening server can connect to the client and start a transaction.

UDP socket Basics
When using the user data protocol (UDP) for a session, the information must be assembled into a certain size of tabloids. When a message is sent, whether the recipient can receive and return information is always unknown. If the recipient cannot receive the returned information, we cannot determine whether the information we send is received-it may be lost on the way, the response information returned by the receiver may also be lost. In addition, the receiver may ignore our information. Therefore, UDP is described as unreliable, connectionless, and message-oriented.
Creating UDP sockets is like creating a mailbox. An email address is identified by an address. However, you do not need to create a new email address for each sender. You can write a destination address on a postcard containing the sent message, put it in your mailbox and send it out. The recipient may wait for a long time until a postcard containing information arrives at its mailbox, and the postcard identifies the sender's return address.

Principle of IP multi-point transfer
To support IP multi-point transmission, IP addresses in some ranges are separately set for this purpose. These IP addresses are Class D addresses, and the maximum 4-bit bitwise mode of their addresses is "1110 ", that is, the IP address range is between 224.0.0.0 and 239.255.255.255. Each of these IP addresses can be referenced as a multi-point transfer group, and any IP packets that address the IP address will be received by all other machines in the group, that is, an IP address is equivalent to an email address. In addition, the members in the group are dynamic and change over time.
For IP multi-point transfer, the inter-network Group Management Protocol (IGMP) is used to manage members in a multi-point transfer group. Multi-Point transmission routes can use IGMP to determine whether local machines are in favor of joining a group. A multi-point transmission route can decide whether to forward a multi-point transmission packet.
Time-to-live (TTL) is an important parameter that affects multi-point transmission packets ). TTL is used to describe how many different networks the sender wants to transmit. When the packet is forwarded by the router, the TTL in the packet will be reduced by one. When the TTL is zero, the packet will not be sent forward.
In actual use, we must pay attention to the following points:
1. These IP addresses can only be used as sink addresses. They cannot appear in any source address domain or in the source path or record Path Options:
2. Because IP multi-point transmission is one-to-multiple transmission, error messages cannot be generated using the error and Control Message Protocol (ICMP.
3. Send a message to a group. The sending host may not be a member of the group;
4. Some groups are assigned by Internet Assigned Numbers Authority (IANA) for special purposes. For more information, see ftp://ftp.internic.net/rfc/rfc1700.txt. In addition, avoid using some retention groups, from 224.0.0.0 to 224.0.0.225 only for local subnets. We recommend that you select an IP address between 224.0.1.27 and 224.0.1.225.
5. If the selected group has been used, communication with other machines will be messy. Once this happens, you can exit the application and try other addresses.
6. When a machine is added to a multicast group, it will begin to receive information about the IP multicast address. If a multi-point transmission packet is distributed to the network, any machine that listens to this information will have the opportunity to receive it. For IP multi-point transfer, there is no mechanism to limit whether machines on the same network can join this multi-point transfer group. Therefore, security is one of the issues that must be considered.
7. The selected TTL parameter should be as small as possible. A large TTL value may occupy the Internet bandwidth without any need. In addition, it may damage other multi-point transmission communication that uses the same group in different regions.

Classes related to IP multi-point transfer in Java
The java.net package contains the tools required for UDP communication, including IP multi-point transmission.

Datagrampacket class
We can use the mongorampacket class to create a datagram for sending. When receiving UDP data reports, we can use the mongorampacket class to read data, senders, and other information in the datagram.
To create a datagram and send it to the remote system, you can use the following constructor:
Public datagrampacket (byte ibuf, int length, inetaddress iaddr, int iport ,);
Ibuf is a byte array of encoded information data. Its length is the length of the byte array in which the data is stored. iaddr is an inetaddress object that stores the Host Name and IP address of the receiver, iport identifies the port on which the datagram is sent to the receiving host.
To receive data packets, you must use the mongorampacket constructor. Its prototype is public mongorampacket (byte ibuf, int ilength). ibuf refers to the data part of the received data packets, ilength is the length of this part of data. If ilength is smaller than the size of the UDP datagram received by the machine, the redundant bytes will be ignored by Java.
In addition, there are some methods in the class that let us get some relevant information:
Public int getlength (); // obtain the byte size of the data block in the datagram.
Public bytegetdata (); // get the data in the received Datagram
Public inetaddress getaddress (); // provides an inetaddress object for the sender
Public int getport (); // obtain the UDP port
It is worth noting that in TCP sockets programming, we do not need to block the transmitted data. However, when we create a UDP-based network communication application, we must create a set of methods, the running time determines the length of the datagram to be split. For TCP/IP, the maximum datagram can contain 65507 bytes of data. However, the host can only receive up to 548 bytes of data, A platform that supports 8192-byte large datagram uses the IP layer to split the datagram. If a data block containing IP packets is lost during transmission, the entire UDP datagram is lost. Therefore, when we determine the size of the datagram in the application, be cautious about the rationality of the dimension.
The following is an example of data Splitting:
// Read a row of data from the input stream cyclically
While (nextline = input. Readline ())! = NULL ){
// Define an empty datagram with a size of 512
Mcastbuffer = new byte [512];
// If the read data length is greater than the defined datagram length,
// Use the defined length; otherwise, use the length of the read data.
If (nextline. Length ()> mcastbuffer. Length ){
Sendlength = mcastbuffer. length;
} Else {
Sendlenth = nextline. Length ();
}
// Convert the read data to the byte type
Linedata = nextline. getbytes ();
// Copy the data to the byte array used to create the datagram
For (INT I = 0; I mcastbuffer [I] = linedata [I];
}
...... Create a datagram, send or receive ......
}

Multicastsocket class
The Java multicastsocket class is the key to implementing the network features of IP multicast. It allows us to use multicast IP addresses to send or receive UDP datagram. The constructor of multicastsocket is:
Public multicastsocket () throws ioexception; // create a multi-point transfer socket
Public multicastsocket (INT port) throws ioexception; // create a multi-point transfer socket on the specified port
In addition, other common methods in the class include:
Public void joingroup (inetaddress mcastaddr) throws ioexception {}// add to multicast group
Public void leavegroup (inetaddress mcastaddr) throws ioexception {}// exit the multicast group
Public synchronized void send (datagrampacket P, byte TTL) throws ioexception {} // send a Datagram
Public synchronized void receive (datagrampacket P, byte TTL) throws ioexception {} // receives a Datagram
After creating an initrampacket object, we must create a multicastsocket object accordingly. In this way, the datagram can be sent using the send () method. The following code demonstrates how to create a multicastsocket, send and receive IP multicast data packets:
Int multiport = 2345; // defines the port number. A non-superuser must use a port greater than 1024.
Int TTL = 1; // set the TTL value.
Inetaddress multiaddr = inetaddress. getbyname ("224.0.1.100"); // you can specify a multicast IP address.
Bytesmultibytes = {'h', 'E', '1', '1', 'O'}; // defines a datagram whose content is "hello ".
// Create a multi-point transfer Datagram
Datagrampacket smultidatatenew datatepacket (smultibytes, smultibytes, length, multiaddr, multiport );
Multicastsocket multisocket = new multicastsocket (); // create a multi-point transfer socket
Multisocket. Send (smultidatasync, TTL); // send a datagram (not added to the group)
......
Bytermultibytes = new byte [256]; // defines an empty datagram with a length of 256 bytes
// Create a receiving Datagram
Datagrampacket rmultidatax = new datagrampacket (rmultibytes, rmultibytes. Length );
Multisocket. joingroup (multiaddr); // Add to the multicast group
Multisocket. Receive (rmultidatasync); // receives UDP datagram

......
Multisocket. leavegroup (multiaddr); // leave the multicast group
Multisocket. Close (); // close the multi-point transfer socket
When the joingroup () method is called, the machine will focus on transmitting any IP packets belonging to a specific multicast group along the network, that is, the machine has a mailbox. The host should also use IGMP to report the use of the Group accordingly. For machines with multiple IP addresses, the interface for sending data packets should be configured: setinterface (oneofmylocaladdrs );
In initramsocket, there is no method similar to setso timeout () to set timeout.

Development Process of IP multi-point Transfer Application
Because IP multi-point transfer is mainly used for communication with members in the same group, the application development process is as follows:
1. Create a required datagram mongorampacket;
2. Create a multicastsocket for sending and receiving;
3. Add a multicast group;
4. Put the datagram into the multicastsocket for transmission;
5. Wait for receiving the datagram from multicastsocket;
6. decode the datagram extraction information;
7. respond according to the obtained information;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.