1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
| #include<sys/socket.h> #include<arpa/inet.h> #include<cstring> #include<unistd.h> #include<iostream> #include<thread> #include<unistd.h> #include<random> #include<unordered_map> using namespace std;
#define REQUESTDATA_SIZE 20 #define REQUESTHEADER_SIZE 8 #define REQUESTBODY_SIZE 12 #define RESPONSEDATA_SIZE 16 #define RESPONSEHEADER_SIZE 8 #define RESPONSEBODY_SIZE 8
#define ENCODE 1 #define ALIVE 2 #define ADD 0 #define SUB 1 #define TIMES 2 #define DIVIDE 3
struct RequestHeader{ short type; int bodylen; }; struct RequestBody{ short method; int num1; int num2; }; struct RequestData{ struct RequestHeader header; struct RequestBody body; };
struct ResponseHeader{ short type; int bodylen; }; struct ResponseBody{ short method; int result; }; struct ResponseData{ struct ResponseHeader header; struct ResponseBody body; };
unordered_map<int,string> um={ {ADD,"ADD"}, {SUB,"SUB"}, {TIMES,"TIMES"}, {DIVIDE,"DIVIDE"} };
random_device rd; mt19937 gen(rd()); uniform_int_distribution<> randtype(0, 2); uniform_int_distribution<> randmethod(0, 3); uniform_int_distribution<> randnum(0, 100);
void postfunc(string id,int server_fd,struct sockaddr_in server_addr,struct RequestData reqdata){ if(connect(server_fd,(sockaddr*)&server_addr,sizeof(server_addr))==-1){ perror("connect"); return ; } reqdata.header.type=randtype(gen); reqdata.body.method=randmethod(gen); reqdata.body.num1=randnum(gen); reqdata.body.num2=randnum(gen); if((reqdata.header.type&ENCODE)){ reqdata.body.num1+=10; reqdata.body.num2+=10; } ssize_t size=send(server_fd,(void*)&reqdata,REQUESTDATA_SIZE,0); if(size<0){ perror("send"); exit(EXIT_FAILURE); }else if(size==0){ perror("connect closed"); }else{ cout<<"thread"<<id<<" send success"<<"\n"; } struct ResponseData resdata; size=recv(server_fd,(void*)&resdata,sizeof(resdata),0); if(size<0){ perror("recv"); }else if(size==0){ perror("connect closed"); }else{ cout<<"thread"<<id<<":"<<"[ENCODE="<<(reqdata.header.type&ENCODE)<<" ALIVE="<<(reqdata.header.type&ALIVE) <<"num1="<<reqdata.body.num1<<" "<<um[reqdata.body.method]<<" num2="<<reqdata.body.num2<<" Result="<<resdata.body.result<<"\n"; } }
int main(int argc,char* argv){ string id; if(argc==0){ cout<<"no id args"<<"\n"; }else{ id=argv[0]; }
int server_fd=socket(AF_INET,SOCK_STREAM,0); if(server_fd==-1){ perror("socket");exit(EXIT_FAILURE); } struct sockaddr_in server_addr; string server_ip="127.0.0.1"; uint16_t server_port=8001; server_addr.sin_family=AF_INET; server_addr.sin_port=htons(server_port); inet_pton(AF_INET,server_ip.c_str(),&server_addr.sin_addr.s_addr); struct RequestHeader reqheader; struct RequestBody reqbody; reqheader.type=0; reqheader.bodylen=REQUESTBODY_SIZE; reqbody.method=0; reqbody.num1=0; reqbody.num2=0; struct RequestData reqdata; reqdata.header=reqheader; reqdata.body=reqbody; postfunc(id,server_fd,server_addr,reqdata); return 0; }
|