📄 main.c
字号:
int Init(){ WSLOGDATA m_log; char *m_tmpchar; /*--------------------------------------------------- init the server and client information---------------------------------------------------*/ m_tmpchar=Readprofile(INIFILE,"MAXQUEUELEN","MAXQUEUELEN"); if(strcmp(m_tmpchar,"failed")==0) { printf("cannot set the correct values,but continue\n"); m_log=SetLogContent(L_Warning,"cannot set the correct values to variabl","the ini file corupt but continue to run"); writeLog(&m_log); } MAXQUEUELEN=atoi(m_tmpchar);/*--------------------------------------------------- init the server and client information---------------------------------------------------*/ memset(&remote_ser[1],0,sizeof(remote_ser[1])); m_tmpchar=Readprofile(INIFILE,"REMOTEPC1","RemoteIP"); if(strcmp(m_tmpchar,"failed")==0) { printf("cannot set the correct values,but continue\n"); m_log=SetLogContent(L_Warning,"cannot set the correct values to variabl","the ini file corupt but continue to run"); writeLog(&m_log); } sprintf(remote_ser[1].RemoteIP,m_tmpchar); remote_ser[1].RemoteIP[strlen(m_tmpchar)]='\0'; m_tmpchar=Readprofile(INIFILE,"REMOTEPC1","RemotePort"); if(strcmp(m_tmpchar,"failed")==0) { printf("cannot set the correct values,but continue\n"); m_log=SetLogContent(L_Warning,"cannot set the correct values to variabl","the ini file corupt but continue to run"); writeLog(&m_log); }/* remote_ser[1].RemotePort =htons(atoi(m_tmpchar));*/ remote_ser[1].RemotePort =atoi(m_tmpchar); m_tmpchar=Readprofile(INIFILE,"REMOTEPC1","Rem_testPort"); if(strcmp(m_tmpchar,"failed")==0) { printf("cannot set the correct values,but continue\n"); m_log=SetLogContent(L_Warning,"cannot set the correct values to variabl","the ini file corupt but continue to run"); writeLog(&m_log); } /* remote_ser[1].Rem_testPort =htons(atoi(m_tmpchar)); */ remote_ser[1].Rem_testPort =atoi(m_tmpchar); m_tmpchar=Readprofile(INIFILE,"REMOTEPC1","RemoteID"); if(strcmp(m_tmpchar,"failed")==0) { printf("cannot set the correct values,but continue\n"); m_log=SetLogContent(L_Warning,"cannot set the correct values to variabl","the ini file corupt but continue to run"); writeLog(&m_log); } remote_ser[1].RemoteID=atoi(m_tmpchar); m_tmpchar=Readprofile(INIFILE,"REMOTEPC1","RemoteLevef"); if(strcmp(m_tmpchar,"failed")==0) { printf("cannot set the correct values,but continue\n"); m_log=SetLogContent(L_Warning,"cannot set the correct values to variabl","the ini file corupt but continue to run"); writeLog(&m_log); } remote_ser[1].RemoteLevef=atoi(m_tmpchar); memset(&remote_ser[2],0,sizeof(remote_ser[2])); m_tmpchar=Readprofile(INIFILE,"REMOTEPC2","RemoteIP"); if(strcmp(m_tmpchar,"failed")==0) { printf("cannot set the correct values,but continue\n"); m_log=SetLogContent(L_Warning,"cannot set the correct values to variabl","the ini file corupt but continue to run"); writeLog(&m_log); } sprintf(remote_ser[2].RemoteIP,m_tmpchar); remote_ser[2].RemoteIP[strlen(m_tmpchar)]='\0'; m_tmpchar=Readprofile(INIFILE,"REMOTEPC2","RemotePort"); if(strcmp(m_tmpchar,"failed")==0) { printf("cannot set the correct values,but continue\n"); m_log=SetLogContent(L_Warning,"cannot set the correct values to variabl","the ini file corupt but continue to run"); writeLog(&m_log); } remote_ser[2].RemotePort =atoi(m_tmpchar); m_tmpchar=Readprofile(INIFILE,"REMOTEPC2","Rem_testPort"); if(strcmp(m_tmpchar,"failed")==0) { printf("cannot set the correct values,but continue\n"); m_log=SetLogContent(L_Warning,"cannot set the correct values to variabl","the ini file corupt but continue to run"); writeLog(&m_log); } remote_ser[2].Rem_testPort =atoi(m_tmpchar); m_tmpchar=Readprofile(INIFILE,"REMOTEPC2","RemoteID"); if(strcmp(m_tmpchar,"failed")==0) { printf("cannot set the correct values,but continue\n"); m_log=SetLogContent(L_Warning,"cannot set the correct values to variabl","the ini file corupt but continue to run"); writeLog(&m_log); } remote_ser[2].RemoteID=atoi(m_tmpchar); m_tmpchar=Readprofile(INIFILE,"REMOTEPC2","RemoteLevef"); if(strcmp(m_tmpchar,"failed")==0) { printf("cannot set the correct values,but continue\n"); m_log=SetLogContent(L_Warning,"cannot set the correct values to variabl","the ini file corupt but continue to run"); writeLog(&m_log); } remote_ser[2].RemoteLevef=atoi(m_tmpchar); memset(&remote_ser[3],0,sizeof(remote_ser[3])); m_tmpchar=Readprofile(INIFILE,"REMOTEPC3","RemoteIP"); if(strcmp(m_tmpchar,"failed")==0) { printf("cannot set the correct values,but continue\n"); m_log=SetLogContent(L_Warning,"cannot set the correct values to variabl","the ini file corupt but continue to run"); writeLog(&m_log); } sprintf(remote_ser[3].RemoteIP,m_tmpchar); remote_ser[3].RemoteIP[strlen(m_tmpchar)]='\0'; m_tmpchar=Readprofile(INIFILE,"REMOTEPC3","RemotePort"); if(strcmp(m_tmpchar,"failed")==0) { printf("cannot set the correct values,but continue\n"); m_log=SetLogContent(L_Warning,"cannot set the correct values to variabl","the ini file corupt but continue to run"); writeLog(&m_log); } remote_ser[3].RemotePort =atoi(m_tmpchar); m_tmpchar=Readprofile(INIFILE,"REMOTEPC3","Rem_testPort"); if(strcmp(m_tmpchar,"failed")==0) { printf("cannot set the correct values,but continue\n"); m_log=SetLogContent(L_Warning,"cannot set the correct values to variabl","the ini file corupt but continue to run"); writeLog(&m_log); } remote_ser[3].Rem_testPort =atoi(m_tmpchar); m_tmpchar=Readprofile(INIFILE,"REMOTEPC3","RemoteID"); if(strcmp(m_tmpchar,"failed")==0) { printf("cannot set the correct values,but continue\n"); m_log=SetLogContent(L_Warning,"cannot set the correct values to variabl","the ini file corupt but continue to run"); writeLog(&m_log); } remote_ser[3].RemoteID=atoi(m_tmpchar); m_tmpchar=Readprofile(INIFILE,"REMOTEPC3","RemoteLevef"); if(strcmp(m_tmpchar,"failed")==0) { printf("cannot set the correct values,but continue\n"); m_log=SetLogContent(L_Warning,"cannot set the correct values to variabl","the ini file corupt but continue to run"); writeLog(&m_log); } remote_ser[3].RemoteLevef=atoi(m_tmpchar); memset(&myname,0,sizeof(myname)); m_tmpchar=Readprofile(INIFILE,"MYNAME","myIP"); if(strcmp(m_tmpchar,"failed")==0) { printf("cannot set the correct values,but continue\n"); m_log=SetLogContent(L_Warning,"cannot set the correct values to variabl","the ini file corupt but continue to run"); writeLog(&m_log); } strcpy(myname.IP,m_tmpchar); myname.IP[strlen(m_tmpchar)]='\0'; m_tmpchar=Readprofile(INIFILE,"MYNAME","MainPort"); if(strcmp(m_tmpchar,"failed")==0) { printf("cannot set the correct values,but continue\n"); m_log=SetLogContent(L_Warning,"cannot set the correct values to variabl","the ini file corupt but continue to run"); writeLog(&m_log); } /* myname.MYMAINPORT=htons(atoi(m_tmpchar)); */ myname.MYMAINPORT=atoi(m_tmpchar); m_tmpchar=Readprofile(INIFILE,"MYNAME","SecondPort"); if(strcmp(m_tmpchar,"failed")==0) { printf("cannot set the correct values,but continue\n"); m_log=SetLogContent(L_Warning,"cannot set the correct values to variabl","the ini file corupt but continue to run"); writeLog(&m_log); } myname.MYSECONDPORT=atoi(m_tmpchar); m_tmpchar=Readprofile(INIFILE,"MYNAME","APPPort"); if(strcmp(m_tmpchar,"failed")==0) { printf("cannot set the correct values,but continue\n"); m_log=SetLogContent(L_Warning,"cannot set the correct values to variabl","the ini file corupt but continue to run"); writeLog(&m_log); } myname.APPPORT=atoi(m_tmpchar); m_tmpchar=Readprofile(INIFILE,"MYNAME","userID"); if(strcmp(m_tmpchar,"failed")==0) { printf("cannot set the correct values,but continue\n"); m_log=SetLogContent(L_Warning,"cannot set the correct values to variabl","the ini file corupt but continue to run"); writeLog(&m_log); } strcpy(myname.UserID,m_tmpchar); myname.UserID[strlen(m_tmpchar)]='\0'; m_tmpchar=Readprofile(INIFILE,"MYNAME","myPWD"); if(strcmp(m_tmpchar,"failed")==0) { printf("cannot set the correct values,but continue\n"); m_log=SetLogContent(L_Warning,"cannot set the correct values to variabl","the ini file corupt but continue to run"); writeLog(&m_log); } strcpy(myname.PassWord,m_tmpchar); myname.PassWord[strlen(m_tmpchar)]='\0'; m_tmpchar=Readprofile(INIFILE,"MYNAME","LocalLevef"); if(strcmp(m_tmpchar,"failed")==0) { printf("cannot set the correct values,but continue\n"); m_log=SetLogContent(L_Warning,"cannot set the correct values to variabl","the ini file corupt but continue to run"); writeLog(&m_log); } myname.LocalLevef=atoi(m_tmpchar); /*--------------------------------------------------- get the queue thread socket numbet---------------------------------------------------*/ m_tmpchar=Readprofile(INIFILE,"QUEUESOCKET","SOCKETNO"); if(strcmp(m_tmpchar,"failed")==0) { printf("cannot set the correct values,but continue\n"); m_log=SetLogContent(L_Warning,"cannot set the correct values to variable","the ini file corupt but continue to run"); writeLog(&m_log); } else { int temp; temp=atoi(m_tmpchar); itoa(temp,m_tmpchar,10); strcpy(QueueMsg,m_tmpchar); }/*------------------------------------------ get the application socket number------------------------------------------*/ m_tmpchar=Readprofile(INIFILE,"APPSOCKET","SOCKETNO"); if(strcmp(m_tmpchar,"failed")==0) { printf("cannot set the correct values,but continue\n"); m_log=SetLogContent(L_Warning,"cannot set the correct values to variable","the ini file corupt but continue to run"); writeLog(&m_log); } else { int temp; temp=atoi(m_tmpchar); itoa(temp,m_tmpchar,10); strcpy(AppSocket,m_tmpchar); }/*------------------------------------------------ GET THE SLEEP TIME VALUE FROM THE INI FILE------------------------------------------------*/ m_tmpchar=Readprofile(INIFILE,"SLEEPTIMEVALUE","SLEEPTIMEVALUE"); if(strcmp(m_tmpchar,"failed")==0) { printf("cannot set the SLEEPTIMEVALUE correctLY values,but continue\n"); m_log=SetLogContent(L_Warning,"cannot set the correct values to variable","the ini file corupt but continue to run"); writeLog(&m_log); } else { SleepTimeValue=atoi(m_tmpchar); }/*---------------------------------------------------- GET THE TIMER SLEEP TIME VALUE FROM THE INI FILE-----------------------------------------------------*/ m_tmpchar=Readprofile(INIFILE,"TIMERSLEEPTIMEVALUE","TIMERSLEEPTIMEVALUE"); if(strcmp(m_tmpchar,"failed")==0) { printf("cannot set the TIMERSLEEPTIMEVALUE correctLY values,but continue\n"); m_log=SetLogContent(L_Warning,"cannot set the TIMERSLEEPTIMEVALUE correctLY values to variable","the ini file corupt but continue to run"); writeLog(&m_log); } else { TimerSleepTimeValue=atoi(m_tmpchar); } return 1;}/*------------------------------------------------------------Readprofile():read section(m_section) in the profile(m_file) and get the key values(m_key) according tothe key(key)------------------------------------------------------------*/char * Readprofile(char *m_file,char m_section[],char m_key[]){ WSLOGDATA m_log; FILE *fp2; char ch[2]; int i; char m_tempsec[40],m_tempkey[40]; static char m_value[20]; char temp[150]; if (strlen(m_section)<1 || strlen(m_key)<1 || strlen(m_file) < 1) { strcpy(temp,"when call the readprofile funtion ,the file name \ or the section or the key is null"); m_log=SetLogContent(L_Error,"read profile error",temp); printf(" the variable is invalidate\n"); return "failed"; } i =0; if((fp2=fopen(m_file,"r"))==NULL) { printf(" File opened failed!"); m_log=SetLogContent(L_Error,"open profile failed","in readprofile functin ,cannot open the ini file"); writeLog(&m_log); return "failed"; }/*step 1 :loop int the file to get the section name */ while(!feof(fp2)) { if((ch[0]=fgetc(fp2))=='[')/*the start of the section flag*/ { strcpy(m_tempsec,""); ch[0]=fgetc(fp2); i =0 ; while(ch[0] !=']' && ch[0] !='\n' && !feof(fp2))/*the end of the sextion flag*/ { m_tempsec[i++] = ch[0]; ch[0]=fgetc(fp2); } m_tempsec[i++] = '\0'; if((strcmp(m_section,m_tempsec))==0)/*compare the section values with the get values*/ { ch[0]=fgetc(fp2);/*step 2:loop in the section to get the key and kkey values*/ while(ch[0] !='[' && !feof(fp2))/*the end of the section flag of the end flag of file*/ { if((ch[0]>='0' && ch[0] <= '9')||(ch[0]>='A' && ch[0]<='z')) { i = 0 ; strcpy(m_tempkey,""); m_tempkey[i++] = ch[0]; ch[0]=fgetc(fp2); /*step 3:loop in the line to get the key*/ while((ch[0]>='0' && ch[0] <= '9')||(ch[0]>='A' && ch[0]<='z')) { m_tempkey[i++] = ch[0]; ch[0]=fgetc(fp2); } m_tempkey[i++] = '\0'; if((strcmp(m_tempkey,m_key))==0)/*compare the key values*/ { while(ch[0] != '=' && ch[0] !='\n')/*step over the unnessary word*/ ch[0]=fgetc(fp2); if(ch[0] == '\n') { printf(" cannot get the key values :%s in the profile:%s !",m_key,m_file); m_log=SetLogContent(L_Error,"get key values in profile failed","in readprofile functin ,cannot open the ini file"); writeLog(&m_log); fclose(fp2); return "failed"; } else { strcpy(m_value,"");/*step 4:loop int the line to get the key values*/ while(ch[0] !='\n' && !feof(fp2)) { i = 0; ch[0]=fgetc(fp2); if((ch[0]>='0' && ch[0] <= '9')||(ch[0]>='A' && ch[0]<='z')) { m_value[i++] = ch[0]; ch[0]=fgetc(fp2); while((ch[0]>='0' && ch[0] <= '9')||(ch[0]>='A' && ch[0]<='z')||(ch[0]=='.')) { m_value[i++] = ch[0]; ch[0]=fgetc(fp2); } m_value[i++] ='\0'; fclose(fp2); return m_value; } } fclose(fp2); return "failed"; } } } ch[0]=fgetc(fp2); } } } } fclose(fp2); printf("File opened failed!");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -