MULTI USER CHAT 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,len,sport;
 struct sockaddr_in ser,cli;
 char smsg[20],rmsg[20];
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;
}
do
{
printf("\n\t Enter the client no to be communicate : ");
scanf("%d",&i);
 if(i==0)
   exit(0);
printf("\n\t Client %d is connected",i);
clilen=sizeof(cli);
if((sd1=accept(sd,(struct sockaddr*)&cli,&clilen)) <0)
{
 printf("\n\t Error in Accept");
 return 0;   }

 printf("\n\t Accepted ");
  do
 {
  recv(sd1,rmsg,20,0);
  printf("\n\t Clinet Msg Received : %s ",rmsg);
 printf("\n\t Enter Server String : ");
 scanf("%s",smsg);
 send(sd1,smsg,20,0);
 wait(20);
 } while((strcmp(smsg,"exit"))!=0);
} while(i!=0);
  close(sd);
}

Client :1  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,len,cport;
struct sockaddr_in ser;
char smsg[20],rmsg[20];
printf("\n\t Enter the Port : ");
scanf("%d",&cport);
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(cport);
ser.sin_addr.s_addr=htonl(INADDR_ANY);

if(connect(sd,(struct sockaddr*)&ser,sizeof(ser)) <0)
{
  printf("\n\t Error in connect");
  exit(-1);
}

do
{

 printf("\n\t Enter the Client String :");
 scanf("%s",smsg);
 send(sd,smsg,20,0);
 wait(20);
 recv(sd,rmsg,20,0);
 printf("\n\t  Server Msg Received : %s ",rmsg);
}
while((strcmp(rmsg,"exit"))!=0);
close(sd);
}

Client :2  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,len,cport;
struct sockaddr_in ser;
char smsg[20],rmsg[20];
printf("\n\t Enter the Port : ");
scanf("%d",&cport);
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(cport);
ser.sin_addr.s_addr=htonl(INADDR_ANY);

if(connect(sd,(struct sockaddr*)&ser,sizeof(ser)) <0)
{
  printf("\n\t Error in connect");
  exit(-1);
}

do
{

 printf("\n\t Enter the Client String :");
 scanf("%s",smsg);
 send(sd,smsg,20,0);
 wait(20);
 recv(sd,rmsg,20,0);
 printf("\n\t  Server Msg Received : %s ",rmsg);
}
while((strcmp(rmsg,"exit"))!=0);
close(sd);
}


OUTPUT

SERVER

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

         Enter the Port : 3326

         Enter the client no to be communicate : 1
         Client 1 is connected
         Accepted
         Clinet Msg Received : hi
         Enter Server String : how_are_you
         Clinet Msg Received : i_am_fine
         Enter Server String : exit

         Enter the client no to be communicate : 2
         Client 2 is connected
         Accepted
         Clinet Msg Received : hi
         Enter Server String : how_are_you
         Clinet Msg Received : i_am_boss
         Enter Server String : exit

         Enter the client no to be communicate : 0
[11ca013@mcalinux network]$

CLIENT:1

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

         Enter the Port : 3326

         Enter the Client String :hi
          Server Msg Received : how_are_you
         Enter the Client String :i_am_fine
          Server Msg Received : exit

CLIENT:2

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

         Enter the Port : 3326

         Enter the Client String :hi

          Server Msg Received : how_are_you
         Enter the Client String :i_am_boss
          Server Msg Received : exit


SIMULATION OF ROUTING PROTOCOL (OSPF)

Program

#include<stdio.h>
#include<string.h>
int main()
{
int count,src_router,i,j,k,w,v,min;
int cost_matrix[100][100],dist[100],last[100];
int flag[100];
printf("\nENTER THE NO OF ROUTERS:");
scanf("%d",&count);
printf("\nENTER THE COST MATRIX VALUES:");
for(i=0;i<count;i++)
{
 for(j=0;j<count;j++)
{
printf("\n%d->%d:",i,j);
scanf("%d",&cost_matrix[i][j]);
if(cost_matrix[i][j]<0)
cost_matrix[i][j]=1000;
}
}


printf("\n\t The COST MATRIX is : \n ");
for(i=0;i<count;i++)
  {
 for(j=0;j<count;j++)
  printf("%d\t",cost_matrix[i][j]);
  printf("\n");
   }
printf("\n ENTER THE SOURCE ROUTER:");
scanf("%d",&src_router);
for(v=0;v<count;v++)
  {
flag[v]=0;
last[v]=src_router;
dist[v]=cost_matrix[src_router][v];
  }
 flag[src_router]=1;
for(i=0;i<count;i++)
{
min=1000;
for(w=0;w<count;w++)
      {
if(!flag[w])
if(dist[w]<min)
{
v=w;
min=dist[w];
      }
  }
flag[v]=1;
for(w=0;w<count;w++)
            {
if(!flag[w])
if((min+cost_matrix[v][w])<dist[w])
{
dist[w]=min+cost_matrix[v][w];
last[w]=v;
  }    }   }
for(i=0;i<count;i++)
{
printf("\n%d==>%d:PATH TAKEN:%d",src_router,i,i);
w=i;
while(w!=src_router)
{
printf("\n<--%d",last[w]);
w=last[w];
}
printf("\nSHORTEST PATH COST:%d\n",dist[i]);
}
return(0);
}











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

ENTER THE NO OF ROUTERS:3

ENTER THE COST MATRIX VALUES:
0->0:100

0->1:1

0->2:3

1->0:1

1->1:100

1->2:1

2->0:1

2->1:1

2->2:100

         The COST MATRIX is :
 100    1       3
1       100     1
1       1       100

 ENTER THE SOURCE ROUTER:2

2==>0:PATH TAKEN:0
<--2
SHORTEST PATH COST:1

2==>1:PATH TAKEN:1
<--2
SHORTEST PATH COST:1

2==>2:PATH TAKEN:2

SHORTEST PATH COST:100

SIMPLE SOCKET CREATION PROGRAMS

#include <sys/socket.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
main(int argc,char *argv[])
{
int i,pid,ppid,c,magic,sk[2];
struct
{
float t,h;
}num;
socketpair(AF_UNIX,SOCK_STREAM,0,sk);
if(fork()==0)
{
close(sk[0]);
pid=getpid();
while(read(sk[1],&magic,sizeof(magic)) >0)
{
printf("child [%d] the magic number is %d \n:",pid,magic);
num.t=magic*magic;
num.h=num.t*magic;
write(sk[1],&num,sizeof(num));
}
close(sk[1]);
exit(1);
}
close(sk[1]);
ppid=getppid();
for(i=1;i<argc;i++)
{
magic=atoi(argv[i]);
write(sk[0],&magic,sizeof(magic));
}
for(i=1;i<argc;i++)
{
if(read(sk[0],&num,sizeof(num)) < 0)
break;
printf("Parent [%d] square = %f,cube = %f \n",ppid,num.t,num.h);
}
close(sk[0]);   }
OUTPUT

 [11ca013@mcalinux network]$ cc exno2a.c
[11ca013@mcalinux network]$ ./a.out 3 4 5 6
child [22746] the magic number is 3
child [22746] the magic number is 4                                               
child [22746] the magic number is 5
child [22746] the magic number is 6
Parent [4041] square = 9.000000,cube = 27.000000
Parent [4041] square = 16.000000,cube = 64.000000
Parent [4041] square = 25.000000,cube = 125.000000

Parent [4041] square = 36.000000,cube = 216.000000

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



Your IP Address is:

Browser: