Use of MulticastSocket in java

Source: Internet
Author: User

A basic concept: this class is a subclass of the initramsocket mentioned above.

Mongoramsocket only allows data to be sent to the specified destination address, while MulticastSocket can send data reports to multiple clients in broadcast mode.

To use multicast broadcast, you need to mark a datagram with a group of target host addresses. When the datagram is sent, all hosts in the group can receive the datagram. IP multi-point broadcast (or multi-point transmission) enables a single message to be sent to multiple recipients. The idea is to set a special network address as the multi-point broadcast address, each multicast address is considered as a group. When the client needs to send and receive broadcast information, add it to the group.

MulticastSocket can send a datagram to a multicast address or receive broadcast information from other hosts.


As a subclass, the maximum extension is the method it provides: 1. joinGroup (InetAddress mcastaddr) 2. joinGroup (SocketAddress mcastaddr, NetworkInterface netIf) 3. leaveGroup (InetAddress mcastaddr)
4. leaveGroup (SocketAddress mcastaddr, NetworkInterface netIf) 5.get/ setInterface (InetAddress inf) 6. get/setNetworkInterface (NetworkInterface netIf)
Note: NetworkInterface Network Interface Class
What is a network interface? The Network Interface Name is not a computer name, but a name used to identify a physical or logical network interface, which is generally set by the operating system. The network interface name starts with eth on most operating systems (including Windows, Linux, and Unix), followed by the index number of the network interface, starting from 0. If the local machine has three NICs, the network interface names are eth0, eth1, and eth2. Each network interface can be bound with an IP address or the MAC address of the device can be obtained accordingly.
Method: 1. Enumeration GetInetAddresses (): An Enumeration object that has all or part of the InetAddress method bound to this network interface.

After creating a MulticastSocket object, you also need to add the MulticastSocket to the specified multicast address. The MulticastSocket uses the joinGroup () method to join the specified group, and the leaveGroup () method to leave the group.

1. joinGroup (InetAddress multicastAddr): add the MulticastSocket to the specified multicast address.

2. leaveGroup (InetAddress multicastAddr): Let the MulticastSocket leave the specified multicast address.


The application only sends the datagram packet to the multicast address. The router ensures that the packet is sent to all hosts in the multicast group.
Multicast address: the address shared by a group of hosts in a multicast group. The multicast address ranges from 224.0.0.0 --- 239.255.255.255 (all of them start with Class D address 1110 ).
Note: If the IP addresses of three machines A, B, and C are different, A \ B listens to broadcast messages for the server, and C sends broadcast messages to the client, in my opinion, the MulticastSocket objects of machines A and B are bound to one of the multicast addresses, and the multicast addresses sent by client C are the same, then A and B can receive messages sent by C.
If MulticastSocket is used to receive information, use the default address and random port, but if it is used to receive information, you must specify the port; otherwise, the sender cannot determine the target port for sending the datagram.

Sample Code
MulticastSocket _socket = new MulticastSocket(5353);        InetSocketAddress sockaddr = new InetSocketAddress(224.0.0.251, 5353);                    try {                _socket.setNetworkInterface(hostInfo.getInterface());                _socket.joinGroup(sockaddr, hostInfo.getInterface());            } catch (SocketException e) {                DLog.w(TAG, TAGClass + "openMulticastSocket() Set network interface exception: "                        + e.getMessage());            }        }        _socket.setTimeToLive(255);





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.