Skip to content

Instantly share code, notes, and snippets.

@arn-ob
Created July 30, 2017 09:10
Show Gist options
  • Save arn-ob/3277ec310196e2c3ee77982a1976c102 to your computer and use it in GitHub Desktop.
Save arn-ob/3277ec310196e2c3ee77982a1976c102 to your computer and use it in GitHub Desktop.
MultiCast Client/Server
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package multiclassclient;
import java.net.*;
/**
*
* @author Student
*/
public class MultiCastClient {
public static void main(String args[]){
try{
MulticastSocket ms = new MulticastSocket(40000);
InetAddress is = InetAddress.getByName("224.0.0.2");
ms.joinGroup(is);
byte b[] = new byte[500];
DatagramPacket p = new DatagramPacket(b,b.length);
while(true){
ms.receive(p);
String s = new String(p.getData());
System.out.println(s);
}
}catch(Exception e){
e.getMessage();
}
}
}
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.Scanner;
/**
*
* @author Student
*/
public class MulticastServer {
public static void main(String args[]){
try{
DatagramSocket ds = new DatagramSocket();
InetAddress is = InetAddress.getByName("224.0.0.2");
while(true){
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();
byte b[] = ("arnob : "+s).getBytes();
DatagramPacket dp3 = new DatagramPacket(b,b.length,is,40000);
ds.send(dp3);
}
}catch(Exception e){
e.getMessage();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment