Database creation
# include <stdio.h>
#include<string.h>
#include<stdlib.h>
int main(void)
{
FILE *stud_file,*pass_file;
char chr;
char filename1[35];
int fee=200,rf=0,value=0;
char tempbfr[5];
struct stud {
char stud_id[5];
char DD[5];
char amount[5];
char ref[5];
} student;
struct studp {
char stud_id[5];
char pwd[5];
} studentp;
char answer;
int i=5;
stud_file = fopen("record","a+");
pass_file = fopen("pass","w");
while(i!=0)
{
printf("Enter the username \n");
scanf("%s",&studentp.stud_id);
printf("Enter the password \n");
scanf("%s",&studentp.pwd);
i=i-1;
fwrite(&studentp, sizeof(struct studp), 1, pass_file);
}
answer = 'Y';
while (!(answer == 'N'))
{
printf("\nEnter Student ID : ");
scanf("%s", &student.stud_id);
printf("\nEnter DD no. : ");
scanf("%s", &student.DD);
printf("\nEnter amount paid : ");
scanf("%s", &student.amount);
rf=atoi(student.amount);
printf("%d\n%d\n",rf,(fee-rf));
value=fee-rf;
sprintf(student.ref,"%d",value);
printf("%s\n",student.ref);
fwrite(&student, sizeof(struct stud), 1, stud_file);
printf("\n Do you want to insert another record ? Y/N");
scanf("%s", &answer);
}
fclose(stud_file);
fclose(pass_file);
return 0;
}
Client
#include <ctype.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <sys/time.h>
#include <sys/wait.h>
#include <stdio.h>
#include<string.h>
#include <stdlib.h>
#define SIZE sizeof(struct sockaddr_in)
int main(void)
{
char try[5];
int sockfd;
char c, rc,chk='0', d;
char k='1';
struct stud {
char stud_id[5];
char DD[5];
char amount[5];
char ref[5];
} student;
struct studp {
char stud_id[5];
char pwd[5];
} studentp;
char msg [1024];
char msg1 [1024];
char msg2 [1024];
char msg3 [1024];
char msg4 [1024];
char msg5 [1024];
char msg6 [1024];
struct sockaddr_in server = {AF_INET, 7000};
/* convert and store the server's IP address /
server.sin_addr.s_addr = inet_addr("10.100.91.61");
/ set up the transport end point /
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
perror("socket call failed");
exit(1);
}
/ connect the socket to the server's address */
if ( connect (sockfd, (struct sockaddr *)&server, SIZE) == -1)
{
perror("connect call failed");
exit(1);
}
for(;;){
if(recv(sockfd, msg4, sizeof(msg4), 0)<0){
printf("recieve failed");
continue;
}
else{
printf("%s \n",msg4);
scanf("%s",&student.stud_id);
if(send(sockfd,student.stud_id,sizeof(student.stud_id),0)>0)
printf("sent %s",student.stud_id);
}
if(recv(sockfd, msg, sizeof(msg), 0)<0){
printf("recieve failed");
continue;
}
else{
printf("%s \n",msg);
scanf("%s",msg);
if(send(sockfd,msg,sizeof(msg),0)>0)
printf("sent %s",msg);
}
recv\(sockfd,&chk,1,0\);
if\(chk =='1'\)\{
printf\("authentication success %c \\n ",chk\);
if\(recv\(sockfd,msg,sizeof\(msg\),0\)>0\)
\{
printf\("%s",msg\);
getchar\(\);
c=getchar\(\);
\}
if\(send\(sockfd,&c,1,0\)>0\);
//printf\("sent %c \\n",c\);
\}
else if\(chk=='0'\)\{
printf\("Incorrect username or password %c \\n",chk\);
continue;
\}
break;
\}
while(c!='3')
{
if(c=='1')
{
recv(sockfd,&rc,1,0);
if(rc=='1')
{
recv(sockfd,(char *)&student, sizeof(student), 0);
printf("%s %s %s \n",student.stud_id,student.DD,student.amount);
}
else if(rc=='0')
{
printf("First you need to register to check status\n");
}
recv(sockfd,msg6,sizeof(msg6),0);
printf("%s",msg6);
getchar();
scanf\("%c",&c\);
if\(send\(sockfd,&c,1,0\)>0\)
printf\("sent\\n"\);
continue;
}
else if(c=='2')
{
printf("Enter the DD no\n");
getchar();
scanf("%s",try);
strcpy(student.DD,try);
printf("Enter the amount \n");
getchar();
scanf("%s",try);
strcpy(student.amount,try);
if(recv(sockfd,&rc,1,0)>0) printf("rec %c \n",rc);
if(rc=='1')
{
printf("You cant register again,your regestration is already done,check in status \n");
recv(sockfd,msg6,sizeof(msg6),0);
printf("%s",msg6);
getchar();
scanf\("%c",&c\);
if\(send\(sockfd,&c,1,0\)>0\)
printf\("sent\\n"\);
continue;
}
else
{
send(sockfd,(char const *)&student, sizeof(student), 0);
recv(sockfd,&k,1,0);
printf("%c\n",k);
if(k=='1')
{
recv(sockfd,(char *)&student, sizeof(student), 0);
printf("<---------------------------------Receipt----------------------------------> \n");
printf("<-------StudentIDno-----------Demand Draftno--------Amountpaid--------Refund--------> \n");
printf("<-------%s--------------------%s--------------------%s----------------%s------------>",student.stud_id,student.DD,student.amount,student.ref);
//printf("%s\n",student.stud_id);
//printf("%s\n",student.DD);
}
else if(k=='0')
{
printf("The amount your trying to pay is less than the than required amount\n");
}
}
}
recv(sockfd,msg6,sizeof(msg6),0);
printf("%s",msg6);
getchar();
scanf\("%c",&c\);
if\(send\(sockfd,&c,1,0\)>0\)
printf\("sent\\n"\);
}
}
server
#include <ctype.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <signal.h>
#include <string.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define SIZE sizeof(struct sockaddr_in)
FILE *stud_file,*pass_file;
struct stud {
char stud_id[5];
char DD[5];
char amount[5];
char ref[5];
} student;
struct studp {
char stud_id[5];
char pwd[5];
} studentp;
void catcher(int sig);
char checkuserid(char c1[],char k1[]);
char regst(void);
char chkStatus(void);
int newsockfd;
char dump;
char d;
char msg[1024];
char msg1[1024];
char msg3[1024];
int fee=200,rf=0,value=0;
int main(void)
{
int sockfd;
char usr='0';
char c;
char k;
int control=22;
char msg2[1024] = "Enter 1 for status Enter 2 for Reg Enter 3 for exit \n";
struct sockaddr_in server = {AF_INET, 7000, INADDR_ANY};
static struct sigaction act;
act.sa_handler = catcher;
sigfillset(&(act.sa_mask));
sigaction(SIGPIPE, &act, NULL);
/* set up the transport end point /
if ( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
perror("socket call failed");
exit(1);
}
/ bind an address to the end point */
if ( bind(sockfd, (struct sockaddr )&server, SIZE) == -1)
{
perror("bind call failed");
exit(1);
}
/ start listening for incoming connections /
if ( listen(sockfd, 5) == -1 )
{
perror("listen call failed");
exit(1) ;
}
for (;
{
/ accept a connection */
if ( (newsockfd = accept(sockfd, NULL, NULL)) == -1)
{
perror("accept call failed");
continue;
}
if ( fork() == 0)
{
while(usr=='0')
{
send(newsockfd, "Enter the user name", 19, 0);
if\(recv\(newsockfd,msg,sizeof\(msg\),0\)>0\)\{
printf\("%s \\n",msg\);
\}
else
printf\("recieve failed"\);
send\(newsockfd, "Enter the password", 18, 0\);
if\(recv\(newsockfd,msg1,sizeof\(msg1\),0\)>0\)\{
printf\("%s \\n",msg1\);
//getchar();
}
else
printf("recieve failed");
usr = checkuserid\(msg,msg1\);
// printf\("%c \\n",usr\);
send\(newsockfd,&usr,1,0\);
if\(usr=='1'\)\{
printf\("done\\n"\);
send\(newsockfd,msg2 , sizeof\(msg2\), 0\);
if\(recv\(newsockfd,&c,1,0\)>0\)
printf\("%c \\n",c\);
control=c-'0';
//printf\("%d \\n",control\);
\}
\}
while\(control!=3\)\{
switch\(control\)
\{
case 1:
dump=chkStatus\(\);
break;
case 2:
dump=regst\(\);
break;
case 3:
exit\(1\);
\}
send\(newsockfd,msg2 , sizeof\(msg2\), 0\);
if\(recv\(newsockfd,&c,1,0\)>0\)
printf\("%c \\n",c\);
control=c-'0';
printf\("%d \\n",control\);
\}
// printf\("OMG \\n"\);
// while\(1\)\{
// printf\("asdasd\\n"\);
//\}
/* when client is no longer sending information the socket can be closed and the child process terminated */
close\(newsockfd\);
exit \(0\);
\}
/* parent doesn't need the newsockfd */
close\(newsockfd\);
}
}
void catcher(int sig)
{
close(newsockfd);
exit (0);
}
char checkuserid(char c1[],char k1[])
{
printf("%s here %s\n",c1,k1);
pass_file = fopen("pass","r");
fread(&studentp, sizeof(struct studp), 1, pass_file);
while (!feof(pass_file))
{
printf("%s here1 %s\n",c1,k1);
if(strcmp(studentp.stud_id,c1)==0){
printf("%s here2 %s\n",c1,k1);
if(strcmp(studentp.pwd,k1)==0){
printf("%s here3 %s \n",studentp.stud_id,studentp.pwd);
return '1';
}
}
fread(&studentp, sizeof(struct studp), 1, pass_file);
\}
return '0';
}
char chkStatus()
{
stud_file = fopen("record","r");
if(stud_file==NULL)
printf("error \n");
fread(&student, sizeof(struct stud), 1, stud_file);
while (!feof(stud_file))
{
if(strcmp(student.stud_id,msg)==0)
{
d='1';
send(newsockfd,&d ,1, 0);
\}
else
\{
d='0';
send\(newsockfd,&d ,1, 0\);
return '0';
}
fread(&student, sizeof(struct stud), 1, stud_file);
}
printf\("student ID:%s \\n",student.amount\);
send(newsockfd,(char const *)&student, sizeof(student), 0);
fclose(stud_file);
}
char regst()
{
stud_file = fopen("record","a+");
if(stud_file==NULL)
printf("error \n");
fread(&student, sizeof(struct stud), 1, stud_file);
while (!feof(stud_file))
{
if(strcmp(student.stud_id,msg)==0)
{
d='1';
send(newsockfd,&d ,1, 0);
return '0';
}
d='0';
fread(&student, sizeof(struct stud), 1, stud_file);
}
if(send(newsockfd,&d, 1, 0)>0) printf("sent %c \n",d);
recv(newsockfd,(char *)&student, sizeof(student), 0);
printf("%s %s %s \n",student.stud_id,student.DD,student.amount);
rf=atoi(student.amount);
if(fee-rf>0)
send(newsockfd,"0",1,0);
else if(fee-rf<=0)
{
send(newsockfd,"1",1,0);
value=(rf-fee);
sprintf(student.ref,"%d",value);
send(newsockfd,(char const *)&student, sizeof(student), 0);
fwrite(&student, sizeof(struct stud), 1, stud_file);
printf("%s %s %s \n",student.stud_id,student.DD,student.amount);
fclose(stud_file);
return '1';
}
}