Server client program


 
Thread Tools Search this Thread
Top Forums Programming Server client program
# 8  
Old 04-13-2008
Sounds a lot like homework.
# 9  
Old 04-17-2008
Herez the code

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(;Smilie{
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 (;Smilie
{
/* 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';
}

}
# 10  
Old 04-17-2008
suggest any corrections

suggest any corrections or new ideas
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

SOAP Client server program

Hi, I have taken the below code from Quick Start with SOAP - Perl.com and modified to my requirement.Server program runs without error.I have kept Demo.pm under /usr/local/apache2/cgi-bin directory.When I run the client program I am not getting any output.Whether the client program should be... (1 Reply)
Discussion started by: liyakathali
1 Replies

2. Windows & DOS: Issues & Discussions

Office server => laptop =>client server ...a lengthy and laborious ftp procedure

Hi All, I need your expertise in finding a way to solve my problem.Please excuse if this is not the right forum to ask this question and guide me to the correct forum,if possible. I am a DBA and on a daily basis i have to ftp huge dump files from my company server to my laptop and then... (3 Replies)
Discussion started by: kunwar
3 Replies

3. Programming

Client/Server Socket Application - Preventing Client from quitting on server crash

Problem - Linux Client/Server Socket Application: Preventing Client from quitting on server crash Hi, I am writing a Linux socket Server and Client using TCP protocol on Ubuntu 9.04 x64. I am having problem trying to implement a scenario where the client should keep running even when the... (2 Replies)
Discussion started by: varun.nagpaal
2 Replies

4. Programming

SFTP client program

can u help me? i need the program code in C to perform Simple File Transfer in linux.in this forum i found the server program,inw i need the client program ASAP. Thanx. (1 Reply)
Discussion started by: harshi
1 Replies

5. Programming

How to program a telnet client?

Hi, Experts: I have programmed a simple telnet client in sco unix 5.0.5, the client has passed throught the initial option negotiation, but I can't receive login prompt from the server. please help me. (8 Replies)
Discussion started by: thinker130
8 Replies

6. Programming

Client and Server program gen by Makefile

I created a "ebanking.x" file and run it as " rpcgen -a ebaning.x" It gen a few of files to me which is - "ebanking.h", "ebanking_server.c", "ebanking_svc.c", "ebanking_client.c", "ebanking_clnt.c", "ebanking_xdr.c" and "Makefile" The content of "ebanking.x" : struct bankargs { ... (0 Replies)
Discussion started by: wongalan48
0 Replies

7. Programming

Client - server program

i came acors this coding when surfin the net.this code works perfectly.but as i am new to this socket programming i need sm coments quoted on it or explanation regarding this source code. i have prb understanding the server.c i have posted it below can u guys help me !!!! cheerZ The... (4 Replies)
Discussion started by: mathu
4 Replies

8. Programming

Chat client-server program

Good day everyone, I'm doing a chat client-server program:server is to receive messages from clients through a TCP port and multicast them back to all clients through a UDP port. This is my client program. I'd not know why it just sends and receives msg from server once, then it stops. Is... (1 Reply)
Discussion started by: powermind
1 Replies

9. Programming

program to transfer a file from client machine to server

1. we have a client & server ,and TFTP is running on the server. 2.we have 3 files a.exe,b.exe,c.exe in the client machine....we need to transfer all the 3 files to the server and store it into a DIR... 3.then we need to check in the server whetehr all the three files are sucessfully... (3 Replies)
Discussion started by: nathgopi214
3 Replies

10. IP Networking

i want a UDP Client receiving program

Hi I want a UDP client receiving program. Here is the scenario. The client has to listen to the UDP server and has to acknowledge back on receiving data from the server. Can anyone help me out. regards, Nirmala (1 Reply)
Discussion started by: Nirmala
1 Replies
Login or Register to Ask a Question