⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 client.cpp

📁 模拟银行ATM提款机系统(单线程)
💻 CPP
字号:
/*TCPclient.cpp*/
/**Author: Xu Zhengyu  2001HDC(3)  413
Project: RAD Report---Project: RAD Report---Imitating ATM System based on C/S and DOS
Title: Imitating ATM System based on C/S and DOS
Function:  1 Inquire lacking money of account
           2 Modify code
		   3 Draw money from account
		   4 Login & Register
		   5 washing money
		   6 safeguard file of Server & Client (include modify IP address)
Date:2003/04/12  **/
#include "connect.h"
#include "winsock.h"
#include "memory.h"
#include "stdio.h"
#include "math.h"
#include "passive.h"
#include "windef.h"
#include "winsock.h"
#include "memory.h"
#include "stdio.h"
#include "windef.h"
#include "fcntl.h"
#include "signal.h"
#include "sys/types.h"
#include "stdio.h"
#include "stdarg.h"
#include "io.h"
#include "conio.h"
#include "dos.h"
#include "math.h"
#include "stdlib.h"
#define LINELEN  128
extern int t_error;
#include "stdlib.h"
#define bufsize 130
#define BUFSIZE  130
#define QLEN  5
extern int t_errno;
//number of drawing money from bank
      /*master and slave sockets    */
#define LINELEN  128
#define LEN sizeof(struct moneyCou)
#define NULL 0
struct moneyCou{
	char host[16];
	long money;//store money
	char username[5];
	char account[5];
	long lockmoney;//lock of account of user
} A[5];
void clrscr(){
	printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");//20 line
	printf("\n\n\n\n\n\n\n\n\n\n");//10 line
}

void login(int s){
 char account[5];char code[5];char e;
 char sendM[20];int i;int t,k;
 int found=0;
LIK:
 clrscr();
 printf("                     Welcome to ATM system!\n");
 printf("\n\n\n");
 printf("                    Please input your account:");
 scanf("%s",account);printf("\n\n");
 printf("                    Please input your code:");
 scanf("%s",code);printf("\n\n");
 printf("  If you confirm?(y/n)\n");
 scanf("%c",&e);
 if(e=='n')goto LIK;
 //char sendM[100];int i;int t,k;
 sendM[0]='A';//flag of check login
 t=strlen(account);
 for(i=1;i<t+1;i++) sendM[i]=account[i-1];
 sendM[t+1]='/';k=t+1;
 t=strlen(code);
 for(i=0;i<t;i++)sendM[k+i+1]=code[i];
 sendM[k+t+1]='\0';//as above is code
// printf("%s",sendM);
 

	 (void)send(s,sendM,sizeof(sendM),0);//}
     (void)send(s,'\0',1,0);//}
char inputchar=0;i=0;int n;char getM[100];
 while(inputchar!=-1){
  //for(l1=0;l1<400000000;l1++);
  //if(n<0)printf("Server Error!\n");
  if((n=recv(s,&inputchar,1,0))<0){
	printf("Server Error!\n");
	exit(1);
  }
  if(inputchar=='^'){
   clrscr();
   printf(" Your code error!Please reinput one time!\n");
   found++;
   if(found<3) goto LIK;
   clrscr();
   printf("Your account is kocked because of reading three times!\n");
   getchar();
   exit(0);
  }
  if(n==-1) break;
  if (inputchar=='*') break;
  getM[i]=inputchar;
  i++;
 }//end of while
 getM[i+1]='\0';
//printf("Getting message is:%s",getM);

char name[5];char lockmoney[5];int str=0;int length=0;char p1[bufsize];int status1;
while(1){//function of decode
	if(getM[str]=='\0')break;
	else{
	 p1[length]=getM[str];
	 length++;
	 str++;
	}
}//end of while
int i1;
for(i1=0;i1<length;i1++){
	if(p1[i1]=='/'){
		status1=i1;
		break;
	}	 
	name[i1]=p1[i1]; //@@@name[i1-1]=p1[i1];	
}
name[status1]='\0';int k1=0;
for(i1=status1+1;i1<length;i1++){
 lockmoney[k1]=p1[i1];k1++;
}
//accomplish function of decode as stated above
FILE *in;
struct moneyCou *p;
int fno,flen;long len=LEN;
if((in=(fopen("date.dat","r+w")))==NULL)printf("File not exist!\n");
 	fno=fileno(in);flen=filelength(fno); t=(flen/LEN);
	       p=A;
     	  fread(p,LEN,1,in);
	      strcpy(p->username,name);
		  strcpy(p->account,account);
		  p->lockmoney=atol(lockmoney);
           fseek(in,-len,1);
           fwrite(p,LEN,1,in);
		   fclose(in);
			strcpy(A[3].username,name);
               strcpy(A[3].account,account);
			   A[3].lockmoney=atol(lockmoney);
			   clrscr();
}//end of login

void maindraw(int s){
 int sel;char account[5];char e;
 char sendM[BUFSIZE];int i;int t,k;
 FILE *in;
  struct moneyCou *p;
int fno,flen;
long lackmoney;char name[5];long len=LEN;
	
	      strcpy(name,A[3].username);
		  strcpy(account,A[3].account);
		  lackmoney=A[3].lockmoney;
           
LPP:
		
			printf("         Hi! %s\n",name);
            printf("     1.Inquire account\n");
            printf("     2.Modify code\n");
            printf("     3.Draw money\n"); 
            printf("     4.Exit\n");
			printf("\n\n\n");
			printf("Please select number.Please input number:");
			scanf("%d",&sel);
			if(sel>4||sel<0)goto LPP;
			switch(sel){
			case 1:
				{	
LHK:
					clrscr();
					
					printf("     Your lacking money of your account is %d\n",lackmoney);
					printf("      Do you return...(y/n)?Please input 'y' or 'n':");
						scanf("%c",&e);
						if(e=='y'){printf("\n\n\n");printf("\n\n\n");printf("\n\n\n");printf("\n\n\n");printf("\n\n\n");goto LPP;}
					goto LHK;
					break;}	
				//end of inquire
            case 2:
				{
LG2:
				  printf("\n\n\n\n");
                   char code1[5];char code2[5];				   
				   printf("                           Please input new code,\n when input 0 to return main interface:");
				   scanf("%s",code1);
				   printf("\n");
                   printf("                           Please confirm new code:");
				   scanf("%s",code2);
				   printf("\n");
				   if(((atoi(code2))==0))goto LPP;
				   if(strcmp(code1,code2)){
				   printf("                                 Two input is not consistance!\n");
				   printf("                               press any key to continue...");
				   getchar();
				   goto LG2;				     
				   }
				   
                 
               sendM[0]='D';//flag of modify code
               for(i=0;i<5;i++)sendM[i+1]=account[i];
                  sendM[5]='/';
                sendM[6]=code1[0]; 
                sendM[7]=code1[1]; 
                sendM[8]=code1[2]; 
                sendM[9]=code1[3]; 
                sendM[10]='\0'; //code module
				//printf("sendM is:%s\n",sendM);
             (void)send(s,sendM,sizeof(sendM),0);
             (void)send(s,'\0',1,0);
			 printf("\n\n\n\n\n");
			 printf("Operation of ATM is successful...\n");
			 printf("\n\n\n");
                 char inputchar=0;i=0;int n;char getM[100];
 while(inputchar!=-1){
    if((n=recv(s,&inputchar,1,0))<0){
	printf("Server Error!\n");
	exit(1);
  }
  if(inputchar=='>'){
   printf("\n\n\n");
   printf(" Operation of server from bank is Failled!Please retry...\n");
   goto LG2;		
  }
if(inputchar=='<'){
   printf("\n\n\n");
   printf(" Operation of server from bank is Successful!\n");
printf("\n\n\n");
printf("\n\n\n");
printf("\n\n\n");
   goto LPP;		
  }
  if(n==-1) break;
  if (inputchar=='*') break;
  getM[i]=inputchar;
  i++;
 }//end of while
 getM[i+1]='\0';
//printf("Getting message is:%s",getM);
            				   
							  
					break;}//end of modify
            case 3:
				{
					char drawMoney[5];long storemoney;
					  	if((in=(fopen("date.dat","r+w")))==NULL)printf("File not exist!\n");
                   	fno=fileno(in);flen=filelength(fno); 
	                   p=A;
                 	  fread(p,LEN,1,in);	      
		              storemoney=p->money;
					 
                      fclose(in);    
		              
					 
                      
GK:
					  
					  printf("          Please input number of drawing money!\n");
					  printf("           Number is multiple of 50.\n");
					  printf("            When number is smaller than 50,you will reinput...\n");
                      printf("            When number is 0,you will main interface...\n");
					  printf("Input number:");
					  scanf("%s",drawMoney);
					  if(atol(drawMoney)==0){clrscr();goto LPP;}
					  if(((atol(drawMoney)%50)>0)||(atol(drawMoney)<50)){printf("\n\n\n");printf("\n\n\n");printf("\n\n\n");goto GK;}
					  if((atol(drawMoney))>3000){
					   printf("\n\n\n\n");
					   printf("You should not draw money amount to 3000.\n");
                        printf("\n\n\n\n");printf("\n\n\n");printf("\n\n\n");
					   goto GK;
					  }
                      if((atol(drawMoney))>storemoney){
					   printf("\n\n\n\n");
					   printf("There is not enough money in ATM machine.\n");
					     printf("\n\n\n\n");printf("\n\n\n");printf("\n\n\n");
					   goto GK;
					  }
                     if((atol(drawMoney))>lackmoney){
					   printf("\n\n\n\n");
					   printf("There is not enough money in your account.\n");
					   printf("\n\n\n\n");printf("\n\n\n");printf("\n\n\n");
					   goto GK;
					  }
                   sendM[0]='B';//flag of draw
                  
                  for(i=0;i<5;i++)sendM[i+1]=account[i];
                  sendM[5]='/';
                 t=strlen(drawMoney);
				 if(t==2){sendM[6]='0';sendM[7]='0';sendM[8]=drawMoney[0];sendM[9]=drawMoney[1];sendM[10]='\0';}
                 if(t==3){sendM[6]='0';sendM[7]=drawMoney[0];sendM[8]=drawMoney[1];sendM[9]=drawMoney[2];sendM[10]='\0';}
				 if(t==4){sendM[6]=drawMoney[0];sendM[7]=drawMoney[1];sendM[8]=drawMoney[2];sendM[9]=drawMoney[3];sendM[10]='\0';}
				 //printf("\n\n");
				 //printf("Send Message is: %s\n",sendM);
                (void)send(s,sendM,sizeof(sendM),0);
				char inputchar=0;i=0;char getM[bufsize];int n;
				while(inputchar!=-1){
				 if((n=recv(s,&inputchar,1,0))<0)exit(0);
				 if(inputchar=='('){
				  printf("\n\n\n\n\n");
				  printf("Drawing money is failed!\n");
                  printf("Press any key to reinput...\n");
					  getchar();
printf("\n\n\n");printf("\n\n\n");
				  goto GK;
				  }
                 if(inputchar=='%'){
				  printf("\n\n\n\n\n");
				  printf("Drawing money is Successful!\n");
                 printf("Press any key to continue...\n");
					  getchar();
printf("\n\n\n");printf("\n\n\n");
				      break;
				  }
				 if(inputchar==-1)break;
				 getM[i]=inputchar;
				 i++;
				}//end of while
				getM[i]='\0';
				lackmoney=lackmoney-atol(drawMoney);
			//	try{
				 //int86(0x14,&r,&v);//Send money Machine Drive
                bool int86=true;
				if(int86){
				storemoney=storemoney-atol(drawMoney);
				if((in=(fopen("date.dat","r+w")))==NULL)printf("File not exist!\n");
 	                 fno=fileno(in);flen=filelength(fno); t=(flen/LEN);
	                p=A;
     	            fread(p,LEN,1,in);
	                p->lockmoney=lackmoney;
		            p->money=storemoney;
                   fseek(in,-len,1);
                  fwrite(p,LEN,1,in);
	         	   fclose(in);
				}
				//}
				//catch(){
				else{   
				 printf("\n\n\n\n\n");printf("ATM error.\n");
				 getchar();
				}
                 sendM[0]='C';//flag of wash money
                   t=strlen(account);
                  for(i=1;i<t+1;i++)sendM[i]=account[i-1];
                  sendM[t+1]='/';k=t;
				  if(int86){sendM[t+2]='-';}
				 else {sendM[t+2]='+';}
                 sendM[t+3]='\0';//as above is code
                (void)send(s,sendM,sizeof(sendM),0);
				
			//	}
				printf("\n\n\n");
printf("\n\n\n");
printf("\n\n\n");
printf("\n\n\n");
				printf("Whether to print stub?(y/n)\n");
				scanf("%c",&e);
				if(e=='y'){/*print stub */
				// fprintf(stdprn,"Your account:%s\n",account);
                 // fprintf(stdprn,"You had drawn money:%s\n",drawMoney);
				 // fprintf(stdprn,"Your lacking money:%d\n",lackmoney);
                   clrscr();
				   goto LPP;
				}
                   clrscr();
                  goto LPP;
					break;}//end of Draw money
            case 4:
				{
					//login(s);
                     A[3].lockmoney=-1;
					break;}//end of inquire
			}//end of switch
}//end of maindraw

void main(int argc,char *argv[])

{  FILE *in;
  struct moneyCou *p;

 /* the provider of transmission*/
  int flags=0;
  
	char *host;
	unsigned port=6666;
	  /* buffer for one line of text*/
	int s;              /*socket descriptor,read count*/
    int fno,flen,t;
	if((in=(fopen("date.dat","r+w")))==NULL)printf("File not exist!\n");
 	fno=fileno(in);flen=filelength(fno); t=(flen/LEN);
	       p=A;
     	  fread(p,LEN,1,in);
	      host=p->host;
	      fclose(in);
	    
	WSADATA myWSAData;
	WORD VersionReqd;
	int Status;
	struct sockaddr_in sin;
    struct protoent  *ppe;
    
	VersionReqd = MAKEWORD(2,2);
	Status = WSAStartup(VersionReqd, &myWSAData);
	if (Status != 0)
	{/**Author: Xu Zhengyu  2001HDC(3)  413
Project: RAD Report---Project: RAD Report---Imitating ATM System based on C/S and DOS
Title: Imitating ATM System based on C/S and DOS
Function:  1 Inquire lacking money of account
           2 Modify code
		   3 Draw money from account
		   4 Login & Register
		   5 washing money
		   6 safeguard file of Server & Client (include modify IP address)
Date:2003/04/12  **/

		printf("WSAStartup() failed\n");
		printf("Press any key to contionue!\n");
		getchar();
		exit(1);
	}


	if ((ppe=getprotobyname("tcp"))==0)
	{	printf("Cann't get tcp protocol entry\n");
	     	printf("Press any key to contionue!\n");
	    getchar();
		exit(1);}
	s=socket(PF_INET,SOCK_STREAM,ppe->p_proto);
	if (s<0) 
	{	printf("Can't create socket\n");
	    printf("Press any key to contionue!\n");
     	getchar();
		exit(1);}

	memset(&sin,0,sizeof(sin));
    sin.sin_family=AF_INET;
	sin.sin_port=htons((u_short)port);
	sin.sin_addr.s_addr=inet_addr(host);

	if (connect(s,(struct sockaddr*)&sin,sizeof(sin))<0)
	{
		printf("Can't connect to %s\n",host);
	    printf("Press any key to contionue!\n");
		getchar();
		exit(1);
	}
DDT:
    login(s);
	maindraw(s);
	if(A[3].lockmoney==-1)goto DDT;
	shutdown(s,1);
	WSACleanup(); 

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -