|
Re: Debug Assertion Failed!
btw here are my codes
RMServer.c
// RMServer side
#include "RMServer.h"
char msg[50];
char logfile[100]="";
int error;
struct RMConfig cfg;
extern int WriteToLog(char* str);
extern void displayVersion();
int main(){
WSADATA wsda;
WSAStartup(0x0101,&wsda);
// Start Logger
logStart();
// end Logger
if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1)
{
printf("Socket error...");
return 0;
}
server.sin_addr.s_addr=INADDR_ANY; /*INADDR_ANY will simply put your IP */
server.sin_port=htons(PORT); /*htons means host to network short */
server.sin_family=AF_INET;
if(bind(sockfd,(struct sockaddr*)&server,sizeof(struct sockaddr))==-1)
{
printf("Cannot bind...");
return 0;
}
if(listen(sockfd,BACKLOG))/*Listening */
{
printf("Error Listening...");
return 0;
}
while (1)
{
int size=sizeof(struct sockaddr_in);
if((sockfd2=accept(sockfd,(struct sockaddr*)&client,&size))==-1)
{
printf("Accept Error...");
return 0;
}
if((n_bytes=recv(sockfd2,msg ,50,0))==-1){
printf("Error Recv...");
return 0;
}
msg[n_bytes]='\0';
printf("Client's Message:%s",msg);
}
WSACleanup();
closesocket(sockfd); /*used to close the socket */
closesocket(sockfd2);
return 0;
}// end of main
// log init
int logStart()
{
int error=0;
char logfile[100]="";
// char *rmConfig;
/*
if((rmConfig = getenv(RM_SYS_VAR)) != NULL)
{
sprintf(PNL_CONFIG_FILE,"%s",rmConfig);
}
else
{
return 1;
}
*/
cfg = initConfig();
//cfg = getConfig(LOG_CONFIG,cfg);
cfg = getLogger(LOG_CONFIG,cfg);
strcpy(logfile,cfg.log.logDirectory);
strcat(logfile, cfg.log.logFile);
strcpy(LOGFILE,logfile);
error = WriteToLog("Monitoring started.");
if (!error)
{
displayVersion();
sprintf(logfile,"RM CONFIG FILE = %s",PNL_CONFIG_FILE);
WriteToLog(logfile);
}
return(error);
}
RMServer.c
#include <stdio.h>
#include <winsock.h>
#include "RMUtil.h"
#define PORT 1200
#define BACKLOG 4
struct sockaddr_in server;
struct sockaddr_in client;
int sockfd,sockfd2,n_bytes;
int logStart();
RMUtil.c
#include "RMUtil.h"
char errorNum[20]="";
char tempVal[100]="";
int WriteToLog(char* str)
{
time_t t;
FILE* log;
char today[50]="";
t = time(NULL);
sprintf(today,"%s",ctime(&t));
today[strlen(today)-1] = '\0';
log = fopen(LOGFILE, "a+");
if (log == NULL)
return -1;
fprintf(log, "%s %s\n", today, str);
fclose(log);
return 0;
}
int writeToFile(char *file, char* str)
{
char logger[100]="";
time_t t;
FILE* log;
char today[50]="";
t = time(NULL);
sprintf(today,"%s",ctime(&t));
today[strlen(today)-1] = '\0';
log = fopen(file, "w");
if (log == NULL){
sprintf(logger, "FAILED to open file: %s. Make sure that the directory path exists.", file);
WriteToLog(logger);
return 1;
}
fprintf(log, "%s %s\n", today, str);
fclose(log);
return 0;
}
char* removeNewline(char line[])
{
if (line[strlen(line)-1] == '\n')
{
line[strlen(line)-1] = '\0';
}
return line;
}
char* getValue(char *line)
{
char value[130]="";
char str[130]="";
int i=0, k=0;
strcpy(value, line);
if(value[0] == ' ')
{
while(value[i] == ' '){
i++;
}
while(value[i] != '\0' && value[i] != '\n'){
str[k]= value[i];
k++;i++;
}
str[k]='\0';
strcpy(value,str);
}
if(value[0] == '\"'){
i=1;k=0;
while(value[i] != '\0' && value[i] != '\"' && value[i] != '\n'){
str[k] = value[i];
i++; k++;
}
str[k]='\0';
strcpy(value,str);
}
return removeNewline(value);
}
struct RMConfig initConfig()
{
struct RMConfig cfg;
struct tm time;
time.tm_hour =0;
time.tm_mday =1;
time.tm_min =0;
time.tm_mon = 1;
time.tm_year = 1900;
strcpy(cfg.log.logFile,"");
strcpy(cfg.log.logDirectory,"");
strcpy(cfg.log.logArchiveDirectory,"");
strcpy(cfg.log.nextLogClearStr,"");
strcpy(cfg.version.build_date_Str,"");
strcpy(cfg.version.version,"");
strcpy(cfg.version.system_name,"");
return cfg;
}
struct RMConfig getLogger(int configType, struct RMConfig cfg)
{
FILE *configFile;
char msg[30];
char val[200];
char ch;
char cfgTitle[50]="";
if ((configFile = fopen( PNL_CONFIG_FILE, "r" )) == NULL)
{
return cfg;
}
if (configType == LOG_CONFIG){
strcpy(cfgTitle,LOG_CONFIG_TITLE);
}
while(!feof(configFile)){
fscanf(configFile,"%s", msg);
if (strcmp(cfgTitle,msg) == 0){
if (configType == LOG_CONFIG) {
while(!feof(configFile) && (ch = fgetc(configFile)) != EOF){
if (ch == '#')
return cfg;
if (ch != '\n'){
fseek(configFile,-1, SEEK_CUR);
fscanf(configFile,"%s", msg);
fgets(val,sizeof(val),configFile);
strcpy(val,getValue(val));
if(!strcmp(msg,LOGFILE_LABEL))
{
strcpy(cfg.log.logFile,val);
}
if(!strcmp(msg,LOG_DIRECTORY_LABEL))
{
strcpy(cfg.log.logDirectory,val);
}
if(!strcmp(msg,LOG_ARCHIVE_DIRECTORY_LABEL))
{
strcpy(cfg.log.logArchiveDirectory,val);
}
}
}
}
}
}
fclose(configFile);
return cfg;
}
char* getPropertyValue(char *filename, char *property)
{
char log[100]="";
char val[100]="";
char key[100]="";
//char ch;
FILE *propertyFile;
if ((propertyFile = fopen(filename, "r" )) == NULL)
{
sprintf(log, "FAILED to open file: %s. Make sure that the file and the directory path exists.", filename);
WriteToLog(log);
}
else
{
while(!feof(propertyFile)){
fscanf(propertyFile,"%s", key);
if (strcmp(key,property) == 0){
fgets(val,sizeof(val),propertyFile);
strcpy(val,getValue(val));
}
}
fclose(propertyFile);
}
if(strcmp(val,"")==0)
{
sprintf(log,"WARNING: Property name '%s' is not found in %s", property,filename);
WriteToLog(log);
}
strcpy(tempVal,"");
strcpy(tempVal,val);
return tempVal;
}
struct RMConfig getConfig(int configType,struct RMConfig cfg)
{
char log[100]="";
char cfgTitle[50]="";
// sprintf(log,"Getting configuration type: %d",configType );
// WriteToLog(log);
if (configType == LOG_CONFIG){
strcpy(cfg.log.logFile,getPropertyValue(PNL_CONFIG_FILE, LOGFILE_LABEL));
strcpy(cfg.log.logDirectory,getPropertyValue(PNL_CONFIG_FILE, LOG_DIRECTORY_LABEL));
strcpy(cfg.log.logArchiveDirectory,getPropertyValue(PNL_CONFIG_FILE, LOG_ARCHIVE_DIRECTORY_LABEL));
}
if (configType == VERSION_CONFIG){
strcpy(cfg.version.system_name,getPropertyValue(PNL_CONFIG_FILE, SYSTEM_NAME_LABEL));
}
return cfg;
}
void displayVersion()
{
struct RMConfig cfg;
char log[100]="";
cfg = initConfig();
cfg = getConfig(VERSION_CONFIG,cfg);
sprintf(log,"%s",cfg.version.system_name);
WriteToLog(log);
sprintf(log,"Version: %s",VERSION);
WriteToLog(log);
sprintf(log,"Build date: %s",BUILD_DATE);
WriteToLog(log);
}
RMUtil.h
#include <string.h>
#include <stdio.h>
#include <time.h>
#include <ctype.h>
#include <errno.h>
#include <stdlib.h>
#include "RMConstants.h"
char LOGFILE[200];
//char PNL_CONFIG_FILE[100] = "C://RISK/RMResource/PNL.cfg";
struct logInfo
{
struct tm nextLogClear;
char logFile[ 30 ];
char logDirectory[ 200 ];
char logArchiveDirectory[ 200 ];
char nextLogClearStr[ 16 ];
int daysBeforeArchiveLog;
};
struct versionInfo{
struct tm build_date;
char build_date_Str[20];
char version[8];
char system_name[50];
};
struct RMConfig{
// struct resetInfo reset;
struct logInfo log;
// struct dbConfig db;
// struct mqConfig mq_write;
// struct mqConfig mq_read;
struct versionInfo version;
// struct paramInfo param;
// struct debug debugger;
// struct discard discard;
};
int WriteToLog(char* str);
int writeToFile(char *file, char* str);
char* removeNewline(char line[]);
char* getValue(char *line);
struct RMConfig initConfig();
struct RMConfig getLogger(int configType, struct RMConfig cfg);
char* getPropertyValue(char *filename, char *property);
struct RMConfig getConfig(int configType,struct RMConfig cfg);
void displayVersion();
RMConstants.h
#include<time.h>
#define BUILD_DATE "2005-11-11"
#define VERSION "beta 0.0.1"
#define LOG_CONFIG_TITLE "#log"
#define LOGFILE_LABEL "log_file"
#define LOG_DIRECTORY_LABEL "log_directory"
#define LOG_ARCHIVE_DIRECTORY_LABEL "log_archive_directory"
#define RM_SYS_VAR "RM"
#define VERSION_CONFIG_TITLE "#version"
#define SYSTEM_NAME_LABEL "system_name"
#define LOG_CONFIG 259
#define VERSION_CONFIG 260
#define PNL_CONFIG_FILE "C://RISK/RMResource/PNL.cfg"
and finally the contents of the cfg file (RM.log)
#version
system_name RISK MANAGEMENT
#log
log_file RM.log
log_directory C:\\RISK\RMResource\
log_archive_directory C:\\RISK\RMResource\log archive\
|