📄 server.cpp
字号:
/*server.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 "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 "ctype.h"
#include "stdlib.h"
#define bufsize 20
#define BUFSIZE 20
#define QLEN 5
extern int t_errno;
//number of drawing money from bank
/*master and slave sockets */
#define LINELEN 128
#define LEN sizeof(struct bank)
#define NULL 0
struct bank
{
char name[5];//name of user
char account[5];
char money[5];
char code[5];//code of user
long crash;
} user[5];//large number of users imiating
void main(int argc,char *argv[])
{
unsigned port=6666;
struct sockaddr_in fsin; /*the from address of a client*/
int alen; /*the from address length */
char buf[BUFSIZE]; /* buffer for one line of text*/
char filenamestep; /*char of filename*/
WSADATA myWSAData;
WORD VersionReqd;
int Status;
int ssock; /*recive filename*/
int msock;
struct sockaddr_in sin;
struct protoent *ppe;
VersionReqd = MAKEWORD(2,2);
Status = WSAStartup(VersionReqd, &myWSAData);//The Windows Sockets WSAStartup function initiates use of WS2_32.DLL by a process.
if (Status != 0)
{
/* Tell the user that system could not find a useable file */
/* WinSock Listener DLL. */
printf("WSAStartup() failed\n");
exit(1);
}
/* Confirm that the WinSock DLL supports 2.2.*/
/* Note that if the DLL supports versions greater */
/* than 2.2 in addition to 2.2, it will still return */
/* 2.2 since that is the version we */
/* requested. */
if ((ppe=getprotobyname("tcp"))==0)
{ printf("Cann't get tcp protocol entry");
exit(1);}
msock=socket(PF_INET,SOCK_STREAM,ppe->p_proto);
if (msock<0)
{ printf("Can't create socket");
exit(1);}
memset(&sin,0,sizeof(sin));
sin.sin_family=AF_INET;
sin.sin_port=htons((u_short)port);
sin.sin_addr.s_addr=INADDR_ANY;
if (bind(msock,(struct sockaddr*)&sin, sizeof(sin))<0)
{ printf("Can't bind to %d port",port);
exit(1);}
/**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 **/
if (listen(msock,QLEN)<0)
{
printf("Can't listen on %d port",port);
exit(1);
}
int flag;
while (1)
{
flag=0;
printf(" ATM Server is listening...........\n");
alen=sizeof(fsin);
ssock=accept(msock,(struct sockaddr *)&fsin,&alen);
if (ssock<0)
{
printf("accept failed\n");
break;
}
printf("accepted...........\n");
/*******************************************/
//receive code of command
long crash=0;long prt=0;int t;
FILE *fp;
struct bank *p;
int fno,flen;
long len=LEN;
int cc;//int jks;
int i,j;
char c;
int bs=0;
int retry=0;
LK1:
i=0; j=0;
while(cc=recv(ssock,&filenamestep,1,0))
{
if (cc<0)
{
printf("receive error\nlistening......\n");
break;
}
if(filenamestep=='\0')//flag of end to file
{
printf("receive Message is %d\n",i);
break;
}
j++;
if ((retry>0)&&(j<(20-bs))) continue;
printf("%c",filenamestep);
buf[i]=filenamestep;
i++;
}
bs=i;
buf[i]='\0';//define flag of file
printf("\n\n");
printf("filename: %s\nTrans content: ",buf);//test language
printf("\n\n");
int leng;int kg,kd=0,fkg;char buft;
leng=strlen(buf);
printf("length of buf is:%d\n",leng);
//-----------------------
if(leng>10){
for(kg=0;kg<leng+1;kg++){
if(isgraph(buf[kg])==1){
printf("%c\n",buf[kg]);
fkg=kg;
break;
}
}
printf("\n\n");
if(fkg==leng){
buft=buf[fkg];
buf[0]=buft;
buf[1]='\0';
}
else{
for(kg=0;kg<10;kg++){
buft=buf[fkg];fkg++;
buf[kg]=buft;
}
buf[10]='\0';
}//解析出数据,去掉多余的乱码
printf(" second filename: %s\nTrans content: ",buf);
}
//-----------------------------------
c=buf[0];
switch(c){
case 'A':
{
/*check login*/
int status1;
int length=0;
char p1[bufsize];
while(1){//function of decode
if(buf[length]=='\0')break;
else{
p1[length]=buf[length];
length++;
//str++;
}
}//end of while
int i1;
char moneyN[5];char nameN[5];
char codeN[5];char accountN[5];
for(i1=1;i1<length;i1++){
if(p1[i1]=='/'){
status1=i1;
break;
}
accountN[i1-1]=p1[i1];
}
accountN[status1-1]='\0';
printf("Account is:%s\n",accountN);
int k1=0;
for(i1=status1+1;i1<length;i1++){
codeN[k1]=p1[i1];k1++;
}
codeN[k1]='\0';
printf("code is:%s\n",codeN);
//accomplish function of decode as stated above
if((fp=fopen("bank.dat","r+w"))==NULL)printf("File not exist!\n");
fno=fileno(fp);flen=filelength(fno);
char sendM[10];
char sendchar;
int t,s6=0,t2,t3;
t=(flen/LEN);
for(i1=0,p=user;i1<5;i1++,p++){
fread(p,LEN,1,fp);
if((!strcmp(p->account,accountN))&&(!strcmp(p->code,codeN))){
strcpy(nameN,p->name);strcpy(moneyN,p->money);fclose(fp);
printf("name is:%s\n",nameN); printf("money:%s\n",moneyN);
for(t2=0;t2<5;t2++){
if(nameN[t2]=='\0')break;
sendM[t2]=nameN[t2];
}
sendM[t2]='/';t3=t2+1;
for(t2=t3;t2<11;t2++){
sendM[t2]=moneyN[t2-t3];}
printf("send is:%s\n",sendM);
s6=3;
fclose(fp);
break;
}
}
if(s6==3) goto LK;
(void)send(ssock,"^",1,0);
printf("Send is '^'\n login retry......\n");
retry++;
goto LK1;
LK:
int gt;
gt=strlen(sendM);
for(i1=0;i1<gt;i1++){
printf("Now it is sending....Please wait for a while!\n");
sendchar=sendM[i1];
if(sendchar=='\0')break;
if(send(ssock,&sendchar,1,0)<0)break;
printf("%c",sendchar);
}
(void)send(ssock,"*",1,0);
printf("Sending is over'*'\n"); //add specific end char.
//LK1:
printf("Sending successfully!\n");
break;}
case 'B':
{
/*execute drawing money*/
char accountN3[5];char moneyN3[5];
int i23;int i3;
for(i23=0;i23<4;i23++)accountN3[i23]=buf[i23+1];
accountN3[4]='\0';
printf("account is: %s\n",accountN3);
for(i23=0;i23<5;i23++)moneyN3[i23]=buf[i23+6];
moneyN3[5]='\0';//decode module
printf("drawmoney is: %s\n\n\n",moneyN3);
int s9=0;
crash=atol(moneyN3);
printf("last drawmoney is: %d\n",crash);
if((fp=fopen("bank.dat","r+w"))==NULL)printf("File not exist!\n");
fno=fileno(fp);flen=filelength(fno);t=(flen/LEN);
for(i3=0,p=user;i3<5;i3++,p++){
fread(p,LEN,1,fp);
if(!strcmp(p->account,accountN3)){
prt=atol(p->money);
prt=prt-crash;
ltoa(prt,p->money,10);
p->crash=crash;
fseek(fp,-len,1);
fwrite(p,LEN,1,fp);
fclose(fp);
s9=6;
break;
}
}
if(s9==6)goto LN3;
(void)send(ssock,"(",1,0);//drawing money failed
retry++;
goto LK1;
LN3:
(void)send(ssock,"%",1,0);//drawing money successful
//LN31:
printf("Drawing money is %d\n",crash);
break;}
case 'C':
{
/*wash money*/
char accountM[5];
int fd2;
for(fd2=0;fd2<4;fd2++)accountM[fd2]=buf[fd2+1];
accountM[4]='\0';
printf("\n\n\n");
printf("account is: %s\n\n",accountM);
flag=buf[6];
printf("\n\n\n");
printf("Flag is: %c\n\n",flag);
long len=LEN;
if(flag=='+'){//drawMoney machine error
if((fp=fopen("bank.dat","r+w"))==NULL)printf("File not found!\n");
fno=fileno(fp);flen=filelength(fno);
t=(flen/LEN);
for(j=0,p=user;j<5;j++,p++){
fread(p,LEN,1,fp);
if(!strcmp(p->account,accountM)){
prt=atol(p->money);
crash=p->crash;
prt=prt+crash;
ltoa(prt,p->money,10);
p->crash=0;
fseek(fp,-len,1);
fwrite(p,LEN,1,fp);
fclose(fp);break;
}
}
}
if(flag=='-'){//drawMoney machine formal
if((fp=fopen("bank.dat","r+w"))==NULL)printf("File not found!\n");
fno=fileno(fp);flen=filelength(fno);
t=(flen/LEN);
for(j=0,p=user;j<t;j++,p++){
fread(p,LEN,1,fp);
if(!strcmp(p->account,accountM)){
p->crash=0;
fseek(fp,-len,1);
fwrite(p,LEN,1,fp);
fclose(fp);break;
}
}
}
break;}
case 'D':
{
/*modify code*/
char accountW[5];char codeW[5];int k3=0;
accountW[0]=buf[1];accountW[1]=buf[2];accountW[2]=buf[3];accountW[3]=buf[4];accountW[4]='\0';
codeW[0]=buf[6];codeW[1]=buf[7];codeW[2]=buf[8];codeW[3]=buf[9];codeW[4]='\0';
printf("Account is:%s\n",accountW);
printf("Code is:%s\n",codeW);
if((fp=fopen("bank.dat","r+w"))==NULL)printf("File not exist!\n");
fno=fileno(fp);flen=filelength(fno);t=flen/LEN;
for(j=0,p=user;j<5;j++,p++)
{
fread(p,LEN,1,fp);
if(!strcmp(p->account,accountW)){
strcpy(p->code,codeW);
fseek(fp,-len,1);
fwrite(p,LEN,1,fp);
fclose(fp);k3=6; break;
}
}
if(k3==6)goto LJH2;
(void)send(ssock,">",1,0);//modify failed
retry++;
goto LK1;
LJH2:
(void)send(ssock,"<",1,0);//modify successful
(void)send(ssock,"*",1,0);
printf("Now code had been modifyed!\n");
break;}
}//end of switch
printf("\nSend File %s OK\nSending is successful.........\n",buf);
goto LK1;//返回开始位置,不要关闭socket
if (flag==1) (void) closesocket(ssock);
}
(void) closesocket(msock);
WSACleanup();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -