c语言实现模拟发送报文改进版(添加程序自动打log)

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdarg.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#include "ItcastLog.h"

#define ITCAST_DEBUG_FILE_	"socketclient.log"
#define ITCAST_MAX_STRING_LEN 		10240

//Level类别
#define IC_NO_LOG_LEVEL			0
#define IC_DEBUG_LEVEL			1
#define IC_INFO_LEVEL			2
#define IC_WARNING_LEVEL		3
#define IC_ERROR_LEVEL			4

int  LogLevel[5] = {IC_NO_LOG_LEVEL, IC_DEBUG_LEVEL, IC_INFO_LEVEL, IC_WARNING_LEVEL, IC_ERROR_LEVEL};

//Level的名称
char ICLevelName[5][10] = {"NOLOG", "DEBUG", "INFO", "WARNING", "ERROR"};

static int ITCAST_Error_GetCurTime(char* strTime)
{
	struct tm*		tmTime = NULL;
	size_t			timeLen = 0;
	time_t			tTime = 0;	
	
	tTime = time(NULL);
	tmTime = localtime(&tTime);
	//timeLen = strftime(strTime, 33, "%Y(Y)%m(M)%d(D)%H(H)%M(M)%S(S)", tmTime);
	timeLen = strftime(strTime, 33, "%Y.%m.%d %H:%M:%S", tmTime);
	
	return timeLen;
}

static int ITCAST_Error_OpenFile(int* pf)
{
	char	fileName[1024];
	
	memset(fileName, 0, sizeof(fileName));
#ifdef WIN32
	sprintf(fileName, "c:\\itcast\\%s",ITCAST_DEBUG_FILE_);
#else
	sprintf(fileName, "c:\\itcast\\%s",ITCAST_DEBUG_FILE_);
	//sprintf(fileName, "%s/log/%s", getenv("HOME"), ITCAST_DEBUG_FILE_);
#endif
    
    *pf = open(fileName, O_WRONLY|O_CREAT|O_APPEND, 0666);
    if(*pf < 0)
    {
        return -1;
    }
	
	return 0;
}

static void ITCAST_Error_Core(const char *file, int line, int level, int status, const char *fmt, va_list args)
{
    char str[ITCAST_MAX_STRING_LEN];
    int	 strLen = 0;
    char tmpStr[64];
    int	 tmpStrLen = 0;
    int  pf = 0;
    
    //初始化
    memset(str, 0, ITCAST_MAX_STRING_LEN);
    memset(tmpStr, 0, 64);
    
    //加入LOG时间
    tmpStrLen = ITCAST_Error_GetCurTime(tmpStr);
    tmpStrLen = sprintf(str, "[%s] ", tmpStr);
    strLen = tmpStrLen;

    //加入LOG等级
    tmpStrLen = sprintf(str+strLen, "[%s] ", ICLevelName[level]);
    strLen += tmpStrLen;
    
    //加入LOG状态
    if (status != 0) 
    {
        tmpStrLen = sprintf(str+strLen, "[ERRNO is %d] ", status);
    }
    else
    {
    	tmpStrLen = sprintf(str+strLen, "[SUCCESS] ");
    }
    strLen += tmpStrLen;

    //加入LOG信息
    tmpStrLen = vsprintf(str+strLen, fmt, args);
    strLen += tmpStrLen;

    //加入LOG发生文件
    tmpStrLen = sprintf(str+strLen, " [%s]", file);
    strLen += tmpStrLen;

    //加入LOG发生行数
    tmpStrLen = sprintf(str+strLen, " [%d]\n", line);
    strLen += tmpStrLen;
    
    //打开LOG文件
    if(ITCAST_Error_OpenFile(&pf))
	{
		return ;
	}
	
    //写入LOG文件
    write(pf, str, strLen);
    //IC_Log_Error_WriteFile(str);
    
    //关闭文件
    close(pf);
    
    return ;
}


void ITCAST_LOG(const char *file, int line, int level, int status, const char *fmt, ...)
{
    va_list args;
	
	//判断是否需要写LOG
//	if(level!=IC_DEBUG_LEVEL && level!=IC_INFO_LEVEL && level!=IC_WARNING_LEVEL && level!=IC_ERROR_LEVEL)
	if(level == IC_NO_LOG_LEVEL)
	{
		return ;
	}
	
	//调用核心的写LOG函数
    va_start(args, fmt);
    ITCAST_Error_Core(file, line, level, status, fmt, args);
    va_end(args);
    
    return ;
}
#include "stdlib.h"
#include "stdio.h"
#include "string.h"
#include "itcastlog.h"


typedef struct _SCK_HANDLE {
	char		version[16];
	char	    serverip[16];
	int		serverport;
	unsigned char *buf ;
	int				buflen;
}SCK_HANDLE;


//客户端初始化 获取handle上下
__declspec(dllexport) 
int cltSocketInit(void **handle /*out*/)
{
	int ret = 0;
	SCK_HANDLE *sh = NULL;

	ITCAST_LOG(__FILE__, __LINE__,  LogLevel[2], ret, "func cltSocketInit() Begin", ret);
	
	sh = (SCK_HANDLE *)malloc(sizeof(SCK_HANDLE));
	if (sh == NULL)
	{
		ret = -1;
		ITCAST_LOG(__FILE__, __LINE__,  LogLevel[4], ret, "func cltSocketInit() err: %d, malloc err....", ret);
		return ret;
	}
	memset(sh, 0, sizeof(SCK_HANDLE));
	strcpy(sh->serverip, "192.168.0.128");
	sh->serverport= 88;

	*handle = sh;
	ITCAST_LOG(__FILE__, __LINE__,  LogLevel[2], ret, "func cltSocketInit() End", ret);
	return ret;
}

//客户端发报文
__declspec(dllexport) 
int cltSocketSend(void *handle /*in*/, unsigned char *buf /*in*/,  int buflen /*in*/)
{
	int		ret = 0;
	SCK_HANDLE *sh = NULL; 
	
	if (handle==NULL || buf==NULL)
	{
		ret = -1;
		ITCAST_LOG(__FILE__, __LINE__, LogLevel[4], ret, "func cltSocketSend() err: %d, (handle==NULL || buf==NULL)", ret);
		return ret;
	}
	sh = (SCK_HANDLE *)handle ;
	sh->buf = (char *)malloc(buflen);
	if (sh->buf == NULL)
	{
		ret = -2;
		ITCAST_LOG(__FILE__, __LINE__, LogLevel[4], ret, "func cltSocketSend() err: %d, (buflen:%d)", ret, buflen);
		return ret;
	}
	memcpy(sh->buf, buf, buflen);
	sh->buflen = buflen;

	return ret;
}


//客户端收报文
__declspec(dllexport) 
int cltSocketRev(void *handle /*in*/, unsigned char *buf /*in*/, int *buflen /*in out*/)
{
	int  ret = 0;
	SCK_HANDLE *sh = NULL; 

	if (handle==NULL || buf==NULL || buflen==NULL)
	{
		ret = -1;
		ITCAST_LOG(__FILE__, __LINE__, LogLevel[4], ret, "func cltSocketRev() err: %d, ((handle==NULL || buf==NULL || buflen==NULL))", ret);
		return ret;
	}

	sh = (SCK_HANDLE *)handle;

	memcpy(buf, sh->buf, sh->buflen);
	*buflen  = sh->buflen;

	if (sh->buf != NULL)
	{
		free(sh->buf);
		sh->buf = NULL; //把状态回到原始
		sh->buflen = 0;
	}
	return ret;
}

//客户端释放资源
__declspec(dllexport) 
int cltSocketDestory(void *handle/*in*/)
{
	int  ret = 0;
	SCK_HANDLE *sh = NULL; 

	if (handle==NULL )
	{
		ret = -1;
		ITCAST_LOG(__FILE__, __LINE__, LogLevel[4], ret, "func cltSocketSend() err: %d, ((handle==NULL )", ret);
		return ret;
	}

	sh = (SCK_HANDLE *)handle;
	
	if (sh->buf != NULL)
	{
		free(sh->buf);
		sh->buf = NULL;
		sh->buflen = 0;
	}
	free(sh);

	return ret;
}

//------------------第一套api接口---End-----------------------------------//


//------------------第二套api接口---Begin--------------------------------//
__declspec(dllexport)
int cltSocketInit2(void **handle)
{
	return cltSocketInit(handle /*out*/);
}

//客户端发报文
__declspec(dllexport)
int cltSocketSend2(void *handle, unsigned char *buf,  int buflen)
{
	return cltSocketSend(handle /*in*/, buf /*in*/, buflen /*in*/);
}
//客户端收报文
__declspec(dllexport)
int cltSocketRev2(void *handle, unsigned char **buf, int *buflen)
{
	int  ret = 0;
	SCK_HANDLE *sh = NULL; 

	if (handle==NULL || buf==NULL || buflen==NULL)
	{
		ret = -1;
		ITCAST_LOG(__FILE__, __LINE__, LogLevel[4], ret, "func cltSocketRev2() err: %d,  (handle==NULL || buf==NULL || buflen==NULL)", ret);
		return ret;
	}

	sh = (SCK_HANDLE *)handle;

	*buf = (char *)malloc( sh->buflen);

	memcpy(*buf, sh->buf, sh->buflen);
	*buflen  = sh->buflen;
	return ret;
}

__declspec(dllexport)
int cltSocketRev2_Free(unsigned char **buf)
{
	if (buf == NULL)
	{
		return -1;
	}
	free(*buf);
	*buf = NULL;
	return 0;
}
//客户端释放资源
__declspec(dllexport)
int cltSocketDestory2(void **handle)
{
	int  ret = 0;
	SCK_HANDLE *sh = NULL; 
	

	if (handle==NULL )
	{
		ret = -1;
		ITCAST_LOG(__FILE__, __LINE__, LogLevel[4], ret, "func cltSocketSend() err: %d, ((handle==NULL )", ret);
		return ret;
	}

	sh = (SCK_HANDLE *)*handle;

	if (sh->buf != NULL)
	{
		free(sh->buf);
		sh->buf = NULL;
		sh->buflen = 0;
	}
	free(sh);
	*handle = NULL; //把实参赋值null

	return ret;
}
//------------------第二套api接口---End--------------------------------//

void main()
{
	int ret = 0;

	void *handle = NULL;

	unsigned char buf[1024]; /*in*/  
	int buflen = 10;

	unsigned char *out = NULL; /*in*/
	int outlen = 0;

	strcpy(buf, "aadd3456789012233ddddddddddddddaaaaaaaaaaa");
	//客户端初始化 获取handle上下
	//运行的上下文环境 
	ret = cltSocketInit2(&handle/*out*/);
	if (ret !=0)
	{
		printf("func cltSocketInit2() err :%d \n", ret);
		return ;
	}

	// 	//客户端发报文
	ret = cltSocketSend2(handle /*in*/, buf /*in*/,buflen /*in*/);
	if (ret !=0)
	{
		printf("func cltSocketSend2() err :%d \n", ret);
		return ;
	}
	// 
	// 	//客户端收报文
	ret = cltSocketRev2(handle /*in*/, &out /*in*/, &outlen /*in out*/);
	if (ret != 0)
	{
		printf("func cltSocketRev() err :%d \n", ret);
		return ;
	}
	printf("out: %s \n", out);
	cltSocketRev2_Free(&out);

	// 
	// 	//客户端释放资源
	ret = cltSocketDestory2(&handle/*in*/);
	if (ret != 0)
	{
		printf("func cltSocketDestory2() err :%d \n", ret);
		return ;
	}

	system("pause");
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值