Java Multicast MulticastSocket

Source: Internet
Author: User

In unicast mode there are server and client points, and the multicast mode is different from unicast mode, each end is a router or switch as a relay station, either end of the router or switch to send messages, routing or switch responsible for sending other nodes, each node is equal. So you can use the same class representation in programming mode--multicastsocket.

MulticastSocket belongs to the class provided by the jdk , the classpath is java.net.MulticastSocket, and this class makes it easy to implement multicast functionality , a simple example is shown below, with two nodes transmitting messages through multicast.

① node One, specify the multicast address as228.0.0.4, the port is8000, node one by callingMulticastSocketof theJoingroupmethod request to add node one to the multicast team, and then use an infinite loop to send it to the group "Hello from Node1"message, which is for the convenience of the node2receive node after join1To prepare for the message, it is necessary to note that multicast is done byDatagrampacketobject that sends the message, calls theMulticastSocketof theSendmethod to send the message out. In order to reduce the length of the example to eliminate the exit group and the closure of some of the socket operation, the actual use needs to be perfected.

public class Node1 {

private static int port = 8000;

private static String address = "228.0.0.4";

public static void Main (string[] args) throws Exception {

try {

InetAddress Group = inetaddress.getbyname (address);

MulticastSocket MSS = null;

MSS = new MulticastSocket (port);

Mss.joingroup (group);

while (true) {

String message = "Hello from Node1";

byte[] buffer = message.getbytes ();

Datagrampacket DP = new Datagrampacket (buffer, buffer.length,

Group, port);

Mss.send (DP);

Thread.Sleep (1000);

}

} catch (IOException e) {

E.printstacktrace ();

}

}

}

② node Two, specify the same multicast address and port, request to join the same multicast group as the node, and then continue to receive messages sent from other nodes through the loop, through the multicastsocket receive method can be read to a message and will continue to receive messages sent from node one " receive from Node1:hello from Node1 ". Of course, node 2 can also send messages to the multicast group because each node is equal as long as the other nodes receive the multicast message. If you also want to add additional nodes, all nodes can receive the send message, even though they are requesting to join the multicast group.

public class Node2 {

private static int port = 8000;

private static String address = "228.0.0.4";

public static void Main (string[] args) throws Exception {

InetAddress Group = inetaddress.getbyname (address);

MulticastSocket MSR = null;

try {

MSR = new MulticastSocket (port);

Msr.joingroup (group);

byte[] buffer = new byte[1024];

while (true) {

Datagrampacket DP = new Datagrampacket (buffer, buffer.length);

Msr.receive (DP);

string s = new string (Dp.getdata (), 0, Dp.getlength ());

System.out.println ("Receive from Node1:" +s);

}

} catch (IOException e) {

E.printstacktrace ();

}

}

}

Java Multicast MulticastSocket

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.