Student Record Management System - Client-Server Program - Network-DBMS Lab - C program

Program:

// Client Program: sttcpc.c

#include<stdio.h>
#include<string.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<stdlib.h>

struct
{
    int roll_no;
    char name[20];
    int mark;
}student;
       
main()
{
   struct sockaddr_in client;
   int s,flag,choice,shift,rollnumber,found,continu;
   char buffer[20];
  
  
   s=socket(AF_INET,SOCK_STREAM,0);
   client.sin_family=AF_INET;
   client.sin_port=2000;
   client.sin_addr.s_addr=inet_addr("127.0.0.1");
   connect(s,(struct sockaddr *)&client,sizeof client);
  
   do{
      printf("\n\t\t<<< STUDENT FILE MANAGEMENT SYSTEM>>>\n\t1 -> Store a new record in server\n\t2 -> Search a student record from server\nEnter your choice: ");
      scanf("%d",&choice);
      send(s,&choice,sizeof(int),0);
      switch(choice)
      {
       case  1:
       printf("\nEnter Student Details:\nRoll number (Integer): ");
       scanf("%d",&student.roll_no);
       printf("\nName : ");
       scanf("%s",student.name);
       printf("\nMark : ");
       scanf("%d",&student.mark);
       send(s,(const char*)&student,sizeof(student),0);
       printf(" A student record has been sent successfully...\n");
       printf("\n press 1 -> conitnue,\n\t 2 -> Exit: ");
       scanf("%d",&shift);
       send(s,&shift,sizeof(int),0);
      
       if(shift==1)
        break;
       else if(shift==2)
        {
            continu=0;
            break;
        }
               
       case 2:
       printf("\nEnter Roll number: ");
       scanf("%d",&rollnumber);
       send(s,&rollnumber,sizeof(int),0);
       recv(s,&found,sizeof(int),0);
       if(found)
       {
          recv(s,(void *)&student,sizeof(student),0);
         printf("\nThe record is found.\nRoll no: %d\nName: %s\nMark: %d \n",student.roll_no,student.name,student.mark);
       }
       else
       {
         printf("Not found...\n");
         exit(0);
       }
       printf("\n press 1 -> conitnue,\n\t 2 -> Exit: ");
       scanf("%d",&shift);
       send(s,&shift,sizeof(int),0);
       if(shift==1)
        break;
       else if(shift==2)
       {
            continu=0;
            break;
       }
       default :
          printf(" Bad choice...Exiting...\n");
          continu=0;
        break;
        }
      }while(continu!=0);
           
      close(s);
}

// Server Program: sttcps.c

#include<stdio.h>
#include<string.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<stdlib.h>

struct
{
    int roll_no;
    char name[20];
    int mark;
}student;

main()
{
  int s,n,sock,choice,found,rollnumber,shift,continu;
  char buffer[20];
  FILE *fp;
  struct sockaddr_in client,server;
  s=socket(AF_INET,SOCK_STREAM,0);
  server.sin_family=AF_INET;
  server.sin_port=2000;
  server.sin_addr.s_addr=inet_addr("127.0.0.1");
  bind(s,(struct sockaddr *)&server,sizeof server);
  listen(s,1);
  n=sizeof client;
  sock=accept(s,(struct sockaddr *)&client,&n);
  printf("\n\t\t<<< STUDENT FILE MANAGEMENT SYSTEM>>>\n");
  do{
  recv(sock,&choice,sizeof(int),0);
  switch(choice)
  {
    case 1 :
   
        recv(sock,(void *)&student,sizeof(student),0);
        printf("\nNew student record received is:%d %s %d \n",student.roll_no,student.name,student.mark);
        fp=fopen("studentfile.txt","a+");
        fprintf(fp,"\n%d\t%s\t%d\t",student.roll_no,student.name,student.mark);
        fclose(fp);
        printf(" A student record has been added successfully...\n");
        recv(sock,&shift,sizeof(int),0);
        if(shift==1)
        break;
        else if(shift==2)
        {
                continu=0;
                break;
        }
      
    case 2:
           recv(sock,&rollnumber,sizeof(int),0);
           printf("Searching record with rollnumber=%d.\n",rollnumber);
           found=0;
           if((fp=fopen("studentfile.txt","r"))==NULL)
        {
            printf(" ! The File is Empty...\n\n");
            send(sock,&found,sizeof(int),0);
        }
        else
        {
            while(!feof(fp)&& found==0)
                {
                fscanf(fp,"\n%d\t%s\t%d\t",&student.roll_no,student.name,&student.mark);
                if(student.roll_no==rollnumber)
                    found=1;
            }
            send(sock,&found,sizeof(int),0);
                 fclose(fp);
                 if(found)
            {
                printf(" Record found...\n");
                send(sock,(const char*)&student,sizeof(student),0);
               
            }
            else
            {
                printf(" Record not found...Exiting\n");
                exit(0);
            }   
           
        }
        recv(sock,&shift,sizeof(int),0);
        if(shift==1)
        break;
        else if(shift==2)
        {
                continu=0;
                break;
        }
    default :
          printf(" Bad request...Exiting...\n");
          continu=0;
        break;
  }
  }while(continu!=0);
  
  close(sock);
  close(s);
}

Output:

Terminal 1: (Client)
nn@linuxmint ~ $ gcc sttcpc.c -o client
nn@linuxmint ~ $ ./client

        <<< STUDENT FILE MANAGEMENT SYSTEM>>>
    1 -> Store a new record in server
    2 -> Search a student record from server
Enter your choice: 1

Enter Student Details:
Roll number (Integer): 1

Name : nidheesh               

Mark : 45
 A student record has been sent successfully...

 press 1 -> conitnue,
     2 -> Exit: 1

        <<< STUDENT FILE MANAGEMENT SYSTEM>>>
    1 -> Store a new record in server
    2 -> Search a student record from server
Enter your choice: 1

Enter Student Details:
Roll number (Integer): 2

Name : nithin

Mark : 45
 A student record has been sent successfully...

 press 1 -> conitnue,
     2 -> Exit: 1

        <<< STUDENT FILE MANAGEMENT SYSTEM>>>
    1 -> Store a new record in server
    2 -> Search a student record from server
Enter your choice: 1

Enter Student Details:
Roll number (Integer): 3

Name : asish

Mark : 45
 A student record has been sent successfully...

 press 1 -> conitnue,
     2 -> Exit: 1

        <<< STUDENT FILE MANAGEMENT SYSTEM>>>
    1 -> Store a new record in server
    2 -> Search a student record from server
Enter your choice: 2

Enter Roll number: 3

The record is found.
Roll no: 3
Name: asish
Mark: 45

 press 1 -> conitnue,
     2 -> Exit: 1

        <<< STUDENT FILE MANAGEMENT SYSTEM>>>
    1 -> Store a new record in server
    2 -> Search a student record from server
Enter your choice: 2

Enter Roll number: 5
Not found...
nn@linuxmint ~ $

Terminal 2: (Server)

nn@linuxmint ~ $ gcc sttcps.c -o server
nn@linuxmint ~ $ ./server

        <<< STUDENT FILE MANAGEMENT SYSTEM>>>

New student record received is:1 nidheesh 45
 A student record has been added successfully...

New student record received is:2 nithin 45
 A student record has been added successfully...

New student record received is:3 asish 45
 A student record has been added successfully...
Searching record with rollnumber=3.
 Record found...
Searching record with rollnumber=5.
 Record not found...Exiting
nn@linuxmint ~ $

File : studentfile.txt



1    nidheesh    45   
2    nithin    45   
3    asish    45

1 comment:

Related Posts Plugin for WordPress, Blogger...