Download as pdf or txt
Download as pdf or txt
You are on page 1of 34

NETWORK AND COMMUNICATION

CSE1004
FINAL LAB ASSIGNMENT

NAME : NIVESH D
REG NO : 19BCE0713

Alloted question :1

Question :

a) Develop a menu driven code to extract and display NETID, HOSTID, number of blocks and the
number of addresses of Class A, B and C IP address.

b) Develop a TCP client - server program to find the factorial of a given

number. The client program send any integer number to the client and

the server program reads and compute the factorial value of the
received number and display the same at client side.

a) Develop a menu driven code to extract and display NETID, HOSTID, number of blocks and the
number of addresses of Class A, B and C IP address.
CODE :

#include<stdio.h>

#include<stdlib.h>

#include<math.h>

#include<string.h>

void classful()

char IP[15],cls,a[4][4],application[15];

int
IP1[4],strtadr[4]={0,0,0,0},endadr[4]={255,255,255,255},j=0,k=0,netidbits=0,hostidbits=0,defmask[4]
={0,0,0,0};
long int noofnet=0,addrpernet=0;

printf("\nIP address: ");

scanf("%s",IP);

for(int i=0;i<strlen(IP);i++)

if(IP[i]=='.')

a[j++][k]='\0';

k=0;

continue;

a[j][k++]=IP[i];

a[j][k]='\0';

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

IP1[i]=atoi(*(a+i));

if(IP1[0]>=0 && IP1[0]<=127)

cls='A';

netidbits=8;

hostidbits=24;

strcpy(application,"Unicast");

noofnet=pow(2,7);

addrpernet=pow(2,24);

endadr[0]=127;

defmask[0]=255;

else if(IP1[0]>=128 && IP1[0]<=191)

cls='B';

netidbits=16;
hostidbits=16;

strcpy(application,"Unicast");

noofnet=pow(2,14);

addrpernet=pow(2,16);

strtadr[0]=128;

endadr[0]=191;

defmask[0]=255;

defmask[1]=255;

else if(IP1[0]>=192 && IP1[0]<=223)

cls='C';

netidbits=24;

hostidbits=8;

strcpy(application,"Unicast");

noofnet=pow(2,21);

addrpernet=pow(2,8);

strtadr[0]=192;

endadr[0]=223;

defmask[0]=255;

defmask[1]=255;

defmask[2]=255;

else if(IP1[0]>=224 && IP1[0]<=239)

cls='D';

strcpy(application,"Multicast");

strtadr[0]=224;

endadr[0]=239;

else if(IP1[0]>=240 && IP1[0]<=255)


{

cls='E';

strcpy(application,"Reserved");

strtadr[0]=240;

printf("\nClass: %c\nNetwork ID bits: %d\nHost ID bits: %d\nApplication: %s\nNumber of Networks:


%ld\nAddress Per Network: %ld",cls,netidbits,hostidbits,application,noofnet,addrpernet);

printf("\nDefault Mask: ");

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

if(i!=3)

printf("%d.",defmask[i]);

else

printf("%d",defmask[i]);

printf("\nStart Address: ");

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

if(i!=3)

printf("%d.",strtadr[i]);

else

printf("%d",strtadr[i]);

printf("\nEnd Address: ");

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

if(i!=3)

printf("%d.",endadr[i]);

else

printf("%d",endadr[i]);

printf("\n");

void classless()

char IP[18],cls,a[4][4],application[15];
int
IP1[5],strtadr[4]={0,0,0,0},endadr[4]={255,255,255,255},j=0,k=0,netidbits=0,hostidbits=0,defmask[4]
={0,0,0,0},cusmask[4]={0,0,0,0};

long int noofhost=0,usablehost=0,noofsub=0;

printf("\nIP address: ");

scanf("%s",IP);

for(int i=0;i<strlen(IP);i++)

if(IP[i]=='.' || IP[i]=='/')

a[j++][k]='\0';

k=0;

continue;

a[j][k++]=IP[i];

a[j][k]='\0';

for(int i=0;i<5;i++)

IP1[i]=atoi(*(a+i));

netidbits=IP1[4];

hostidbits=32-IP1[4];

if(IP1[0]>=0 && IP1[0]<=127)

cls='A';

strcpy(application,"Unicast");

cusmask[0]=defmask[0]=255;

netidbits-=8;

if(netidbits>=16)

cusmask[1]=255;

cusmask[2]=255;

cusmask[3]=255-pow(2,8-(netidbits%16))+1;
}

else if(netidbits>=8 && netidbits<16)

cusmask[1]=255;

cusmask[2]=255-pow(2,8-(netidbits%8))+1;

else if(netidbits<8)

cusmask[1]=255-pow(2,8-netidbits)+1;

endadr[0]=strtadr[0]=IP1[0];

if(hostidbits>16)

endadr[1]=pow(2,hostidbits-16)-1;

else if(hostidbits>8)

endadr[1]=IP1[1];

endadr[2]=pow(2,hostidbits-8)-1;

else

endadr[1]=IP1[1];

endadr[2]=IP1[2];

endadr[3]=pow(2,hostidbits)-1;

else if(IP1[0]>=128 && IP1[0]<=191)

cls='B';

strcpy(application,"Unicast");

cusmask[0]=defmask[0]=255;

cusmask[1]=defmask[1]=255;

netidbits-=16;

if(netidbits>=8)
{

cusmask[2]=255;

cusmask[3]=255-pow(2,hostidbits)+1;

else if(netidbits<8)

cusmask[2]=255-pow(2,8-netidbits)+1;

endadr[0]=strtadr[0]=IP1[0];

endadr[1]=strtadr[1]=IP1[1];

if(hostidbits>8)

endadr[2]=pow(2,hostidbits-8)-1;

else

endadr[2]=IP1[2];

endadr[3]=pow(2,hostidbits)-1;

else if(IP1[0]>=192 && IP1[0]<=223)

cls='C';

strcpy(application,"Unicast");

cusmask[0]=defmask[0]=255;

cusmask[1]=defmask[1]=255;

cusmask[2]=defmask[2]=255;

netidbits-=24;

cusmask[3]=255-pow(2,hostidbits)+1;

endadr[0]=strtadr[0]=IP1[0];

endadr[1]=strtadr[1]=IP1[1];

endadr[2]=strtadr[2]=IP1[2];

endadr[3]=pow(2,hostidbits)-1;

noofsub=pow(2,netidbits);
noofhost=pow(2,hostidbits);

usablehost=noofhost-2;

printf("\nClass: %c\nApplication: %s",cls,application);

printf("\nDefault Mask: ");

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

if(i!=3)

printf("%d.",defmask[i]);

else

printf("%d",defmask[i]);

printf("\nCustom Subnet Mask: ");

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

if(i!=3)

printf("%d.",cusmask[i]);

else

printf("%d",cusmask[i]);

printf("\nTotal Number of Subnets: %ld\nNumber of Host Addresses: %ld\nNumber of Usable Host


Address: %d\nNumber of Bits Borrowed: %d",noofsub,noofhost,usablehost,netidbits);

printf("\nSubnet Address: ");

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

if(i!=3)

printf("%d.",strtadr[i]);

else

printf("%d",strtadr[i]);

printf("\nUsable Host Address Range: ");

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

if(i!=3)

printf("%d.",strtadr[i]);

else

printf("%d",strtadr[i]+1);

printf(" - ");

for(int i=0;i<4;i++)
if(i!=3)

printf("%d.",endadr[i]);

else

printf("%d",endadr[i]-1);

printf("\nBroadcast Address: ");

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

if(i!=3)

printf("%d.",endadr[i]);

else

printf("%d",endadr[i]);

printf("\n");

int main()

int ch;

printf(" WELCOME ");/* */

printf("\n19BCE0713 NETCOM LAB FAT \n");

printf("\nMenu Driven Program For\n\t1. no.hosts\n\t2. Finding Classes for Given ip address\n\t3.
Finding Default SubnetMasks\n\t4. Finding Custom Subnet Masks etc.. \n");

printf("\nChoose Any One of The Below As Per Requirement\n");/* */

printf("\n\t1. Classful Addressing\n\t2. Classless Addressing\n\nEnter your choice: ");

scanf("%d",&ch);

switch(ch)

case 1:

classful();

break;

case 2:

classless();

break;

default:
printf("Invalid Choice!!!");

break;

return 0;

OUTPUT :
b) Develop a TCP client - server program to find the factorial of a given

number. The client program send any integer number to the client and

the server program reads and compute the factorial value of the

received number and display the same at client side.


CODE:

#server.py

import socket

import argparse

class Fact:

def init(self,a):

self.p=a

def factorial(self):
s=1

while self.p>0:

s=s*self.p

self.p=self.p-1

return s

class Host:

def init(self,h,p):

self.host=h

self.port=int(p)

def job(self):

try:

# create a socket object

with socket.socket(socket.AF_INET,socket.SOCK_STREAM) as s:

#Bind the socket to address

s.bind((self.host,self.port))

#listen() enables a server to accept connections. listen() has a backlog parameter. It specifies the
number of unaccepted connections that the system will allow before refusing new connections.

s.listen()

#accept() accepts a connection and return value is a pair (conn, address) where conn is a new socket
object usable to send and receive data on the connection, and address is the address bound to the
socket on the other end of the connection.

connection,address=s.accept()

print('host has started')

with connection:

print('address is:',address)

while True:

#recv() function is used to Receive data from the socket no more than 1024 bytes

data=connection.recv(1024)

if not data:

break

a=int.from_bytes(data,byteorder='big')
j=Fact(a)

jc=j.factorial()

data=jc.to_bytes(2,byteorder='big')

connection.sendall(data)

s.close()

except:

print('error establishing the host')

#close() function is used to close the socket

s.close()

class CommandLineParser:

def call(self):

parser = argparse.ArgumentParser(description="Script to get host and port")

required_arguments = parser.add_argument_group('Required command line arguments')

required_arguments.add_argument("--host", nargs='*', required=True, help="enter the host")

required_arguments.add_argument("--port", nargs='*', required=True, help="enter the port


number")

args = parser.parse_args()

conn_obj=Host(args.host[0],args.port[0])

conn_obj.job()

def main():

CommandLineParser()()

if name=="main":

main()

Client :

#client.py

import socket

import argparse

class Client:

def init(self,h,p,n):
self.host=h

self.port=int(p)

self.nu=int(n)

def job(self):

with socket.socket(socket.AF_INET,socket.SOCK_STREAM) as s:

s.connect((self.host,self.port))

b=self.nu

s.sendall(b.to_bytes(2,byteorder='big'))

data=s.recv(1024)

print('factorial is:',int.from_bytes(data, byteorder='big'))

s.close()

class CommandLineParser:

def call(self):

parser = argparse.ArgumentParser(description='Script to get host and port')

required_arguments = parser.add_argument_group('Required command line arguments')

required_arguments.add_argument("--host", nargs='*', required=True, help="enter the host")

required_arguments.add_argument("--port", nargs='*', required=True, help="enter the port


number")

required_arguments.add_argument("--number", nargs='*', required=True, help="enter the number


to get the factorial")

args = parser.parse_args()

conn_obj=Client(args.host[0],args.port[0],args.number[0])

conn_obj.job()

def main():

CommandLineParser()()

if name=="main":

main()

You might also like