Using Java to make multiple point senders

Source: Internet
Author: User
Tags socket port number

The IP protocol is the means by which all information on the Internet is transmitted, and UDP (user Datagram Protocol, Subscriber Datagram Protocol) datagram is encapsulated in an IP packet and sent to the appropriate machine on the network. As we all know, most IP uses a single point of dispatch, that is, sending one package from one host to another. However, the IP protocol also has the ability to send multicast, to use multicast, a message labeled with a set of target host address, when the message issued, the entire group can receive. To support multicast, a range of IP addresses are divided separately. These IP addresses are Class D addresses, which range from 224.0.0.0 to 239.255.255.255.

IP multicast (or multicast) is a fairly new technique, which is an improvement on simple broadcasts. The multicast function is similar to a single message sent to multiple receivers, but only to the recipients who wait for it. The idea is to set up a set of network addresses as multicast addresses, which range from 225.0.0.0 to 239.255.255.255. Each multicast address is treated as a group. When a user needs information from an address, it can be added to the group. For example, you might set up an address 225.23.32.36 as a multicast address for its stock quote system, and if you want to receive a stock query quote, you must add to the 225.23.32.36 Group.

First, the basic knowledge of the production of multi-point transmission

When you use Java for multicast, its MulticastSocket class is the key to achieving this functionality. The MulticastSocket class allows users to send and receive datagrams that use multicast IP. To send or receive multicast data, you must first create a multicast socket (a multicast socket is similar to a datagram socket, in fact MulticastSocket is a subclass of Datagramsocket). To send a data clock, you can use the default port number to create a multicast socket, or you can specify a port number in the constructor to create a multicast socket:

Public MulticastSocket () throws IOException

Public multicastsocket (int portnumber) throws IOException

To add to a multicast address, you can use the Jiongroup method; to detach from 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 can be problematic for multicast because the user needs to listen on a specified interface, by calling Setinterface to select the interface used by multicast sockets:

public void Setinterface (InetAddress interface) throws SocketException

You can query the interface for multicast sockets by calling the GetInterface method:

Public InetAddress GetInterface () throws SocketException

The Send method is used when a user sends data from a multicast point:

public synchronized void Send (Datagrampacket packet, byte timetolive) throws IOException

Where the TTL value specifies how many networks the packet should span. When the TTL is 0 o'clock, the specified packet should remain on the local host, when the value of the TTL is 1 o'clock, the specified packet is sent to the local network, and when the TTL value is 32 o'clock, it means that it should only be sent to the network on this site, and when the TTL is 64 o'clock, means that the packet should remain in the region; when the TTL value is 128, the data should remain on the continent, and when the TTL is 255, it means that the packet should be sent to all places, and if the default Send method is used, the TTL value is 1.

Related Article

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.