#include#include #include #include #include #include "windows.h" #include #include using namespace std; //首先声明了一个回调函数,也就是钩子函数 BOOL WINAPI HandlerRoutine(DWORD dwCtrlType); #pragma comment(lib,"ws2_32.lib") #define BUFFSIZE 1024 #define MAX_CFG_BUF_LEN 200 #define MAX_DATA_LEN 8192 #define TIME_LEN 8 #define ERR -1 #define SUCCESS 0 #pragma pack (4) //指定按4字节对齐 typedef struct { int protocol; //协议标志:0x1a1b1c1d char time[TIME_LEN]; //发送时间 int len; //数据长度 char data[MAX_DATA_LEN]; }BUF_SEND; typedef struct { int num_sensor; //传感器序号 int num_time; //时间序号 int num_dest; //目的端序号 double val; }BUF_DATA; typedef struct { int min; int max; } DATA_RANGE; #pragma pack ()/*取消指定对齐,恢复缺省对齐*/ //定义全局变量 DATA_RANGE *data_range=NULL; BUF_DATA *buf_data=NULL; BUF_SEND *buf_send=NULL; SOCKET ListeningSocket; SOCKET NewConnection; double dataRound(double a,int digi) { int mult=(int)pow(10,digi); return (double)((int)(a*mult))/mult; } double getRandData(int min,int max) { double m1=(double)(rand()%101)/101; // 计算 0,1之间的随机小数,得到的值域近似为(0,1) min++; //将 区间变为(min+1,max), double m2=(double)((rand()%(max-min+1))+min); //计算 min+1,max 之间的随机整数,得到的值域为[min+1,max] m2=m2-1; //令值域为[min,max-1] return m1+m2; //返回值域为(min,max),为所求随机浮点数 } int get_cfg_val( char *file_name,char *name, char *val_buf, int buf_len) { int ret = SUCCESS; int len, name_len, found = 0; FILE *fp = NULL; char tmp_buf[MAX_CFG_BUF_LEN]; if((file_name==NULL) || (name==NULL) || (val_buf==NULL) || (buf_len == 0)) { return ERR; } fp = fopen(file_name, "r"); if(fp == NULL) { val_buf[0] = '\0'; return ERR; } name_len = strlen(name); while(feof(fp) == 0) { memset(tmp_buf, '\0', sizeof(tmp_buf)); if(fgets(tmp_buf, sizeof(tmp_buf), fp) == NULL) { printf("fail fgets data!\n"); } if(tmp_buf[0] == '#') { continue; } if((strncmp(tmp_buf, name, name_len) == 0) && (tmp_buf[name_len]=='=')) { found=1; len = strlen(tmp_buf) - (name_len+1) - 1; // -1 skip the tailing nameLen+1是变量名称加上等号的长度 if(len >= buf_len) { ret = ERR; val_buf[0] = '\0'; break;//value缓存区不够大,读取数据失败 } strncpy(val_buf, tmp_buf+name_len+1, len); //+1 skip the '=' val_buf[len] = '\0'; break; } } fclose(fp); fp=NULL; if(found == 0) { val_buf[0] = '\0'; return ERR; } return ret; } void get_min_and_max(char *buf,int *min,int *max) { int i=0,flag=0,left; char ch[10]; int len; len = strlen(buf); while(flag<2) { while((!(buf[i]>='0'&&buf[i]<='9'))&&(i ='0'&&buf[i]<='9')&&(i protocol = 0x1a1b1c1d; //通过配置文件读取每个变量的取值范围 for(i=0;i 0) { memcpy((char *)&tmp_protcol,recvData,4); if(tmp_protcol == 0x1a1b1c1d) { printf("接收到的协议头为: %x\n", tmp_protcol); printf("协议头正确,开始向客户端发送数据!\n"); } else { printf("协议头错误!"); break; } } else if(Ret < 0) { printf("RECV_ERROR: %d\n", SOCKET_ERROR); break; } else { printf("对方退出程序,聊天结束!\n"); break; } while(true) { //发送数据 t++; if(t==1000)break; //定时发送随机数 for(i=0;i time,0,sizeof(buf_send->time)); memset(buf_send->data,0,sizeof(buf_send->data)); //获取当前系统时间,精确到ms get_system_time(buf_send->time); buf_send->len = sizeof(BUF_DATA)*variable_sum; memcpy(buf_send->data,(char *)buf_data,sizeof(BUF_DATA)*variable_sum); send_len=4+TIME_LEN+4+buf_send->len; if(send(NewConnection, (char *)buf_send, send_len, 0) == SOCKET_ERROR) { printf("消息发送失败!\n"); break; } Sleep(15000); printf("消息发送成功!\n"); } } //从容关闭 shutdown(NewConnection, SD_BOTH); //完成新接受的连接后,用closesocket API关闭这些套接字 closesocket(NewConnection); closesocket(ListeningSocket); //应用程序完成对接的处理后,调用WSACleanup if(WSACleanup() == SOCKET_ERROR) { printf("WSACLEANUP_ERROR: %d\n", WSAGetLastError()); return 0; } } system("pause"); return 0; } //异常退出控制台程序前进行回收处理 BOOL WINAPI HandlerRoutine(DWORD dwCtrlType) { // 控制台将要被关闭 if(CTRL_CLOSE_EVENT == dwCtrlType) { //从容关闭 shutdown(NewConnection, SD_BOTH); //完成新接受的连接后,用closesocket API关闭这些套接字 closesocket(NewConnection); closesocket(ListeningSocket); } return TRUE; }
配置文件名字是config.cfg
内容如下:
/*需要生成的随机变量总数*/
VARIABLESUM=6
/**/
VARIABLE1=[0,10]
VARIABLE2=[10,20]
VARIABLE3=[30,40]
VARIABLE4=[40,50]
VARIABLE5=[50,60]
VARIABLE6=[60,70]
VARIABLE7=[70,80]
//the end
来源:我是码农,转载请保留出处和链接!
本文链接:http://www.54manong.com/?id=1226