資源簡介
udp通信測試代碼,支持cmake,單向通信實現,發送端支持linux64,接收端支持win64/linux64

代碼片段和文件信息
#include?“Log.h“
#include?
#include?
#ifdef?__linux__
#ifndef?sprintf_s
#define?sprintf_s?sprintf
#endif
#include?
#include?
#include?
#endif
CLogger*?CLogger::m_pLogInstance?=?NULL;
//extern?void?WriteLog(?const?int?iMsgType?const?std::string?strMsg);
extern?char?LOG_FILE_NAME[128]; //LOGFILE?Name?should?be?defined
extern?std::string?logdir;
void?WriteLog1(?const?int?iMsgType?const?char?*?strMsg)
{
try?{
time_t?tt;
struct?timeb?tm0;
struct?tm?tm1;
char?buf[512];
FILE?*?fpLog?=?NULL;
//time(&tt);
ftime(&tm0);
tt?=?tm0.time;
#ifdef?WIN32
localtime_s(&tm1?&tt);
#else
localtime_r(&tt?&tm1);
#endif
sprintf_s(buf?“%04d-%02d-%02d?%02d:%02d:%02d.%03d?“
?tm1.tm_year?+?1900?tm1.tm_mon?+?1?tm1.tm_mday
?tm1.tm_hour?tm1.tm_min?tm1.tm_sec?tm0.millitm);
std::string?strTime?=?buf;
buf[10]?=?‘\0‘;
//file?name
std::string?strPath?=?logdir;
#ifdef?WIN32
strPath?+=?“\\“;
#else
strPath?+=?“/“;
#endif
strPath?+=?buf;
strPath?+=?“_“;
//
switch?(iMsgType)
{
case?eHardError:
strTime?+=?“[HardErrorIMsg]?“;
break;
case?eSoftError:
strTime?+=?“[SoftErrorIMsg]?“;
break;
case?eConfigError:
strTime?+=?“[ConfErrorIMsg]?“;
break;
case?eParameterError:
strTime?+=?“[ParamErrorMsg]?“;
break;
case?eReadError:
strTime?+=?“[ReadErrorIMsg]?“;
break;
case?eWriteError:
strTime?+=?“[WriteErrorMsg]?“;
break;
case?eControlMessage:
strTime?+=?“[ControlExeMsg]?“;
//strPath?+=?“chain_“;
break;
case?eResponseMessage:
strTime?+=?“[ResponseUpMsg]?“;
//strPath?+=?“chain_“;
break;
case?eTipMessage:
strTime?+=?“[NoticeTipIMsg]?“;
break;
default:
strTime?+=?“[PromptUnNoMsg]?“;
break;
}
strPath?+=?LOG_FILE_NAME;
//open
#ifdef?WIN32
fopen_s(&fpLog?strPath.c_str()?“a+“);
#else
fpLog?=?fopen(strPath.c_str()?“a+“);
#endif
if?(NULL?!=?fpLog)
{
fseek(fpLog?0?SEEK_END);
fwrite(strTime.c_str()?strTime.length()?1?fpLog);
fwrite(strMsg?strlen(strMsg)?1?fpLog);
fwrite(“\n“?1?1?fpLog);
fclose(fpLog);
}
}
catch?(...)?{
printf(“write?log[%d]{%s}error\n“?iMsgType?strMsg);
}
}
#ifdef?WIN32
#include?
#include?
extern?char?SVCNAME[128];
void?WriteLog(?const?int?iMsgType?const?char?*?strMsg)
{
try?{
if?(iMsgType? {
HANDLE?hEventSource;
LPCTSTR?lpszStrings[2];
hEventSource?=?RegisterEventSource(NULL?SVCNAME);
if?(NULL?!=?hEventSource)
{
lpszStrings[0]?=?SVCNAME;
lpszStrings[1]?=?strMsg;
ReportEvent(hEventSource????????//?event?log?handle
EVENTLOG_ERROR_TYPE?//?event?type
0???????????????????//?event?category
0???????????//?event?identifier
NULL????????????????//?no?security?identifier
2???????????????????//?size?of?lpszStrings?arra
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????目錄???????????0??2019-08-27?12:48??udp_test\common\
?????文件????????7200??2019-02-28?10:37??udp_test\common\Log.cpp
?????文件????????1645??2018-09-20?15:33??udp_test\common\Log.h
?????文件????????1123??2019-02-28?10:37??udp_test\common\Mutex.cpp
?????文件?????????650??2018-07-20?18:04??udp_test\common\Mutex.h
?????文件????????1263??2018-02-07?15:42??udp_test\common\myThread.cpp
?????文件????????1154??2017-12-12?17:25??udp_test\common\myThread.h
?????文件?????????458??2019-02-28?10:38??udp_test\common\win32Thread.cpp
?????文件?????????366??2015-06-30?09:46??udp_test\common\win32Thread.h
?????目錄???????????0??2019-08-27?12:31??udp_test\udp_client\
?????目錄???????????0??2019-08-27?12:32??udp_test\udp_client\bin\
?????目錄???????????0??2019-08-27?12:32??udp_test\udp_client\build_linux\
?????目錄???????????0??2019-08-27?12:33??udp_test\udp_client\build_win\
?????文件????????1480??2019-09-01?09:33??udp_test\udp_client\CMakeLists.txt
?????文件??????????82??2019-08-27?12:44??udp_test\udp_client\readme.txt
?????目錄???????????0??2019-08-27?12:31??udp_test\udp_client\src\
?????文件?????????898??2019-08-15?16:35??udp_test\udp_client\src\client.cpp
?????文件????????4946??2019-09-01?09:46??udp_test\udp_client\src\udpreceive.cpp
?????文件?????????938??2019-09-01?09:38??udp_test\udp_client\src\udpreceive.h
?????目錄???????????0??2019-08-27?12:31??udp_test\udp_service\
?????目錄???????????0??2019-08-27?12:33??udp_test\udp_service\bin\
?????目錄???????????0??2019-08-27?12:33??udp_test\udp_service\build_linux\
?????文件????????1181??2019-09-01?09:32??udp_test\udp_service\CMakeLists.txt
?????目錄???????????0??2019-08-27?12:31??udp_test\udp_service\src\
?????文件?????????554??2019-08-15?15:30??udp_test\udp_service\src\service.cpp
?????文件????????3473??2019-09-01?09:46??udp_test\udp_service\src\udpsend.cpp
?????文件?????????855??2019-08-15?15:00??udp_test\udp_service\src\udpsend.h
?????文件?????????423??2019-08-27?12:25??udp_test\組播ip設置.txt
?????目錄???????????0??2019-09-01?09:32??udp_test\
- 上一篇:labview轉速測試程序
- 下一篇:DSP最小系統原理圖設計
評論
共有 條評論