#include
#include<stdarg.h>
using namespace std;
void LogInfo(const char szFormat,...)
{
char szBuf[4096]={};
va_list args; //第一步
va_start(args,szFormat); //第二步
vsnprintf(szBuf,4096,szFormat,args); //第三步
va_end(args); //第四步
//在这里对字符串 szBuf 作处理,输出到日志文件或直接打印信息
printf("%s\r\n",szBuf);
// *******************************************************
return ;
}
int main()
{
LogInfo("收到网关服务器中转命令,主命令:%d,副命令:%d,附带信息",1,2,"hahahaha");
return 0;
}
namespace AA
{
namespace BB
{
class Test
{
public:
Test(int x):x_(x){}
void printMsg()
{
printf("x_ is %d\r\n",x_);
}
private:
int x_;
};
}
}
int main(void)
{
std::shared_ptrAA::BB::Test sharedPtr;
sharedPtr = std::make_sharedAA::BB::Test(34);
sharedPtr->printMsg();
return 0;
}
3)sscanf
sscanf把一个字符串进行拆分(就是把一个字符串按一定格式,取出满足条件的字符串);
int main(void)
{
char str1[] = "https://www.baidu.com:1234";
char protocol[32] = {0};
char hostname[128] = {0};
char port[8] = {0};
sscanf(str1,"%[^:]😕/%[^:]:%[1-9]",protocol,hostname,port);
printf("%s,%s,%s\r\n",protocol,hostname,port);
return 0;
}
4)reactor
对io的处理,变成对事件的处理;
5)fileno函数的使用,(把文件流指针转换为文件描述符)
#include <stdio.h>
int main()
{
FILE *fp;
int fd;
fp = fopen("/etc/passwd", "r");
fd = fileno(fp);
printf("fd = %d\n", fd); //输出3
fclose(fp);
}
43.6+668.8
systemctl status mysql //查看mysql是否运行;
systemctl restart mysqld //重启mysql;
如果编译出现" mysql/mysql.h: No such file or directory #include "mysql/mysql.h" "类似错误,
则用"find /usr/ -name "mysql.h" "命令查看是否有头文件,如果没有,需要安装,方法如下:
sudo apt-get install libmysqlclient-dev
出现下面的情况是权限不足,需要修改过;
(Not all processes could be identified, non-owned process info
will not be shown, you would have to be root to see it all.)
sudo mysql -u root -p //登录mysql;
string中,append函数的用法
#include
using namespace std;
int main()
{
std::string str = "";
str.append("AAAAA");
str.append("BBBBBB");
cout<<str<<endl;//AAAAABBBBBB
return 0;
}
//5 std::stoi函数
#include
using namespace std;
int main()
{
std::string str = "1234abc5";
size_t idx;
int num = std::stoi(str,&idx);
std::cout << num << std::endl; //1234
std::cout << "invalid character pos is " <<idx<< std::endl; //4
return 0;
}
//replace
#include
#include
#include
using namespace std;
int main()
{
std::vector vec = {"123","aaa"};
cout<<vec.at(0).replace(0,2,"AA")<<endl; //从0-2(不包括下标2),顾输出AA3
return 0;
}
//S_ISREG or S_ISDIR
#include
#include <sys/stat.h>
int main(int argc,char** argv)
{
struct stat mystat = {0};
if(argc < 2) return -1;
stat(argv[1],&mystat);
if(S_ISDIR(mystat.st_mode))
{
printf("dir file \r\n");
}
if(S_ISREG(mystat.st_mode))
{
printf("regular file \r\n");
}
}
//zlib
#include
#include <sys/stat.h>
#include <zlib.h>
int main(int argc,char** argv)
{
unsigned char strSrc[] = "hello world! aaaaa bbbbb ccccc ddddd 中文测试 yes";
unsigned char buf[1024] = {0};
unsigned char strDst[1024] = {0};
unsigned long srcLen = sizeof(strSrc);
unsigned long bufLen = sizeof(buf);
unsigned long dstLen = sizeof(strDst);
printf("Src string:%s\nLength:%ld\n", strSrc, srcLen);
/* 压缩 */
compress(buf, &bufLen, strSrc, srcLen);
printf("After Compressed Length:%ld\n", bufLen);
/* 解压缩 */
uncompress(strDst, &dstLen, buf, bufLen);
printf("After UnCompressed Length:%ld\n",dstLen);
printf("UnCompressed String:%s\n",strDst);
return 0;
}
注意:CMakeList.txt中,target_link_libraries(target -lpthread -lz),这样写链接zlib库;
unlink(删除一般文件)
#include
#include <sys/stat.h>
#include <unistd.h>
int main(int argc,char** argv)
{
if(argc < 2) return -1;
int ret = unlink(argv[1]); //可以删除一般文件,不可以删除文件夹;
if(ret == 0)
{
printf("unlink ok\r\n");
}
else printf("not unlink \r\n");
}
//ifstream中,tellg,seekg等的使用
#include
#include
using namespace std;
int main(int argc,char** argv)
{
std::ifstream ifs(argv[1]);
if(!ifs.is_open())
{
cerr << "Open failed" << endl;
exit(1);
}
// 记录并输出当前读取位置
streampos pos = ifs.tellg();
cout << "Current position: " << pos << endl;
// 移动光标并输出当前读取位置
ifs.seekg(7, ios_base::beg);
pos = ifs.tellg();
cout << "Current position: " << pos << endl;
ifs.close();
return 0;
}
//isspace 的使用
#include
#include <ctype.h>
using namespace std;
//如果传入的字符是空白字符,则返回非零值;如果传入的字符不是空白字符,则返回零
int main(int argc,char** argv)
{
char c;
int ret;
scanf("%c",&c);
ret = isspace©;
if(ret > 0) printf("is space charactor\r\n");
else printf("not space charactor\r\n");
return 0;
}
//bind
int fun(int a,int b)
{
return a + b;
}
int main(int argc,char** argv)
{
auto f1 = std::bind(fun,std::placeholders::_1,4);
cout<<f1(5)<<endl;
return 0;
}
class A
{
public:
void printMsg(int a,int b)
{
printf("a+b=%d\r\n",a+b);
}
};
int main(int argc,char** argv)
{
A a;
auto f1 = std::bind(&A::printMsg,&a,std::placeholders::_1,5); //绑定成员函数
f1(5);
return 0;
}
//callback
回调就是把一个函数作为参数传递到另一个函数里面,当那个函数执行完之后,再执行传进去的这个函数;
#include
#include
using namespace std;
typedef int(*ptrFun)(int,int);
int myAdd(int a,int b)
{
return a + b;
}
//回调函数
int callBackFun(int a,int b,const ptrFun& fun)
{
return fun(a,b);
}
int main(void)
{
int ret = callBackFun(3,4,myAdd);
printf("ret is %d\r\n",ret);
return 0;
}
//function和bind的联合应用
#include
#include
#include
using namespace std;
class Baseclass
{
public:
int Add(int a, int b) { return a + b; };
};
int main()
{
int a = 1;
int b = 2;
Baseclass b1;
std::function<int(int, int)> addFunc = std::bind(&Baseclass::Add, &b1, std::placeholders::_1, std::placeholders::_2);
int c = addFunc(a, b);
std::cout << "c: " << c << std::endl;
return 0;
}
ll | grep Makefile
find ./ -iname "makefile " //忽略大小写查找;
netstat -antp | grep hello //配合-t、-u则只打印TCP、UDP协议的统计数据,-n 不解析域名
输出:
nc 127.0.0.1 9995 // nc是netcat的简写
在cmake中,打开编译开关的方法:
cmake -Devent_enable_verbose=on .../ //注意:前面有"-D"的参数;
//找头文件方法
pkg-config libevent --cflags //输出结果: -I/usr/local/include 所以必须sudo make install安装;
//找库方法
pkg-config libevent --libs //-L/usr/local/lib -levent_core -levent_extra
ll /usr/local/lib/ | grep libevent //输出所有libevent相关的库文件;
export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig //只对当前终端窗口有效;
history | grep PKG_CONFIG_PATH //以后也这样查看历史命令
找不到库的解决办法:
export LD_LIBRARY_PATH=/user/local/lib //根据具体情况而添加;
ldd target //查看生成的程序连接信息;
//cmakelist.txt中,正确添加libevent库的方法:
cmake_minimum_required(VERSION 3.10)
project(event)
include_directories( P R O J E C T S O U R C E D I R / i n c ) i n c l u d e d i r e c t o r i e s ( / u s r / l o c a l / i n c l u d e / ) s e t ( C M A K E C X X F L A G S − f n o − e l i d e − c o n s t r u c t o r s ) s e t ( C M A K E C X X F L A G S " {PROJECT_SOURCE_DIR}/inc) include_directories(/usr/local/include/) set(CMAKE_CXX_FLAGS -fno-elide-constructors) set(CMAKE_CXX_FLAGS " PROJECTSOURCEDIR/inc)includedirectories(/usr/local/include/)set(CMAKECXXFLAGS−fno−elide−constructors)set(CMAKECXXFLAGS"{CMAKE_CXX_FLAGS} -g -std=c++17 -Wall")
set(LIBEVENT_ROOT /usr/local/lib)
find_library(LIBEVENT_LIB NAMES event HINTS ${LIBEVENT_ROOT})
add_executable(target ./src/main.cpp)
target_link_libraries(target -lrt -lpthread ${LIBEVENT_LIB})
libevent第一个项目程序
#include<stdio.h>
#include<unistd.h>
#include<event2/event.h>
#include<event2/util.h>
//evconnlistener_new_bind的使用
//bufferevent_socket_new的使用
int main(int argc,char** argv)
{
printf("libevent version is %s\r\n",event_get_version());
const char** method = event_get_supported_methods();
for(int i = 0; method[i] != NULL; i++)
{
printf("\t%s\t",method[i]);
}
printf("\n");
struct event_base* base = NULL;
struct event_config* cfg = NULL;
cfg = event_config_new();
event_config_avoid_method(cfg,"epoll");
event_config_avoid_method(cfg,"poll");
base = event_base_new_with_config(cfg);
printf("default is %s\n",event_base_get_method(base));
event_base_free(base);
event_config_free(cfg);
return 0;
}
//setbuf函数用于打开和关闭缓冲机制
// setbuf(stdout,NULL); //关闭了缓冲机制
ss命令 //socket statistic的简称
ldd命令
util读作[油太哦],是utility的缩写 实用,效用
tcpdump 命令抓包
#科学计数法
#3.34e2 == 3.34 X 10的2次方(相当于x100) = 334.0
#3.34e-2 == 3.34 X 10的-2次方(相当于x0.01) = 0.0334
复数类型
用complex函数创建复数