Sunday 28 June 2020

Write a java program to demonstrate subnetting and find the subnet masks

Implement Client/Server using RPC  Parallel and Distributed Systems




Implement Client/Server using RPC  Parallel and Distributed Systems



Client

import java.io.*;
import java.net.*;

class cli {

    public static void main(String[] args) throws Exception {
        Socket sock = new Socket("127.0.0.1", 3000);
        BufferedReader keyRead = new BufferedReader(new InputStreamReader(System.in));
        OutputStream ostream = sock.getOutputStream();
        PrintWriter pwrite = new PrintWriter(ostream, true);
        InputStream istream = sock.getInputStream();
        BufferedReader receiveRead = new BufferedReader(new InputStreamReader(istream));
        System.out.println("Client ready, type and press Enter key");
        String receiveMessage, sendMessage, temp;
        while (true) {
            System.out.println("\nEnter operation to perform(add,sub,mul,div)....");
            temp = keyRead.readLine();
            sendMessage = temp.toLowerCase();
            pwrite.println(sendMessage);
            System.out.println("Enter first parameter :");
            sendMessage = keyRead.readLine();
            pwrite.println(sendMessage);
            System.out.println("Enter second parameter : ");
            sendMessage = keyRead.readLine();
            pwrite.println(sendMessage);
            System.out.flush();
            if ((receiveMessage = receiveRead.readLine()) != null) {
                System.out.println(receiveMessage);
            }
        }
    }
}

Server


import java.io.*;
import java.net.*;
class ser {

    public static void main(String[] args) throws Exception {
        ServerSocket sersock = new ServerSocket(3000);
        System.out.println("Server ready");
        Socket sock = sersock.accept();
        BufferedReader keyRead = new BufferedReader(new InputStreamReader(System.in));
        OutputStream ostream = sock.getOutputStream();
        PrintWriter pwrite = new PrintWriter(ostream, true);
        InputStream istream = sock.getInputStream();
        BufferedReader receiveRead = new BufferedReader(new InputStreamReader(istream));
        String receiveMessage, sendMessage, fun;
        int a, b, c;
        while (true) {
            fun = receiveRead.readLine();
            if (fun != null) {
                System.out.println("Operation : " + fun);
            }
            a = Integer.parseInt(receiveRead.readLine());
            System.out.println("Parameter 1 : " + a);
            b = Integer.parseInt(receiveRead.readLine());
            if (fun.compareTo("add") == 0) {
                c = a + b;
                System.out.println("Addition = " + c);
                pwrite.println("Addition = " + c);
            }
            if (fun.compareTo("sub") == 0) {
                c = a - b;
                System.out.println("Substraction = " + c);
                pwrite.println("Substraction = " + c);
            }
            if (fun.compareTo("mul") == 0) {
                c = a * b;
                System.out.println("Multiplication = " + c);
                pwrite.println("Multiplication = " + c);
            }
            if (fun.compareTo("div") == 0) {
                c = a / b;
                System.out.println("Division = " + c);
                pwrite.println("Division = " + c);
            }
            System.out.flush();
        }
    }

}
</hr>

package com.mycompany.testprojects;
import java.util.Scanner;
/**
 *
 * @author rajesh shukla
 */
public class npds {
    
    




public static void main(String args[]){

Scanner sc = new Scanner(System.in);
int i;

System.out.print("IP address:" );

String ip = sc.nextLine();

String split_ip[] = ip.split("\\."); 

String split_bip[] = new String[4];

String bip = "";

for(i=0;i<4;i++){

split_bip[i] =appendZeros(Integer.toBinaryString(Integer.parseInt(split_ip[i]))); 

bip += split_bip[i];

}

System.out.println("IP in binary = "+bip);

System.out.print("Enter the addresses: ");

int n = sc.nextInt();

int bits = (int)Math.ceil(Math.log(n)/Math.log(2));

System.out.println("Number of bits required = "+bits);

int mask = 32-bits;

System.out.println("The sub net mask = "+mask);

int fbip[] = new int[32];

for( i=0; i<32;i++)
    fbip[i] = charAt(i)-48;

for(i=31;i>31-bits;i--)
fbip[i] &= 0;

String fip[] = {"","","",""};

for(i=0;i<32;i++)

fip[i/8] = new String(fip[i/8]+fbip[i]);

System.out.print("First address is =");

for( i=0;i<4;i++){

System.out.print(Integer.parseInt(fip[i],2));

if(i!=3) System.out.print(".");

}

System.out.println();

int lbip[] = new int[32];

for( i=0; i<32;i++)
    lbip[i] = charAt(i)-48;

for( i=31;i>31-bits;i--)

lbip[i] |= 1;

String lip[] = {"","","",""};

for(i=0;i<32;i++)

lip[i/8] = new String(lip[i/8]+lbip[i]);

System.out.print("Last address is = ");

for(i=0;i<4;i++){

System.out.print(Integer.parseInt(lip[i],2));

if(i!=3) System.out.print(".");

}

System.out.println();

}

static String appendZeros(String s){

String temp = new String("00000000");

return temp.substring(s.length())+ s;

}

}





0 comments:

Post a Comment