📄 client.c
字号:
/************************/
#include "Definition.h"
#include <sys/types.h>
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <sys/param.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#define MAXSOCKADDR 128
#define ConfigFile "config.ini"
unsigned char buff[256];
int ts=11100;
ReadConfigFile(struct sockaddr_in * pserver_sa)
{
FILE * fid;
unsigned char IP[15];
unsigned int port;
char * filename=ConfigFile;
fid=fopen(filename,"r");
if(fid==NULL)
{
printf("error opening file\n");
exit(1);
}
fscanf(fid,"ServerIP=%s\n",IP);
fscanf(fid,"ServerPORT=%d\n",&port);
if(inet_aton(IP,&pserver_sa->sin_addr)==0)
{
perror("IP address error\n");
exit(1);
}
pserver_sa->sin_port=htons(port);
pserver_sa->sin_family=AF_INET;
}
describe_body(short int oper_type,short int * precord_numb,DESCRIPT * pdescript){
*precord_numb=3;
switch(oper_type)
{
case 0x0001:
case 0x0003:
pdescript[0].field_type='I';
pdescript[0].field_length=4;
pdescript[0].field_descript=1;
pdescript[1].field_type='C';
pdescript[1].field_length=20;
pdescript[1].field_descript=2;
pdescript[2].field_type='I';
pdescript[2].field_length=4;
pdescript[2].field_descript=3;
break;
case 0x0005:
pdescript[0].field_type='I';
pdescript[0].field_length=4;
pdescript[0].field_descript=1;
pdescript[1].field_type='C';
pdescript[1].field_length=20;
pdescript[1].field_descript=2;
pdescript[2].field_type='I';
pdescript[2].field_length=4;
pdescript[2].field_descript=4;
break;
default:
printf("wrong oper_type");
}
}
unsigned int gettimestamp()
{
char tmpbuf[12];
unsigned int timestamp;
struct tm *today;
time_t ltime = time(NULL);
today = localtime( <ime );
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 describe_tail(DESCRIPT * pdescript)
{
pdescript[0].field_type='I';
pdescript[0].field_length=4;
pdescript[0].field_descript=7;
pdescript[1].field_type='I';
pdescript[1].field_length=4;
pdescript[1].field_descript=8;
pdescript[2].field_type='I';
pdescript[2].field_length=4;
pdescript[2].field_descript=9;
}
Message_Head1 head1_assemble(short int oper_type)
{
Message_Head1 head;
head.oper_type=oper_type;
head.pack_length=0;
describe_body(oper_type,&(head.record_numb),head.descript);
head.pack_date=gettimestamp();
head.head_length=sizeof(head);
head.record_numb=3;
return head;
}
Message_Tail tail_assemble(Message_Head1 * phead,Message_Body1 *pbody,int s)
{
Message_Tail tail;
tail.tail_length=sizeof(tail);
phead->pack_length=sizeof(*phead)+sizeof(*pbody)+sizeof(tail);
tail.tail_field_numb=1;
describe_tail(&tail.tail_field_descript);
tail.tail_field_data.trader_id=my_addr(s);
//tail.tail_field_data.check_code=(tail.tail_field_data.trader_id+phead->pack_length)^phead->pack_date;
tail.tail_field_data.check_code=0;
tail.tail_field_data.sum=pbody->currency;
return tail;
}
void add_check_sum(Message_Head1 * phead,Message_Tail * ptail)
{
ptail->tail_field_data.check_code=(ptail->tail_field_data.trader_id+phead->pack_length)^phead->pack_date;
}
int opensocket()
{
int s;
s=socket(AF_INET,SOCK_STREAM,0);
if(s<0)
{
perror("socket error");
return(-1);//exit(1);
}
return s;
}
int makeconnection(int s,struct sockaddr_in * psa)
{
if(connect(s,(struct sockaddr *)psa,sizeof(*psa))<0)
{
perror("connect error");
return(-2);
}
else
{
return(0);
}
}
void body1_assemble(Message_Body1 * pbody)
{
//randomize();
ts=ts+1;
pbody->trader_serial=ts;
strcpy(pbody->contract_id,"0000000068301702");
pbody->currency=500000;
}
void head1_info_print(Message_Head1 * phead)
{
printf("\noper_type is %u\n",phead->oper_type);
printf("packet_length is %u\n",phead->pack_length);
printf("packet_date is %u\n",phead->pack_date);
printf("head_length is %u\n",phead->head_length);
printf("record_numb is %u\n",phead->record_numb);
}
void head2_info_print(Message_Head2 * phead)
{
printf("\noper_type is %u\n",phead->oper_type);
printf("packet_length is %u\n",phead->pack_length);
printf("packet_date is %u\n",phead->pack_date);
printf("head_length is %u\n",phead->head_length);
printf("record_numb is %u\n",phead->record_numb);
}
void body1_info_print(Message_Body1 * pbody)
{
printf("\ntrader_serial is %u\n",pbody->trader_serial);
printf("contract_id is %s\n",pbody->contract_id);
printf("currency is %u\n",pbody->currency);
}
void body2_info_print(Message_Body2 * pbody)
{
printf("\ntrader_serial is %u\n",pbody->trader_serial);
printf("contract_id is %s\n",pbody->contract_id);
printf("currency is %u\n",pbody->account_balance);
printf("resp_type is %u\n",pbody->resp_type);
}
void tail_info_print(Message_Tail * ptail)
{
printf("\ntail_length is %u\n",ptail->tail_length);
printf("tail_field_numb is %u\n",ptail->tail_field_numb);
printf("trader_id is %s\n",inet_ntoa(ptail->tail_field_data.trader_id));
printf("check_code is %u\n",ptail->tail_field_data.check_code);
printf("sum is %u\n",ptail->tail_field_data.sum);
}
format2net(Message_Head1 * phead,Message_Body1 * pbody,Message_Tail * ptail)
{
int i;
phead->oper_type=htons(phead->oper_type);
phead->pack_length=htonl(phead->pack_length);
phead->pack_date=htonl(phead->pack_date);
phead->head_length=htons(phead->head_length);
phead->record_numb=htons(phead->record_numb);
for(i=0;i<3;i++)
{
phead->descript[i].field_descript=htons(phead->descript[i].field_descript);
}
pbody->trader_serial=htonl(pbody->trader_serial);
pbody->currency=htonl(pbody->currency);
ptail->tail_length=htons(ptail->tail_length);
ptail->tail_field_numb=htons(ptail->tail_field_numb);
ptail->tail_field_descript.field_descript=htons(ptail->tail_field_descript.field_descript);
ptail->tail_field_data.check_code=htonl(ptail->tail_field_data.check_code);
ptail->tail_field_data.sum=htonl(ptail->tail_field_data.sum);
}
format2local(Message_Head2 * phead,Message_Body2 * pbody,Message_Tail * ptail)
{
int i;
phead->oper_type=ntohs(phead->oper_type);
phead->pack_length=ntohl(phead->pack_length);
phead->pack_date=ntohl(phead->pack_date);
phead->head_length=ntohs(phead->head_length);
phead->record_numb=htons(phead->record_numb);
for(i=0;i<4;i++)
{
phead->descript[i].field_descript=ntohs(phead->descript[i].field_descript);
}
pbody->trader_serial=ntohl(pbody->trader_serial);
pbody->account_balance=ntohl(pbody->account_balance);
ptail->tail_length=ntohs(ptail->tail_length);
ptail->tail_field_numb=ntohs(ptail->tail_field_numb);
ptail->tail_field_descript.field_descript=ntohs(ptail->tail_field_descript.field_descript);
ptail->tail_field_data.check_code=ntohl(ptail->tail_field_data.check_code);
ptail->tail_field_data.sum=ntohl(ptail->tail_field_data.sum);
}
int transaction(struct sockaddr_in * psa,unsigned int oper_type,Message_Body1 * psndbody,Message_Body2 * prcvbody){
int s;
int ret_code;
unsigned char sendbuff[256];
unsigned char recvbuff[256];
Message_Head1 head;
Message_Tail tail;
Message_Head2 rcvhead;
Message_Tail rcvtail;
memset(sendbuff,0,sizeof(sendbuff));
memset(recvbuff,0,sizeof(sendbuff));
if((s=opensocket())==-1)
return s;//open socket
if((ret_code=makeconnection(s,psa)))//make connetion
return ret_code;
head=head1_assemble(oper_type);//assemble the head
tail=tail_assemble(&head,psndbody,s);//assemble the tail
head1_info_print(&head);//print out the head,body,tail
body1_info_print(psndbody);
tail_info_print(&tail);
format2net(&head,psndbody,&tail);//transform to network format
add_check_sum(&head,&tail);// add the check_sum
memcpy(sendbuff,&head,sizeof(head));//copy to buffer
memcpy(sendbuff+sizeof(head),psndbody,sizeof(*psndbody));
memcpy(sendbuff+sizeof(head)+sizeof(*psndbody),&tail,sizeof(tail));
if(send(s,sendbuff,sizeof(sendbuff),0)<=0)
{
printf("Error in Send Message\n");
return(-3);
}
else
{
if(recv(s,recvbuff,sizeof(recvbuff),0)<=0)
{
printf("Error in Receive Response\n");
return(-4) ;
}
else
{
printf("\nSuccess in receive response\n");
memcpy(&rcvhead,recvbuff,sizeof(rcvhead));
memcpy(prcvbody,(recvbuff+sizeof(rcvhead)),sizeof(*prcvbody));
memcpy(&rcvtail,(recvbuff+sizeof(rcvhead)+sizeof(*prcvbody)),sizeof(rcvtail));
format2local(&rcvhead,prcvbody,&rcvtail);//transform to local format
head2_info_print(&rcvhead);//print out the head,body and tail
body2_info_print(prcvbody);
tail_info_print(&rcvtail);
}
}
close(s);
return 0;
}
int main(int argc,char *argv[])
{
struct sockaddr_in sa; //declare server socket address
Message_Body1 * psndbody; //declare send message body
Message_Body2 * prcvbody; //declare receive message body
unsigned int oper_type; //declare operation type
unsigned int before,after,currency;
int i,result;
ReadConfigFile(&sa); //Read Server IP & Port from Config File
psndbody=(Message_Body1 *)malloc(sizeof(*psndbody));
prcvbody=(Message_Body2 *)malloc(sizeof(*prcvbody));
printf("input messge to send:"); //enter operation type
scanf("%d",&oper_type);
body1_assemble(psndbody); //assembel body for test
//function:transaction, complement the transaction,including open connection,send message,receive message,close connection
// return code:
// 0 success
// -1 open socket error
// -2 make connection error
// -3 send message error
// -4 receive message error
if(transaction(&sa,oper_type,psndbody,prcvbody)==0)
{
printf("transaction success\n");
}
getchar();
getchar();
/* for(i=1;i<=100;i++)
{
result=random()%2;
if(result==0)
oper_type=1;
else if(result==1)
oper_type=3;
//oper_type=1;
body1_assemble(psndbody);
//currency=psndbody->currency;
transaction(&sa,oper_type,psndbody,prcvbody);
//oper_type=5;
//body1_assemble(psndbody);
//transaction(&sa,oper_type,psndbody,prcvbody);
if(after=prcvbody->resp_type!=0)
{
printf("error resp_type is %d",prcvbody->resp_type);
break;
}
sleep(1);
}*/
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -