Java peer-to-peer computing practices: Discovery Based on IP multicast (1)

Source: Internet
Author: User

To do useful work, P2P applications must be able to discover and interact with each other. This article provides an implementation of IP multicast-based discovery.

Before a software entity can participate in direct peer-to-peer interaction with P2P applications, the entity must discover the appropriate Peer-to-Peer Points to interact. All feasible P2P architectures provide a solution for Problem Discovery. In this article, I will describe the implementation of one of the mechanisms. Let's start today's discussion through review. The P2P application's Peer Discovery allows P2P applications to locate and interact with each other. There are multiple methods for implementing peer-to-peer service discovery. The simplest mechanism is the explicit point-to-point configuration. This mechanism works by requiring each peer to know all other peers that it may interact with and connect them. The main advantage of Point-to-Point configuration is simplicity. Its main disadvantage is its lack of flexibility and the ability to expand to a large network of peer nodes.

Another common model found is to use a central directory as an intermediary. This model is popular among many traditional non-P2P distributed applications, and its advantage is that it is well understood. Register your presence with the central directory and use the central directory to locate other peers. The main advantage of this model is its ease of management and scalability. However, its centralized design will lead to single point of failure. Therefore, it lacks the ability to resist the dangers caused by its natural power or the increase in the number of online surfing users.

Many popular P2P applications use the network model instead of the central directory. In the network model, a single peer only knows the peer identity on the local network. Each peer is used as the directory of the Peer that is connected to it. Peer nodes collaborate by spreading directory queries to adjacent peer nodes and returning relevant responses. The main advantage of this model is that it is not centralized. Its main drawback is that propagation query consumes a lot of network and processing capabilities.

There are several variants of the above three mechanisms. Without discussing these variants, let's move on and study another discovery mechanism.

IP multicast discovery

The multicast model is similar to the network model for maintaining the directory of each peer. However, peer nodes do not cooperate to implement large-scale network queries. In addition, peer nodes use the IP multicast feature provided by the network to locate and identify other peer nodes.

IP multicast is connectionless and unreliable, unlike TCP/IP, Which is connection-oriented and reliable ). Although it uses IP datagram, multicast IP datagram can be sent from one host to another, unlike unicast IP datagram.

Peer nodes regularly use IP multicast to declare their own existence. The announcement contains their host names and a port for normal communication. The peer that is interested in the message detects the message, extracts the host name and port number, and uses the message to establish a communication channel.

Review is enough. Let's start to study the code.

Simple client and server

Starting from a simple example, this example demonstrates how two processes use IP multicast for communication. To simplify the demo, I will introduce the examples from the client and server processes. P2P applications usually implement these two processes. It is not easy to divide them into clients or servers.

In this example, the server process loops and waits for the arrival of the datagram packet. Each time a packet is received, the server prints a brief Diagnostic message to the console. The client role is much simpler-It multicast a single datagram packet and exits.

Listing 1 and 2 demonstrate how these two parts are combined. The comments in the Code indicate what is happening.

List 1. Simple Server

public
class Server
{
public
static
void
mainString [] arstring)
{
try
{
// Create a multicast datagram socket for receiving IP
// multicast packets. Join the multicast group at
// 230.0.0.1, port 7777.
MulticastSocket multicastSocket = new MulticastSocket7777);
InetAddress inetAddress = InetAddress.getByName"230.0.0.1");
multicastSocket.joinGroupinetAddress);
// Loop forever and receive messages from clients. Print
// the received messages.
while true)
{
byte [] arb = new byte [100];
DatagramPacket datagramPacket = new DatagramPacketarb, arb.length);
multicastSocket.receivedatagramPacket);
System.out.printlnnew Stringarb));
}
}
catch Exception exception)
{
exception.printStackTrace);
}
}
}


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.