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

📄 newserver.pc

📁 服务器端网络编程
💻 PC
📖 第 1 页 / 共 2 页
字号:
/*******************//*   SERVER        *//******************/#include "Definition.h"#include <sys/types.h>#include <sys/socket.h>#include <netinet/in.h>#include <netdb.h>#include <stdio.h>#include <sys/param.h>#include <errno.h>#include <fcntl.h>#include <signal.h>#include <wait.h>#include <time.h>#include <string.h>#include <stdlib.h>#include <sqlca.h>#pragma pack (push,1)#define ClientNUM 100#define lock_file "log.lck"#define ConfigFile "config.ini"#define RecordFile "record.log"#define TempFile   "temp.log"void head1_info_print(Message_Head1 * phead);void body1_info_print(Message_Body1 * pbody);void tail_info_print(Message_Tail * ptail);struct in_addr ClientIP[ClientNUM];char affirm(int p_error,unsigned int inform_seq){        exec sql begin declare section;/*连接数据库的变量*/        unsigned char phonenum[21]="###"; /*用户的号线*/        unsigned char username[10]="djm";/*连接的数据库名*/        unsigned char passwd[30]="djm";  /*连接的数据库密码*/        unsigned char server[100]="myoracle";  /*连接数据库的服务名*/        int resp_type=26;        unsigned int sequence=inform_seq;        int point=p_error;        exec sql end declare section;               EXEC SQL DECLARE DB2 DATABASE;        EXEC SQL AT DB2 CONNECT :username IDENTIFIED BY :passwd                USING :server;        if(sqlca.sqlcode==0)                printf("OK!!connect to database SUCCESS!!!!!!\n");        else                printf("%.*s\n",sqlca.sqlerrm.sqlerrml,sqlca.sqlerrm.sqlerrmc);	printf("+++++++++++sequence:%d\n",sequence);                   	printf("+++++++++++point:%d\n",point);                           EXEC SQL  EXECUTE	BEGIN	 	tem_confirm(:sequence,:point,:resp_type);	END;	END-EXEC;	printf("%.*s\n",sqlca.sqlerrm.sqlerrml,sqlca.sqlerrm.sqlerrmc);	printf("+++++++++++resp_type:%d\n",resp_type);                           return resp_type;}char accses(short int opertype,unsigned int inform_seq,unsigned char *contractid,unsigned int currencyend,unsigned int *accountbalance){	exec sql begin declare section;/*连接数据库的变量*/	unsigned char phonenum[21]="###"; /*用户的号线*/	unsigned char username[10]="djm";/*连接的数据库名*/	unsigned char passwd[30]="djm";  /*连接的数据库密码*/	unsigned char server[100]="myoracle";  /*连接数据库的服务名*/	unsigned char contract_id[21]="";	unsigned int currency=0;	unsigned int account_balance=0;	int resp_type=26;	unsigned int sequence=inform_seq;	exec sql end declare section;	memcpy(&contract_id,contractid,20);	currency=currencyend;	sequence=inform_seq;	EXEC SQL DECLARE DB2 DATABASE;	EXEC SQL AT DB2 CONNECT :username IDENTIFIED BY :passwd		USING :server;	if(sqlca.sqlcode==0)		printf("OK!!connect to database SUCCESS!!!!!!\n");	else		printf("%.*s\n",sqlca.sqlerrm.sqlerrml,sqlca.sqlerrm.sqlerrmc);        if((currency&(0x80000000))!=0)                resp_type=27;        else        {		switch(opertype)		{			case 1:				EXEC SQL  EXECUTE					BEGIN					tem_plusvalue(:contract_id,:sequence,:currency,:account_balance,:resp_type);				END;				END-EXEC;				printf("%.*s\n",sqlca.sqlerrm.sqlerrml,sqlca.sqlerrm.sqlerrmc);				printf("+++++++++++:%s\n",contract_id);                   				if(resp_type==1)					account_balance=currency;				break;			case 3:				EXEC SQL  EXECUTE					BEGIN					tem_minusvalue(:contract_id,:sequence,:currency,:account_balance,:resp_type);				END;				END-EXEC;				printf("%.*s\n",sqlca.sqlerrm.sqlerrml,sqlca.sqlerrm.sqlerrmc);				if(resp_type==1)					account_balance=currency;				break;			case 5:				EXEC SQL  EXECUTE					BEGIN					tem_inquiry(:contract_id,:account_balance,:resp_type);				END;				END-EXEC;				printf("%.*s\n",sqlca.sqlerrm.sqlerrml,sqlca.sqlerrm.sqlerrmc);				if(resp_type==1)					account_balance=0xffffffff;				break;			default:				printf("WRONG OPERA_TYPE %d\n",opertype);				break;		}		*accountbalance=account_balance;		printf("+++++++++++:%d\n",account_balance);                   	}	return resp_type;}FILE * openfile(){	FILE * fid;	char * filename=ConfigFile;	fid=fopen(filename,"r");	if(fid==NULL)	{		printf("error opening file\n");		exit(1);	}	return fid;}int readfile(FILE *fid){	unsigned int port;	unsigned char IP[15];	int i=0;	fscanf(fid,"ServerPORT=%d\n",&port);	while(!feof(fid))	{		//strcpy(IP,"EOF");		fscanf(fid,"ClientIP=%s\n",IP);		//if(IP[0]=='E')		//	break;		if(inet_aton(IP,&ClientIP[i++])==0)		{			perror("IP address error\n");			exit(1);		}	}	fclose(fid);	return port;}int writelog(char * filename,Message_Head1 * phead,Message_Body1 * pbody,Message_Tail * ptail){	FILE * fid;	int file_desc;	int save_errno;	for(;;)	{		file_desc=open(lock_file,O_RDWR|O_CREAT|O_EXCL,0444);		if(file_desc==-1)		{			printf("%d---wait to save data\n",getpid());			sleep(1);		}		else		{			break;		}	}	fid=fopen(filename,"a+");	if(fid==NULL)	{		printf("error opening file\n");		return(-1);	}	else	{		fwrite(phead,sizeof(*phead),1,fid);		fwrite(pbody,sizeof(*pbody),1,fid);		fwrite(ptail,sizeof(*ptail),1,fid);		fflush(fid);		printf("%d---write log file\n",getpid());		close(fid);		//sleep(1);	}	(void)close(file_desc);	(void)unlink(lock_file);	return(0);}int readlog(){	FILE * fid;	Message_Head1 head;	Message_Body1 body;	Message_Tail  tail;	(void)unlink(lock_file);	fid=fopen(RecordFile,"r");	if(fid==NULL)	{		printf("No Record File\n");		return(0);	}	else	{		while(!feof(fid))		{			printf("%d---read log file\n",getpid());			if(fread(&head,sizeof(head),1,fid)!=1)				break;				if(fread(&body,sizeof(body),1,fid)!=1)				break;			if(fread(&tail,sizeof(tail),1,fid)!=1)				break;			head1_info_print(&head);			body1_info_print(&body);			tail_info_print(&tail);			if(affirm(0,body.trader_serial)==26)				writelog(TempFile,&head,&body,&tail);		}		fclose(fid);		(void)unlink(RecordFile);		rename(TempFile,RecordFile);	}	return 1;}struct sockaddr_in gethoststuff(){	FILE *fid;	int port;	struct sockaddr_in sa;	struct hostent * ph;	char myname[MAXHOSTNAMELEN+1];	fid=openfile();	port=readfile(fid);	gethostname(myname,MAXHOSTNAMELEN);	printf("host name is %s\n",myname);	printf("port is %d\n",port);	//if((ph=gethostbyname(myname))==NULL)	//{	//    printf("gethostbyname failed\n");	//    exit(1);	//  }	memset(&sa,0,sizeof(struct sockaddr_in));	sa.sin_family=AF_INET;	sa.sin_addr.s_addr=htonl(INADDR_ANY);	sa.sin_port=htons(port);	return sa;}int opensocket(){	int s;	s=socket(AF_INET,SOCK_STREAM,0);	if(s<0)	{		perror("socket error");		exit(1);	}	return s;}void bindnlisten(int s, struct sockaddr_in sa){	if(bind(s,(struct sockaddr *)&sa,sizeof(sa))<0)	{		perror("bind error");		exit(1);	}	listen(s,5);}int acceptconn(int s,struct sockaddr_in * p_client_sa){	int sd;	unsigned int sasize;	int i;	sasize=sizeof(*p_client_sa);	if((sd=accept(s,(struct sockaddr *)p_client_sa,&sasize)),0)	{		perror("accept error");		return(-1);	}	for(i=0;i<ClientNUM;i++)	{		if(p_client_sa->sin_addr.s_addr==ClientIP[i].s_addr)		{			printf("Authenticated User From %s Log In\n",inet_ntoa(p_client_sa->sin_addr));			return sd;		}	}	close(sd);	printf("Not Authenticated User Log In\nRefuse to Offer Service\nConnection Closed\n");	return(-1);}unsigned int gettimestamp(){	char tmpbuf[12];	unsigned int timestamp;	struct tm *today;	time_t ltime=time(NULL);	today = localtime( &ltime );	strftime( tmpbuf,12,"%y%m%d", today );	//printf( tmpbuf );	//timestamp = *((unsigned int*)tmpbuf);	timestamp = atoi(tmpbuf);	//printf("******%d******\n",timestamp);	return timestamp;}unsigned int my_addr(int s){	struct sockaddr_in * psa;	socklen_t len=sizeof(struct sockaddr_in);	psa=(struct sockaddr_in *)malloc(sizeof(*psa));	if(getsockname(s,(struct sockaddr *)psa,&len)==0)	{		return psa->sin_addr.s_addr;	}	return 0;}void head1_info_print(Message_Head1 * phead){	printf("\noper_type is %d\n",phead->oper_type);	printf("packet_length is %d\n",phead->pack_length);	printf("packet_date is %d\n",phead->pack_date);	printf("head_length is %d\n",phead->head_length);	printf("record_numb is %d\n",phead->record_numb);}void body1_info_print(Message_Body1 * pbody){	printf("\ntrader_serial is %d\n",pbody->trader_serial);	printf("contract_id is %s\n",pbody->contract_id);	printf("currency is %d\n",pbody->currency);}void body2_info_print(Message_Body2 * pbody){	printf("\ntrader_serial is %d\n",pbody->trader_serial);	printf("contract_id is %s\n",pbody->contract_id);	printf("account_balance is %d\n",pbody->account_balance);}void tail_info_print(Message_Tail * ptail){	struct in_addr temp;	printf("\ntail_length is %d\n",ptail->tail_length);	printf("tail_field_numb is %d\n",ptail->tail_field_numb);	printf("tail_field_dat.traderid is %d\n",ptail->tail_field_data.trader_id);	temp.s_addr=ptail->tail_field_data.trader_id;	printf("traderid is %s\n",inet_ntoa(temp));	printf("check code is %d\n",ptail->tail_field_data.check_code);}

⌨️ 快捷键说明

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