SLIDING WINDOW PROTOCOL USING SOCKETS

SERVER PROGRAM

#include <sys/types.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>

int main()
{
        int sock,size,connect;
        char senddata[50],data[50];
        int val,count,i,port;
        struct sockaddr_in ser,cli;
        printf("\n\n Server Running ...... ");
        if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1)
        {
           perror("\n Socket Creation Error");
            exit(-1);
        }
        printf("\nEnter the port number  : ");
        scanf("%d",&port);
        ser.sin_family = AF_INET;
        ser.sin_port = htons(port);
        ser.sin_addr.s_addr=INADDR_ANY;
        bzero(&(ser.sin_zero),8);
if(bind(sock,(struct sockaddr *)&ser,sizeof(struct sockaddr)) == -1)
        {
            perror("\n\t Error in Bind");
            exit(-1);
        }
if (listen(sock,2)==-1)
        {
             perror("\n\t Error in Listen");
             exit(-1);
        }
printf("\n\t Waiting for connection ");
   size=sizeof(struct sockaddr);
   connect=accept(sock,(struct sockaddr *)&cli,&size);
if(connect==-1)
    {
      perror("\n\t Connection Failed :");
      exit(-1);
    }
    printf("\n\t Connected Successfully");
    printf("\n");
        // get the pocket number from client
    recv(connect,&val,sizeof(val),0);
    count=val;
      while(1)
      {
            i=recv(connect,&data,sizeof(data),0);
            data[i]='\0';
            if (strcmp(data,"end")==0)
           {
               printf("\n\t Finished");
               break;
           }
           if(count!=val)
           {
            strcpy(senddata,"packet missing");
                send(connect,&count,sizeof(count),0);
                send(connect,senddata,strlen(senddata),0);
            }
          else
          {
             printf("\n The packet Number is : %d",val);
             printf("\n The data is :%s",data);
             count++;
             strcpy(senddata,"send nextdata");
             send(connect,&count,sizeof(count),0);
             send(connect,senddata,strlen(senddata),0);
          }

        printf("\n The Expected Packet now is: %d \n",count);
        recv(connect,&val,sizeof(val),0);
     }
close(connect);
close(sock);
return 0;
}
CLIENT PROGRAM

#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>

int main()
{
        int sock,val,i,count,port;
        char recvdata[50],sentdata[50];
        struct sockaddr_in server_addr;
        printf("\n\n Client Running ...... ");
        if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1)
        {
            perror("Socket");
            exit(1);
        }
        printf("\nEnter the port number");
        scanf("%d",&port);
        server_addr.sin_family = AF_INET;
        server_addr.sin_port = htons(port);
        server_addr.sin_addr.s_addr= htonl(INADDR_ANY);
        bzero(&(server_addr.sin_zero),8);
        if (connect(sock, (struct sockaddr *)&server_addr, sizeof(struct sockadd
r)) == -1)
        {
            perror("Connect");
            exit(1);
        }
        while(1)
        {
                //get the pack number from client
                printf("\n Enter packet number ");
                scanf("%d",&val);
                // sent the value to server
                send(sock,&val,sizeof(val),0);
                // get the data from the user
                printf("\n\n Enter data ");
                scanf("%s",sentdata);
                // sent the to server
                send(sock,sentdata,strlen(sentdata),0);
                   if(strcmp(sentdata,"end")==0)
                   break;
                // recev the result from server
                recv(sock,&count,sizeof(count),0);
                i=recv(sock,recvdata,50,0);
                recvdata[i]='\0';
                printf("\n %s %d",recvdata,count);
        }
        close(sock);
        return 0;
}

OUTPUT
SERVER

[11ca013@mcalinux network]$ cc 17slideser.c
[11ca013@mcalinux network]$  ./a.out


 Server Running ......
Enter the port number  : 3326

         Waiting for connection
         Connected Successfully

 The packet Number is : 18
 The data is :google
 The Expected Packet now is: 19

 The packet Number is : 19
 The data is :yahoo
 The Expected Packet now is: 20

 The Expected Packet now is: 20

 The packet Number is : 20
 The data is :ok
 The Expected Packet now is: 21






CLIENT

[11ca013@mcalinux network]$ cc 17slidecli.c
[11ca013@mcalinux network]$ ./a.out

 Client Running ......
Enter the port number3326
 Enter packet number 18


 Enter data google

 send nextdata 19
 Enter packet number 19


 Enter data yahoo

 send nextdata 20
 Enter packet number 26

 Enter data hi

 packet missing 20
 Enter packet number 20


 Enter data ok

SHORTEST PATH ROUTING ALGORITHM (DIJKSTRA ALGORITHM)

Program

#include<stdio.h>
#define INF 999
int main()
{
int i,j,k,n,src,des,index;
int dis[10][10],cost[10][10],path[10][10][20],size[20][20];
char ch;
printf("\n\n\t PROGRAM IMPLEMENTATION TO FIND THE SHORTEST PATH  ");
printf("\n\n\t ============================================== ");
printf("\n\n Enter the Number of vertices    :  ");
scanf("%d",&n);
//Gettig the cost of the paths
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
printf("\n\n Enter the cost from the path %d to %d   :",i,j);
scanf("%d",&cost[i][j]);
if(cost[i][j] == 0)
{
dis[i][j]=INF;
}
else
{
dis[i][j]=cost[i][j];
}
path[i][j][1]=i;
path[i][j][2]=j;
size[i][j]=2;
}
}
printf("\n\t****Adjacency Matrix****\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
printf("\t%d",dis[i][j]);
}
printf("\n");
}
for(k=1;k<=n;k++)
{
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
//If the cost is less than the previous cost
if(dis[i][j]>dis[i][k]+dis[k][j])
{
dis[i][j]=dis[i][k]+dis[k][j];

//update path by isnerting the vertex and the size
size[i][j]=size[i][j]+1;
index=size[i][j];
path[i][j][index]=path[i][j][index-1];
path[i][j][index-1]=k;
}
}
}
}

do
{
printf("\n\n Enter the source Vertex       :       ");
scanf("%d",&src);
printf("\n\n Enter the destination vertex  :       ");
scanf("%d",&des);
printf("\n\n The shortest Path from %d to %d is   : %d",src,des,dis[src][des]);
printf("\n\n The Shortest root from the source vertex %d to the destination vert
ex %d is   :\n",src,des);
for(k=1;k<=size[src][des];k++)
{
printf("\t->%d",path[src][des][k]);
}

printf("\n\n Do you want to continue(y/n) :       ");
scanf(" %c",&ch);
} while(ch=='y');
return 0;
}


OUTPUT
[11ca013@mcalinux network]$ cc 16diji.c
[11ca013@mcalinux network]$  ./a.out


         PROGRAM IMPLEMENTATION TO FIND THE SHORTEST PATH
        =================================================

 Enter the Number of vertices    :  4
 Enter the cost from the path 1 to 1   :2
 Enter the cost from the path 1 to 2   :4
 Enter the cost from the path 1 to 3   :5
 Enter the cost from the path 1 to 4   :1
 Enter the cost from the path 2 to 1   :5
 Enter the cost from the path 2 to 2   :3
 Enter the cost from the path 2 to 3   :6
 Enter the cost from the path 2 to 4   :7
 Enter the cost from the path 3 to 1   :2
 Enter the cost from the path 3 to 2   :3
 Enter the cost from the path 3 to 3   :4
 Enter the cost from the path 3 to 4   :6
 Enter the cost from the path 4 to 1   :1
 Enter the cost from the path 4 to 2   :2
 Enter the cost from the path 4 to 3   :3
 Enter the cost from the path 4 to 4   :4

        ****Adjacency Matrix****
        2       4       5       1
        5       3       6       7
        2       3       4       6
        1       2       3       4


 Enter the source Vertex       :       3
 Enter the destination vertex  :       4
 The shortest Path from 3 to 4 is   : 3

 The Shortest root from the source vertex 3 to the destination vertex 4 is   :
        ->3     ->1     ->4


 Do you want to continue(y/n) :       n

ADDRESS RESOLUTION PROTOCOL (ARP) USING SOCKETS

Server program

#include<stdio.h>
#include<sys/socket.h>
#include<netdb.h>
#include<string.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<unistd.h>
#include<stdlib.h>
int main()
{
 int sd,sd1,i,clilen,byterecv,sport;
 struct sockaddr_in ser,cli;
 char smsg[1024],rmsg[1024];
char ip[5][50]={"192.1.1.134","192.1.1.135","192.1.1.136","192.1.1.137",
"192.1.138"};
char mac[5][50]={"00-01-6C-47-EB-D3","00-01-6C-47-EB-D4",
"00-01-6C-47-EB-D5","-01-6C-47-EB-D6","00-01-6C-47-EB-D7"};
printf("\n\t Enter the Port : ");
scanf("%d",&sport);
if((sd=socket(AF_INET,SOCK_STREAM,0))<0)
{
  printf("\n\t Error in socket");
  return 0;
}
bzero(&ser,sizeof(ser));
ser.sin_family=AF_INET;
ser.sin_port=htons(sport);
ser.sin_addr.s_addr=htonl(INADDR_ANY);
if(bind(sd,(struct sockaddr*)&ser,sizeof(ser)) <0)
{
  printf("\n\t Error in bind");
  return 0;
}
if(listen(sd,5) <0)
{
 printf("\n\t Error in Listen");
 return 0;
}
clilen=sizeof(cli);
if((sd1=accept(sd,(struct sockaddr*)&cli,&clilen)) <0)
{
 printf("\n\t Error in Accept");
 return 0;
}

 printf("\n\t Accepted ");
  while(1)
 {
  byterecv=recv(sd1,rmsg,1024,0);
  rmsg[byterecv]='\0';
  if(strcmp(rmsg,"exit")==0)
 {
  close(sd1);
  close(sd);
break;
}
else
for(i=0;i<5;i++)
 {
  if(strcmp(ip[i],rmsg)==0)
   {
   strcpy(smsg,mac[i]);
   printf("\nRecv ip address =%s",rmsg);
   printf("\nThe MAC address =%s",mac[i]);
   break;
   }
 }
 send(sd1,smsg,strlen(smsg),0);
  }
 printf("\n");
 return 0;
 }









Client program

#include<stdio.h>
#include<sys/socket.h>
#include<netdb.h>
#include<string.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<unistd.h>
#include<stdlib.h>
int main()
{
 int sd,sd1,i,clilen,byterecv,sport;
 struct sockaddr_in ser,cli;
 char smsg[1024],rmsg[1024];

printf("\n\t Enter the Port : ");
scanf("%d",&sport);
if((sd=socket(AF_INET,SOCK_STREAM,0))<0)
{
  printf("\n\t Error in socket");
  return 0;
}
bzero(&ser,sizeof(ser));
ser.sin_family=AF_INET;
ser.sin_port=htons(sport);
ser.sin_addr.s_addr=htonl(INADDR_ANY);
if(connect(sd,(struct sockaddr*)&ser,sizeof(ser)) <0)
{
  printf("\n\t Error in connect");
  return 0;
}

  while(1)
 {
  printf("\nEnter the ip address  " );
  scanf("%s",smsg);

  if(strcmp(smsg,"exit")!=0)
 {
  send(sd,smsg,strlen(smsg),0);
 }
 else
 {
 send(sd,smsg,strlen(smsg),0);
 close(sd);
 break;
 }
byterecv=recv(sd,rmsg,1024,0);
rmsg[byterecv]='\0';
printf("send mac address = %s",rmsg);
}
return 0;
}
OUTPUT

SERVER

[11ca013@mcalinux network]$ cc 10arpser.c
[11ca013@mcalinux network]$ ./a.out

         Enter the Port : 3326

         Accepted
Recv ip address =192.1.1.134
The MAC address =00-01-6C-47-EB-D3
Recv ip address =192.1.1.135
The MAC address =00-01-6C-47-EB-D4
Recv ip address =192.1.1.136
The MAC address =00-01-6C-47-EB-D5
Recv ip address =192.1.1.137
The MAC address =00-01-6C-47-EB-D6

CLIENT

[11ca013@mcalinux network]$ cc 11rarpcli.c
[11ca013@mcalinux network]$ ./a.out

         Enter the Port : 3326

Enter the MAC address  192.1.1.134
The recv IP address = 00-01-6C-47-EB-D3
Enter the MAC address  192.1.1.135
The recv IP address = 00-01-6C-47-EB-D4
Enter the MAC address  192.1.1.136
The recv IP address = 00-01-6C-47-EB-D5
Enter the MAC address  192.1.1.137
The recv IP address = 00-01-6C-47-EB-D6

Enter the MAC address  exit

ADDRESS CONVERSION FOR LOGICAL ADDRESS INTO PHYSICAL ADDRESS

Server program

#include <stdio.h>
#include <sys/types.h>
#include <sys/shm.h>
int main()
{
int shmid;  int a,i;
char *ptr,*shmptr;
shmid=shmget(1200,10,IPC_CREAT|0666);
shmptr=shmat(shmid,NULL,0); //attaching a ptr to shm area
ptr=shmptr;
for(i=0;i<3;i++)
{
puts("\n Enter the MAC:");
scanf("%s",ptr);
a=strlen(ptr);
ptr[a]=' ';
puts("\n Enter the IP:");
ptr=ptr+a+1;
scanf("%s",ptr);
a=strlen(ptr);
ptr[a]='\n';
ptr=ptr+a+1;
}
ptr[strlen(ptr)]='\0';
printf("\n THE ARP TABLE AT SERVER SIDE=\n %s",shmptr);
shmdt(shmptr);   //detach
return 0;
}









Client program

#include <stdio.h>
#include <sys/types.h>
#include <sys/shm.h>
int main()
{
int shmid;  int a;
char *ptr,*shmptr;
char ip[20],mac[20],ptr2[20];
shmid=shmget(1200,10,0666);
shmptr=shmat(shmid,NULL,0);
puts("THE ARP TABLE IS:");
printf("%s",shmptr);
printf("\n 1.ARP 2.RARP 3.EXIT\n");
scanf("%d",&a);
switch(a)
{
case 1:
puts("\n Enter the IP address:");
scanf("%s",ip);
ptr=strstr(shmptr,ip);
ptr-=8;
sscanf(ptr,"%s %*s",ptr2);
printf("\n MAC address:%s",ptr2);
break;
case 2:
puts("\n Enter the MAC address:");
scanf("%s",mac);
ptr=strstr(shmptr,mac);
sscanf(ptr,"%*s %s",ptr2);
printf("%s",ptr2);
break;
case 3:
exit(1);
}
}








OUTPUT

SERVER

[11ca013@mcalinux network]$ cc 10logarpser.c
 [11ca013@mcalinux network]$ ./a.out

 Enter the MAC:www.google.com
 Enter the IP:192.1.1.1
 Enter the MAC:www.yahoo.com
 Enter the IP:192.1.1.2
 Enter the MAC:www.zoho.com
 Enter the IP:192.1.1.3

 THE ARP TABLE AT SERVER SIDE=
 www.google.com 192.1.1.1
www.yahoo.com 192.1.1.2
www.zoho.com 192.1.1.3

CLIENT

 [11ca013@mcalinux network]$ cc 10logarpcli.c
THE ARP TABLE IS:
www.google.com 192.1.1.1
www.yahoo.com 192.1.1.2
www.zoho.com 192.1.1.3
 1.ARP 2.RARP 3.EXIT
2
 Enter the MAC address:

 Ip address is : 192.1.1.1



Your IP Address is:

Browser: