Professional Documents
Culture Documents
Network and Communication CSE1004: Final Lab Assignment
Network and Communication CSE1004: Final Lab Assignment
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.
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;
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));
cls='A';
netidbits=8;
hostidbits=24;
strcpy(application,"Unicast");
noofnet=pow(2,7);
addrpernet=pow(2,24);
endadr[0]=127;
defmask[0]=255;
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;
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;
cls='D';
strcpy(application,"Multicast");
strtadr[0]=224;
endadr[0]=239;
cls='E';
strcpy(application,"Reserved");
strtadr[0]=240;
for(int i=0;i<4;i++)
if(i!=3)
printf("%d.",defmask[i]);
else
printf("%d",defmask[i]);
for(int i=0;i<4;i++)
if(i!=3)
printf("%d.",strtadr[i]);
else
printf("%d",strtadr[i]);
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};
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];
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;
}
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;
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;
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;
for(int i=0;i<4;i++)
if(i!=3)
printf("%d.",defmask[i]);
else
printf("%d",defmask[i]);
for(int i=0;i<4;i++)
if(i!=3)
printf("%d.",cusmask[i]);
else
printf("%d",cusmask[i]);
for(int i=0;i<4;i++)
if(i!=3)
printf("%d.",strtadr[i]);
else
printf("%d",strtadr[i]);
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);
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("\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");
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
#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:
with socket.socket(socket.AF_INET,socket.SOCK_STREAM) as s:
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()
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:
s.close()
class CommandLineParser:
def call(self):
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)
s.close()
class CommandLineParser:
def call(self):
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()