深度学习
#include <winsock2.h> #include <stdio.h> #include <string.h> #include <stdlib.h> #include <time.h> #include "windows.h" #include <iostream> #include <math.h> 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<len)) { i++; } left=i; while((buf[i]>='0'&&buf[i]<='9')&&(i<len)) { i++; } if(flag==0) { memset(ch,0,sizeof(ch)); memcpy(ch,buf+left,i-left); *min = atoi(ch); flag++; } else { memset(ch,0,sizeof(ch)); memcpy(ch,buf+left,i-left); *max = atoi(ch); flag++; } } } void get_system_time(char *time_sys) { SYSTEMTIME lpsystime; GetLocalTime(&lpsystime); printf("%u,%u,%u,%u,%u,%u,%u,%u\n",lpsystime.wYear,lpsystime.wMonth, lpsystime.wDayOfWeek,lpsystime.wDay,lpsystime.wHour,lpsystime.wMinute,lpsystime.wSecond,lpsystime.wMilliseconds); time_sys[0]=(char)(lpsystime.wYear%2000); time_sys[1]=(char)(lpsystime.wMonth); time_sys[2]=(char)(lpsystime.wDay); time_sys[3]=(char)(lpsystime.wHour); time_sys[4]=(char)(lpsystime.wMinute); time_sys[5]=(char)(lpsystime.wSecond); memcpy(time_sys+6,(char *)&(lpsystime.wMilliseconds),2); } int main(int argc, char**argv) { int i; char buf[100]; char name[32]; int ret=SUCCESS; int variable_sum=0; int send_len=0; int Ret; WSADATA wsaData; SOCKADDR_IN ServerAddr; SOCKADDR_IN ClientAddr; int ClientAddrLen = sizeof(ClientAddr); unsigned short Port = 8888; char sendData[BUFFSIZE]; char recvData[BUFFSIZE]; //控制台异常退出事件 SetConsoleCtrlHandler(HandlerRoutine, TRUE); while(1) { if((Ret = WSAStartup(MAKEWORD(2,2), &wsaData)) != 0) { printf("WSASTARTUP_ERROR: %d\n", Ret); return 0; } //创建一个套接字来监听客户机连接 if((ListeningSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == INVALID_SOCKET) { printf("SOCKET_ERROR: %d\n", INVALID_SOCKET); return 0; } /* * 填充SOCKADDR_IN结构,这个结构将告知bind我们想要在5150端口监听所有接口上的连接 */ ServerAddr.sin_family = AF_INET; ServerAddr.sin_port = htons(Port); //将端口变量从主机字节顺序转换位网络字节顺序 ServerAddr.sin_addr.S_un.S_addr = htonl(INADDR_ANY); //使用bind将这个地址信息和套接字绑定起来 if(bind(ListeningSocket, (SOCKADDR *)&ServerAddr, sizeof(ServerAddr)) == SOCKET_ERROR) { printf("BIND_ERROR: %d\n", SOCKET_ERROR); return 0; } //监听客户机连接,这里使用5个backlog if(listen(ListeningSocket, 5) == SOCKET_ERROR) { printf("LISTEN_ERROR: %d\n", SOCKET_ERROR); return 0; } //连接到达时,接受连接 printf("正在接受连接...\n"); if((NewConnection = accept(ListeningSocket, (SOCKADDR *)&ClientAddr, &ClientAddrLen)) == INVALID_SOCKET) { printf("ACCPET_ERROR: %d\n", INVALID_SOCKET); closesocket(ListeningSocket); return 0; } printf("检测到一个连接: %s 端口:%d\n", inet_ntoa(ClientAddr.sin_addr), ntohs(ClientAddr.sin_port)); /*************************************************************/ srand((unsigned)time(NULL)); //通过配置文件读取变量总个数; ret = get_cfg_val("config.cfg", "VARIABLESUM", buf, sizeof(buf)); if(ret != 0) { printf("get_cfg_val fail!\n"); } variable_sum = atoi(buf); printf("variable_sum is :%d\n",variable_sum); data_range = (DATA_RANGE *)malloc(sizeof(DATA_RANGE)*variable_sum); buf_data = (BUF_DATA *)malloc(sizeof(BUF_DATA)*variable_sum); buf_send = (BUF_SEND *)malloc(sizeof(BUF_SEND)); buf_send->protocol = 0x1a1b1c1d; //通过配置文件读取每个变量的取值范围 for(i=0;i<variable_sum;i++) { memset(name,0,sizeof(name)); sprintf(name, "VARIABLE%d", i+1); ret = get_cfg_val("config.cfg", name, buf, sizeof(buf)); if(ret != 0) { printf("get_cfg_val fail!\n"); } get_min_and_max(buf,&(data_range[i].min),&(data_range[i].max)); } int t=0; int tmp_protcol=0; while(true) { //接收数据 memset(recvData,0,sizeof(recvData)); Ret = recv(NewConnection, recvData, BUFFSIZE, 0); if(Ret > 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<variable_sum;i++) { buf_data[i].num_sensor = i; buf_data[i].num_time = rand()%10; buf_data[i].num_dest = rand()%10; buf_data[i].val = dataRound(getRandData(data_range[i].min,data_range[i].max),2); printf("buf_data[i].num_sensor is :%d\n",buf_data[i].num_sensor); printf("buf_data[i].num_time is :%d\n",buf_data[i].num_time); printf("buf_data[i].num_dest is :%d\n",buf_data[i].num_dest); printf("buf_data[i].val is :%f\n",buf_data[i].val); printf(".................................\n"); } memset(buf_send->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
微信号:qq444848023 QQ号:444848023
加入【我是码农】QQ群:864689844(加群验证:我是码农)
全站首页 | 数据结构 | 区块链| 大数据 | 机器学习 | 物联网和云计算 | 面试笔试
var cnzz_protocol = (("https:" == document.location.protocol) ? "https://" : "http://");document.write(unescape("%3Cspan id='cnzz_stat_icon_1276413723'%3E%3C/span%3E%3Cscript src='" + cnzz_protocol + "s23.cnzz.com/z_stat.php%3Fid%3D1276413723%26show%3Dpic1' type='text/javascript'%3E%3C/script%3E"));本站资源大部分来自互联网,版权归原作者所有!
评论专区