Posts

GROUP

Image
 Multicast : UDPMulticastServer: import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; public class UDPMulticastServer {    public static void sendUDPMessage(String message,    String ipAddress, int port) throws IOException {       DatagramSocket socket = new DatagramSocket();       InetAddress group = InetAddress.getByName(ipAddress);       byte[] msg = message.getBytes();       DatagramPacket packet = new DatagramPacket(msg, msg.length,group, port);       socket.send(packet);       socket.close();    }    public static void main(String[] args) throws IOException {       sendUDPMessage("This is a multicast messge", "230.0.0.0",4321);       sendUDPMessage("This is the second multicast messge","230.0.0.0", 4321);       sendUDPMessage("This is th...

MULTITHREADING

Image
Mainserver.java: /*  * 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.  */ /**  *  * @author staffpc  */ /*  * 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.  */ /**  *  * @author rttd  */ import java.io.*; import java.net.*; public class MainServer {    public static void main(String[] args) throws Exception    {        Socket s=null;        ServerSocket ss2=null;        System.out.println("Server Listening ......");        ss2=new ServerSocket(4446);        while(true)        {            try  ...

IPC

Image
 /*  * 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.  */ /**  *  * @author labpc  */ class Customer{     int amount=10000;          synchronized void withdraw(int amount){     System.out.println("going to withdraw...");          if(this.amount<amount){     System.out.println("Less balance; waiting for deposit...");     try{wait();}catch(Exception e){}     }     this.amount-=amount;     System.out.println("withdraw completed...");     }          synchronized void deposit(int amount){     System.out.println("going to deposit...");     this.amount+=amount;     System.out...

RPC/RMI

NAME RESOLUTION PROTOCOL

Image
Code:   import java.io.*; import java.net.*; class NameResolution {     public static void main(String[] args)     {         try {             // IP Address             InetAddress addr                 = InetAddress.getByName("23.229.203.68");                // Host name             System.out.println("Host name is: "                                + addr.getHostName());                // Host Address             System.out.println("Ip address is: "                                + addr.getHostAddress());         }     ...

CLOCK SYNCHRONIZATION (Lamport's Algorithm)

Image
Lamport.java: import java.util.*; import java.util.Scanner; import javax.swing.*; import java.awt.*; import java.awt.geom.*; public class Lamport{ int e[][]=new int[10][10]; int en[][]=new int[10][10]; int ev[]=new int[10]; int i,p,j,k; HashMap<Integer,Integer> hm=new HashMap<Integer,Integer>(); int xpoints[] =new int[5]; int ypoints[] =new int[5]; class draw extends JFrame{ private final int ARR_SIZE = 4;             void drawArrow(Graphics g1, int x1, int y1, int x2, int y2) {                 Graphics2D g = (Graphics2D) g1.create();                 double dx = x2 - x1, dy = y2 - y1;                 double angle = Math.atan2(dy, dx);                 int len = (int) Math.sqrt(dx*dx + dy*dy);                 AffineTransform at = AffineTransform.getTr...

LOAD BALANCING

Image
 IpPool.java: import java.util.Map; import java.util.concurrent.ConcurrentHashMap; public class IpPool {     public static Map<String, Integer> ipMap = new ConcurrentHashMap<>();     static {         ipMap.put("192.168.1.1", 10);         ipMap.put("192.168.1.2", 10);         ipMap.put("192.168.1.3", 10);         ipMap.put("192.168.1.4", 10);         ipMap.put("192.168.1.5", 10);         ipMap.put("192.168.1.6", 10);         ipMap.put("192.168.1.7", 10);         ipMap.put("192.168.1.8", 10);         ipMap.put("192.168.1.9", 10);         ipMap.put("192.168.1.10", 10);     } } LoadBalance.java:Interface public interface LoadBalance {     String getServer(String clientIp); } RoundRobin.java: import java.util.ArrayList; import java.util.Li...