0% found this document useful (0 votes)
12 views33 pages

4 To 13 CN

Computer network practicals

Uploaded by

Ritesh Kolate
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views33 pages

4 To 13 CN

Computer network practicals

Uploaded by

Ritesh Kolate
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 33

Practical No: 4

Output:
Practical 5
#include<stdio.h>
#include<conio.h>
void main()
{
int data[7],rec[7],i,c1,c2,c3,c;
printf("Hamming code is for message of 4 bits in size \nenter 4 bit message bit one by one:");
scanf("%d%d%d%d",&data[0],&data[1],&data[2],&data[4]);
data[6]=data[0]^data[2]^data[4];
data[5]=data[0]^data[1]^data[4];
data[3]=data[0]^data[1]^data[2];
printf("\nThe encoded bits are given below: \n");
for (i=0;i<7;i++)
{
printf("%d ",data[i]);
}
printf("\nEnter the received data bits one by one: ");

for (i=0;i<7;i++)
{
scanf("%d",&rec[i]);
}
c1=rec[6]^rec[4]^rec[2]^rec[0];
c2=rec[5]^rec[4]^rec[1]^rec[0];
c3=rec[3]^rec[2]^rec[1]^rec[0];

c=c3*4+c2*2+c1*1 ;
if(c==0)
{
printf("\ncongratulations there is no error: ");
}
else
{
printf("\nError on the position: %d\nthe corrected bit message is at the location %d is: \n",c,c);
if(rec[7-c]==0)
rec[7-c]=1;
else
rec[7-c]=0;

{
printf("%d ",rec[c]);
}
}
printf("\nThe Corrected message which is recieved by reciever after correcting bit:\n");
for (i=0;i<7;i++)
{
printf("%d ",rec[i]);
}

getch();
}

Output:
Practical No:6

#include <stdio.h>
#include <conio.h>
#include <string.h>
void main()
{
int i,j,keylen,msglen;
char input[100], key[30],temp[30],quot[100],rem[30],key1[30];
//clrscr();
printf("Enter Data: ");
gets(input);
printf("Enter Key: ");
gets(key);
keylen=strlen(key);
msglen=strlen(input);
strcpy(key1,key);

for (i=0;i<keylen-1;i++)
{
input[msglen+i]='0';
}
for (i=0;i<keylen;i++)
temp[i]=input[i];
for (i=0;i<msglen;i++)
{
quot[i]=temp[0];
if(quot[i]=='0')
for (j=0;j<keylen;j++)
key[j]='0';

else
for (j=0;j<keylen;j++)
key[j]=key1[j];
for (j=keylen-1;j>0;j--) {
if(temp[j]==key[j])
rem[j-1]='0'; else
rem[j-1]='1'; }
rem[keylen-1]=input[i+keylen];
strcpy(temp,rem);
}
strcpy(rem,temp);
printf("\nQuotient is ");
for (i=0;i<msglen;i++)
printf("%c",quot[i]);
printf("\nRemainder is ");
for (i=0;i<keylen-1;i++)
printf("%c",rem[i]);
printf("\nFinal data is: ");
for (i=0;i<msglen;i++)
printf("%c",input[i]);
for (i=0;i<keylen-1;i++)
printf("%c",rem[i]);
getch();
}
Output:
Practical No 7

#include<stdio.h>

int main()
{
int w,i,f,frames[50];

printf("Enter window size(w): ");


scanf("%d",&w);

printf("\nEnter number of frames to transmit(f): ");


scanf("%d",&f);

printf("\nEnter %d frames: ",f);

for(i=1;i<=f;i++)
scanf("%d",&frames[i]);

printf("\nWith sliding window protocol the frames will be sent in the following manner (assuming no
corruption of frames)\n\n");
printf("After sending %d frames at each stage sender waits for acknowledgement sent by the
receiver\n\n",w);

for(i=1;i<=f;i++)
{
if(i%w==0)
{
printf("%d\n",frames[i]);
printf("Acknowledgement of above frames sent is received by sender\n\n");
}
else
printf("%d\n ",frames[i]);
}

if(f%w!=0)
printf("\nAcknowledgement of above frames sent is received by sender\n");

return 0;
}

Output:
Practical No:8
#include<stdio.h>
void main()
{
int a[9][9],b[9][9],min,i,j,k=0,p,c,sum=0,m[9],n=1,s,r,l1,l,h;
char ch[10]={'A','B','C','D','E','F','G','H'};
printf("Enter no of nodes:");
scanf("%d",&h);
printf("Enter path matrix:");
for(i=0;i<h;i++)
{
for(j=0;j<h;j++)

scanf("%d",&b[i][j]);
}
printf("Path Matrix:\n");
for(i=0;i<h;i++)
{
printf("\n");
for(j=0;j<h;j++)
printf(" %d",b[i][j]);
}
j=0;
m[0]=0;
while(k!=3)
{
min=500;
l=-1;
l1=0;
for(i=0;i<4;i++)
{
if((b[k][i]!=0)&&(b[k][i]!=100))
{
if(i==3)
{
min=b[k][i];
p=i;
break;
}
else
{
c=b[k][i];
if(c<min)
min=c;
if(min==c)
{
if(l1!=c)
b[k][l]=100;
l=i;
l1=c;
p=i;
b[i][k]=100;
}
else
{
b[k][i]=100;
b[i][k]=100;
}
}
}
}
m[n]=p;
n++;
k=p;
sum=sum+min;
}
printf("\nANS: Minimum Distance= %d",sum);
printf("\nPath: ");
for(i=0;i<n;i++)
{
printf(" %c",ch[m[i]]);
}
}

Output:
Practical No:9
Experiment No:10

Ip Addressing Table for PCs:


Step 2: Configure the PCs (hosts) with IPv4 address and Subnet Mask according to the IP addressing
table given above.
#include<stdio.h>
int main()
{
int a,b,c,d;
printf("\nEnter the first byte");
scanf("%d",&a);
printf("\nEnter the secod byte");
scanf("%d",&b);
printf("\nEnter third byte");
scanf("%d",&c);
printf("\nEnter the fourth byte");
scanf("%d",&d);
if (a<=255 && b<=255 && c<=255 && d<=255)
{
printf("\nThe IP address is %d.%d.%d.%d",a,b,c,d);
}
else
{
printf("\n Invalid IP \n");
return 0;
}
if(a<=127)
{
printf("\nClass A");
}
else if(a>127&&a<=191)
{
printf("\nClass B");
}
else if(a>191&&a<=223)
{
printf("\nClass C");
}
else if(a>223&&a<=239)
{
printf("\nClass D");
}
else
{
printf("\nClass E");
}
}

Output:
Experiment No 11:
Experiment No 12
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
int main(){
char *ip = "127.0.0.1";
int port = 5566;
int server_sock, client_sock;
struct sockaddr_in server_addr, client_addr;
socklen_t addr_size;
char buffer[1024];
int n;
server_sock = socket(AF_INET, SOCK_STREAM, 0);
if (server_sock < 0){
perror("[-]Socket error");
exit(1);
}
printf("[+]TCP server socket created.\n");
memset(&server_addr, '\0', sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_port = port;
server_addr.sin_addr.s_addr = inet_addr(ip);
n = bind(server_sock, (struct sockaddr*)&server_addr, sizeof(server_addr));
if (n < 0){
perror("[-]Bind error");
exit(1);
}
printf("[+]Bind to the port number: %d\n", port);
listen(server_sock, 5);
printf("Listening...\n");
while(1){
addr_size = sizeof(client_addr);
client_sock = accept(server_sock, (struct sockaddr*)&client_addr, &addr_size);
printf("[+]Client connected.\n");
bzero(buffer, 1024);
recv(client_sock, buffer, sizeof(buffer), 0);
printf("Client: %s\n", buffer);
bzero(buffer, 1024);
strcpy(buffer, "HI, THIS IS SERVER. HAVE A NICE DAY!!!");
printf("Server: %s\n", buffer);
send(client_sock, buffer, strlen(buffer), 0);
close(client_sock);
printf("[+]Client disconnected.\n\n");
}
return 0;
}
Experiment No 13
1)
#include<stdio.h>
#include<netinet/in.h>
#include<unistd.h>
#include<sys/socket.h>
#include<sys/types.h>
#include<string.h>
#include<netdb.h>
#include<arpa/inet.h>

int main()
{
struct sockaddr_in sock;
int fd,i,sa,j;
char str1[20];
int in;

fd = socket(PF_INET,SOCK_DGRAM,0);
sock.sin_family = AF_INET;
sock.sin_addr.s_addr = inet_addr("127.0.0.1");
sock.sin_port = htons(3000);

sa = sizeof(sock);

bind(fd,(struct sockaddr*)&sock,sizeof(sock));

while(1)
{
printf("\n\n\tWaiting for the data.....");
recvfrom(fd,str1,sizeof(str1),0,(struct sockaddr*)&sock,&sa);
if(strcmp(str1,"###") == 0)
{
printf("\n\n\tConnection closed.....\n");
break;
}

printf("\n\n\tData is : %s",str1);
in = atoi(str1);
j=1;
for(i=1;i<=in;i++)
{
j = j*i;
}
printf("\n\n\tFactorial of the received data is : %d",j);

}
close(fd);
return 0;
}

2)
#include<netinet/in.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<stdio.h>
#include<unistd.h>
#include<string.h>
#include<netdb.h>

int fd;
int main()
{
struct sockaddr_in sock;
char data[20];

fd = socket(PF_INET,SOCK_DGRAM,0);
sock.sin_family = AF_INET;
sock.sin_addr.s_addr = inet_addr("127.0.0.1");
sock.sin_port = htons(3000);

while(1)
{
if(strcmp(data,"###") == 0)
{
printf("\n\tConnection closed....\n");
break;
}
printf("\n\tEnter the data to be transmit :");
scanf("%s",data);
printf("\n");
sendto(fd,data,sizeof(data),0,(struct sockaddr*)&sock,sizeof(sock));
printf("\n\tdata is : %s\n",data);

close(fd);
}

You might also like