📄 main.c
字号:
m_log=SetLogContent(L_Error,"open profile failed","in readprofile functin ,cannot open the ini file"); writeLog(&m_log); return "failed"; }/*----------------------------------------------------------cmp_time():compare server's the time with the local time---------------------------------------------------------*/void cmp_time(char *mz_buffer){ struct tm *m_curr,m_packtime; char m_year[5]; memcpy(m_year,mz_buffer+3,4); m_year[4]='\0'; m_packtime.tm_year=atoi(m_year); memcpy(m_year,mz_buffer+7,2); m_year[2]='\0'; m_packtime.tm_mon=atoi(m_year); memcpy(m_year,mz_buffer+9,2); m_year[2]='\0'; m_packtime.tm_mday=atoi(m_year); memcpy(m_year,mz_buffer+11,2); m_year[2]='\0'; m_packtime.tm_hour=atoi(m_year); memcpy(m_year,mz_buffer+13,2); m_year[2]='\0'; m_packtime.tm_min=atoi(m_year); memcpy(m_year,mz_buffer+14,2); m_year[2]='\0'; m_packtime.tm_sec=atoi(m_year); m_curr=getlocaldatetime(); if((m_packtime.tm_year!=m_curr->tm_year)||(m_packtime.tm_mon!=m_curr->tm_mon)|| (m_packtime.tm_mday!=m_curr->tm_mday)||(m_packtime.tm_hour!=m_curr->tm_hour)|| (abs(m_packtime.tm_min-m_curr->tm_min)>3)) SetTime(mz_buffer); }/*---------------------------------------------------------------ReadANdEcho():read from the socket and display the recived message---------------------------------------------------------------*/int ReadAndEcho(SOCKET insock, char *Buffer,int size,struct sockaddr_in *m_from,int* m_fromlen) { int rc; WSLOGDATA m_log; if( (rc=recvfrom(insock,Buffer,size,0,(struct sockaddr *)m_from,m_fromlen))<0) { printf("receive from error:%d",WSAGetLastError()); m_log=SetLogContent(L_Error,"recievefron error","in readandecho function ,the recvfrom call failed"); writeLog(&m_log); return 1; } if (rc == SOCKET_ERROR) { fprintf(stderr,"recv() failed with error %d\n",WSAGetLastError()); m_log.log_level=10; sprintf(m_log.log_event,"recv() failed\0"); sprintf(m_log.log_cause,"with error %d\0",WSAGetLastError()); writeLog(&m_log); return 1; } if (rc ==0) { fprintf(stderr,"Connection closed by client\n"); m_log.log_level=10; sprintf(m_log.log_event,"Connection closed by client\0"); sprintf(m_log.log_cause,""); writeLog(&m_log); return 1; } printf("network is on , timer thread Received : [%s].\n Local host can communicate with:[%s]\n",Buffer,inet_ntoa(m_from->sin_addr)); return 0;}/*------------------------------------------------------Writemessage():send the message to the client------------------------------------------------------*/int WriteMessage(SOCKET outsock, char *Buffer,int size,struct sockaddr_in m_from,int m_fromlen) { int rc; WSLOGDATA m_log; int lasterr=0; char temp[100]; printf("Sending [%s] to server\n",Buffer); rc = sendto(outsock,Buffer,size, 0,(struct sockaddr *)&m_from,m_fromlen); if (rc == SOCKET_ERROR) { /* lasterr = WSAGetLastError(); */ fprintf(stderr,">>sendto() failed with error %d\n",lasterr); m_log=SetLogContent(L_Warning,"send to error","in writemessage function ,the sendto function call failed,maybe server down"); writeLog(&m_log); fprintf(stderr,">>sendto() failed with error %d\n",lasterr); sprintf(temp," in writeMessage function sendto function failed \ with error %d\0",WSAGetLastError()); m_log=SetLogContent(L_Error,"send() failed\0",temp); writeLog(&m_log); return -1; } if (rc ==0) { fprintf(stderr,">>Connection closed by client\n"); m_log=SetLogContent(L_Warning,"sendto error","in writemessage function when call sendto \ function ,the connection close by server \ maybe server problem"); writeLog(&m_log); } return rc;}/*----------------------------------------------------------------writeprofile():write the section,key and key values into thefile(m_file)----------------------------------------------------------------*/int Writeprofile(char *m_file,char m_section[],char m_key[],char m_keyvalue[]){ char *m_head1,*m_head2; FILE *fp2; int i,j,k; char m_temfile[1000],m_tembottom[1000]; int m_valuelen; int head2_size; int numwritten; if (strlen(m_section)<1 || strlen(m_key)<1 || strlen(m_file) < 1 || strlen(m_keyvalue) <1) return 2; fp2=fopen(m_file,"r+"); if(fp2==NULL) { printf("open the file %s failed!\n",m_file); return 2; } i=0; while(!feof(fp2)) { m_temfile[i++]=fgetc(fp2); } m_temfile[i++]='\n'; m_temfile[i++]='\0'; fclose(fp2); m_head1=strstr(m_temfile,m_section); if(m_head1==NULL) return 2; if((m_head2=strstr(m_head1,m_key))==NULL) return 2; while(m_head1 <m_head2) { if(*(m_head1++)=='[') return 2; } if((m_head1=strstr(m_head2,"="))==NULL) return 2; if((m_head2=strstr(m_head2,"\n"))==NULL) return 2; if(m_head2<m_head1) return 2; m_head1++; j=strlen(m_temfile) -strlen(m_head1) ; head2_size=strlen(m_head2); for(k=0;k< head2_size;k++) { m_tembottom[k]=*(m_head2++); } m_tembottom[k]='\0'; m_valuelen=strlen(m_keyvalue); for(k=0;k<m_valuelen;k++) { m_temfile[j++]=m_keyvalue[k]; } m_temfile[j]='\0'; strcat(m_temfile,m_tembottom); m_temfile[strlen(m_temfile)-1]='\0'; fp2=fopen(m_file,"w+t"); if(fp2==NULL) { printf("open the file %s failed!\n",m_file); return 2; } numwritten = fwrite( m_temfile, sizeof( char ),strlen(m_temfile)-1, fp2 ); printf( "Wrote %d items\n", numwritten ); fclose(fp2); if(numwritten) return 0; else return 2;}/*----------------------------------------------------------------------writelog():write the log information into the log file----------------------------------------------------------------------*/int writeLog(WSLOGDATA *m_log){ FILE *fp1; struct tm *m_curr; long m_logsize; char m_last512[751]; if((fp1=fopen(LOGFILE,"a+"))==NULL) { printf("open log file %s failed!\n",LOGFILE); return 2; } if(fseek(fp1,0L,SEEK_END)) printf("seek failed!\n"); m_logsize=ftell(fp1); if(m_logsize>10000000) { printf(">>the filesize is %d\n",m_logsize); fseek(fp1,m_logsize-750,SEEK_SET); fread(m_last512,sizeof(char),750 ,fp1); fclose(fp1); if((fp1=fopen(LOGFILE,"w+t"))==NULL) { printf(">>open log file failed!\n"); return 2; } m_last512[738]='\n'; m_last512[739]='\0'; printf("the 512 bytes are %d",strlen(m_last512)); fputs(m_last512,fp1); fclose(fp1); } if((fp1=fopen(LOGFILE,"a"))==NULL) { printf("open log file failed!\n"); return 2; } m_curr=getlocaldatetime(); sprintf(m_log->log_date,"%4d/%02d/%02d",m_curr->tm_year,m_curr->tm_mon,m_curr->tm_mday); if(!fprintf(fp1,"Log level:%-4d%-12s%-10s%s %s\n",m_log->log_level,m_log->log_date,m_log->log_time,m_log->log_event,m_log->log_cause)) printf("write logfile failed!"); fclose(fp1); return 0;}/*------------------------------------------------------------getlocaldatetime():get the local system data and time valuesThis function can resolve the 2k year problem------------------------------------------------------------*/struct tm * getlocaldatetime(void){ struct tm *m_newtime; time_t long_time ; time( &long_time); m_newtime = localtime( &long_time); if(m_newtime->tm_year < 99) m_newtime->tm_year = 2000 + m_newtime->tm_year; else m_newtime->tm_year = 1900 + m_newtime->tm_year; m_newtime->tm_mon ++; return m_newtime;}/*------------------------------------------------------------FormatTestPacket():make up the test packet to send to the server or client------------------------------------------------------------*/void FormateTestPacket(char *r_buffer,char *r_type){ struct tm *r_testdate; r_testdate=getlocaldatetime(); sprintf(m_testpac.Testtype,r_type); m_testpac.Legal='I'; sprintf(m_testpac.Date,"%4d%02d%02d",r_testdate->tm_year,r_testdate->tm_mon,r_testdate->tm_mday); sprintf(m_testpac.Time,"%02d%02d%02d",r_testdate->tm_hour,r_testdate->tm_min,r_testdate->tm_sec); sprintf(r_buffer,"%3s%8s%6s%1s",m_testpac.Testtype,m_testpac.Date,m_testpac.Time,m_testpac.Testtype,m_testpac.Legal);}/*---------------------------------------------------------------------RecieveMessge():this function recieve the message from the server or middelwareIf no message be recieved and the timeout occure then return the error information or return the recieved message size--------------------------------------------------------------------------*/int ReceiveMessage(SOCKET insock, char *Buffer,int size,struct sockaddr_in *m_from,int* m_fromlen) { int rc; WSLOGDATA m_log; struct timeval z_time; fd_set readfds,writefds,exceptfds; int i; int nfds; z_time.tv_sec=0; z_time.tv_usec=700; nfds=getdtablesize(); FD_ZERO(&readfds); FD_ZERO(&writefds); FD_ZERO(&exceptfds); FD_SET(insock,&readfds); i=select(nfds,&readfds,&writefds,&exceptfds,&z_time); if(i<0) { printf("the queuethread function select error :%d\n",WSAGetLastError()); SetLogContent(L_Error,"select errror","in the queuethread function when call select API an error occured"); writeLog(&m_log); return 1; } if(i==0) { /* printf(">>in queuethread there is no socket ready\n"); */ return 1; } if(FD_ISSET(insock,&readfds)) { memset(Buffer,'\0',size); rc = recvfrom(insock,Buffer,size,0,(struct sockaddr *)m_from,m_fromlen); if (rc == SOCKET_ERROR) { fprintf(stderr,"recv() failed with error %d\n",WSAGetLastError()); m_log.log_level=10; sprintf(m_log.log_event,"recv() failed\0"); sprintf(m_log.log_cause,"with error %d\0",WSAGetLastError()); writeLog(&m_log); return 1; } if (rc ==0) { fprintf(stderr,"Connection closed by client\n"); m_log.log_level=10; sprintf(m_log.log_event,"Connection closed by client\0"); sprintf(m_log.log_cause,""); writeLog(&m_log); return 1; } printf("Queue Thread Received [%s]\n",Buffer); return 0; } return 1;}/*-------------------------------------------------------------------------SendMsg():this function send the message to the server or middleware.Iferror occrued then return the error information; return the recieved message size--------------------------------------------------------------------------*/int SendMsg(SOCKET outsock, char *Buffer,int size,struct sockaddr_in m_from,int m_fromlen) { int rc; int lasterr; WSLOGDATA m_log; rc = sendto(outsock,Buffer,size, 0,(struct sockaddr *)&m_from,m_fromlen); if (rc == SOCKET_ERROR) { m_log.log_level=10; sprintf(m_log.log_event,"send() failed\0"); sprintf(m_log.log_cause,"with error %d\0",WSAGetLastError()); writeLog(&m_log); return -1; } if (rc ==0) { fprintf(stderr,"Connection closed by client\n"); m_log.log_level=10; sprintf(m_log.log_event,"Connection closed by client\0"); sprintf(m_log.log_cause,""); writeLog(&m_log); } printf("Sending [%s] to %s\n",Buffer,m_from.sin_addr.s_addr); return rc;}/*------------------------------------------------------------------GetRcordID():this function get the the record from the file------------------------------------------------------------------*/unsigned long GetRecordID(void){ FILE *m_file; unsigned long m_recid; char m_idno[11]; m_file=fopen("recordid.txt","r+"); if(m_file==NULL) printf("not open the file record.txt\n"); rewind(m_file); fread(m_idno,sizeof( char ),10,m_file); m_idno[10]='\0'; m_recid=atol(m_idno); rewind(m_file); fprintf(m_file,"%010d\n",m_recid+1); fclose(m_file); return m_recid;}/*---------------------------------------------------------------SetTime():compare the server's time and sycrinize it--------------------------------------------------------------*/void SetTime(char * m_centertime)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -