- 博客(74)
- 收藏
- 关注
原创 TCP客户端非阻塞connect,EPOLL异步响应
废话不多说,直接上代码下面展示一些 内联代码片。#include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <unistd.h> #include <stdlib.h> #include <stdio.h>#include <iostream&g
2020-10-18 23:50:39
1690
1
原创 lightttpd进程守护化
#ifdef HAVE_FORKstatic void daemonize(void) {#ifdef SIGTTOU signal(SIGTTOU, SIG_IGN);#endif#ifdef SIGTTIN signal(SIGTTIN, SIG_IGN);#endif#ifdef SIGTSTP signal(SIGTSTP, SIG_IGN);#endif if (0...
2020-03-31 10:26:57
445
原创 PcapPlusPlus GeneralUtils.h 通用工具解析 16进制字符串转换
GeneralUtils 通用工具头文件中提供了两个开放接口,实现byte数组与16进制字符串相互的转换。byte数组转16进制字符串实现主要使用了 std::stringstream 这个内置类,继承关系如下std::hex 表示后续使用16进制输出。std::hex实际上是一个函数,类似的有std::oct、std::decdataStream << std::s...
2020-03-05 10:13:39
865
原创 PcapPlusPlus TCP数据结构
直接上代码 /** * @struct tcphdr * Represents an TCP protocol header */#pragma pack(push,1) struct tcphdr { /** Source TCP port */ uint16_t portSrc; /** Destination TCP port */ uint16_t por...
2019-12-12 14:58:01
858
原创 C语言使用sem_t同步线程的启动
场景描述:在程序开始阶段需要开启多个线程进行后续的处理,需要同步开启线程(即thread1启动完成后再启动thread2)sem_t pthread_sem; // 信号量,同步线程的启动顺序void ProcessQuery(){ cout<<"QueryHandle, Thread "<<iCoreID<<" Create Successful...
2019-12-03 22:10:35
1165
翻译 pcap入门教程
第一件事就是需要了解 pcap 应用的格式1.发现哪个网卡需要我们去sniff 嗅探linux下也许是eth0BSD下可能是 xl1我们可以将设备定义为string然后打开该设备,或者我们可以让pcap自己查找然后提供我们网卡的名称2.初始化pcap ,实际上就是我们告诉pcap 去嗅探哪个网卡设备。如果我们想的话,可以同时嗅探多个网卡设备。我们使用文件描述符去区分这些网卡,就像我们打...
2019-08-14 14:46:26
3892
原创 协议分析-libpcap的安装与使用
下载源代码http://www.tcpdump.org/release/通过 tar zxvf libpcap-1.0.0.tar.gz 解压文件。进入文件目录后依次执行./configuremake && make install配置过程中缺少什么就使用 sudo apt install xx 安装实例代码代码的作用是读取一个 .pcap 文件,并解析其中的T...
2019-07-24 09:21:18
383
原创 TCP协议笔记
数据结构typedef struct tcp_hdr{ u_short sport:16; // 源端口号 u_short dport:16; // 目标端口号 u_int seq:32; // 序列值 u_int ack:32; // ...
2019-07-23 10:49:12
140
原创 gperftools安装和使用
gperftools 下载和安装github 上找到源码,下载并解压64位机器需要先安装 apt-get install libunwind8-dev./autogen.sh./configuremake -j8# 安装到系统文件夹sudo make installldconfig 刷新动态库文件CPU使用方法一gcc [...] -o myprogram -lprofil...
2019-07-11 09:55:05
5074
原创 获得真正的线程id
使用目的在linux后台服务器系统中,首先需要实现的就是日志库。在使用日志库的过程中会发现,当引入多线程后,为了更好的排查错误和整理业务逻辑需要对多线程的日志进行区分。一个很好的方法就是在每条日志中打印相对应的线程id简单实现#include <unistd.h>long int id = sys_call(SYS_gettid);...
2019-07-02 17:49:00
355
原创 Linux大小端转换实现
实现#include <byteswap.h>#include <stdint.h>/** * @brief 8字节类型的字节序转化 */template<class T>typename std::enable_if<sizeof(T) == sizeof(uint64_t), T>::typebyteswap(T value)...
2019-07-02 14:39:01
3697
原创 django 准备工作
虚拟环境安装(sudo) pip install virtualenv virtualenvwrapperexport WORKON_HOME=$HOME/.virtualenvsexport PROJECT_HOME=$HOME/workspacesource /usr/local/bin/virtualenvwrapper.sh
2019-06-16 22:18:17
120
原创 linux套接字地址结构
IPv4#include <netinet/in.h>struct in_addr{ in_addr s_addr; // 32bit};struct sockaddr_in{ uint8_t sin_len; sa_family_t sin_family; in_port_t sin_port; // 16bit struct in_addr ...
2019-06-14 15:30:06
240
原创 获取文件信息
struct statfd_(::open(filename.c_str(), O_RDONLY | O_CLOEXEC))fd_ 用于打开一个文件,作为 file descriptor struct stat statbuf; // 获取 文件的信息 存放在 statbuf 中 if (::fstat(fd_, &statbuf) == 0) ...
2019-06-14 12:34:36
569
原创 zeromq安装测试
地址sudo sh -c "echo 'deb http://download.opensuse.org/repositories/network:/messaging:/zeromq:/release-stable/xUbuntu_18.04/ /' > /etc/apt/sources.list.d/network:messaging:zeromq:release-stable.lis...
2019-06-04 12:19:21
368
原创 muduo中eventloop、channel、fd解析
最近在研究muduo源代码,因为之前的项目内部的逻辑与muduo类似,不过使用的是libevent完成内核的构造,muduo使用了自己写的poll和epoll方式完成内部的io复用。之前的项目与muduo使用的策略一致,使用了主线程专门用于接收tcp连接,连接后的connection发送到多个子线程中来支持高并发。eventloop.h这个类是muduo的核心,头文件如下(已做注释):///...
2019-05-30 20:41:13
528
原创 eventfd、timerfd_create使用
使用场景在 pipe 仅用于发出事件信号的所有情况下,都可以使用 eventfd 取而代之。exampleint createEventfd(){ int evtfd = ::eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC); if (evtfd < 0) { LOG_SYSERR << "Failed in eventfd...
2019-05-30 20:05:26
5384
原创 docker命令
获得最新的镜像sudo docker pull ubuntu运行镜像sudo docker run -t -i ubuntu /bin/bash查看镜像信息sudo docker images获得镜像详细信息sudo docker inspect 5506de2b643b使用-f 获得其中一项内容搜寻镜像docker search 关键字删除镜像sudo docker rm...
2019-05-26 11:19:17
116
原创 C++ 大小端判断 endian
直接上代码typedef union uEndianTest { struct { bool flittle_endian; bool fill[3]; }; long value;} EndianTest;static const EndianTest __Endian_Test__ = { (long)1 };const bool...
2019-05-18 23:37:28
1070
原创 google benchmark安装
sudo apt install cmakegit clone https://github.com/google/benchmark.gitgit clone https://github.com/google/googletest.git benchmark/googletestmkdir build && cd buildcmake -DCMAKE_BUILD_TY...
2019-05-08 12:04:23
1582
原创 muduo net模块 channel解析
源码// 绑定在 loop 上一个 fd 的管道,针对事件使用回调函数// Copyright 2010, Shuo Chen. All rights reserved.// http://code.google.com/p/muduo///// Use of this source code is governed by a BSD-style license// that c...
2019-04-23 12:57:21
254
原创 FIONREAD 判断 socket有多少数据可读
Ioctl(sockfd, FIONREAD, &npend); /* check FIONREAD support */检查 sockfd 表示的文件描述符中有多少数据可以读取
2019-04-12 00:25:24
1538
原创 muduo AsyncLogging 解析(异步日志)
异步日志原理需要了解其中使用的几个类使用方法muduo::detail::FixedBuffer<muduo::detail::kLargeBuffer> Buffer;std::vector<std::unique_ptr<Buffer>> BufferVector; BufferVector::value_type BufferPtr;其中 Bu...
2019-04-03 21:39:22
960
原创 muduo LogStream解析
数字转字符串template<typename T>size_t convert(char buf[], T value){ T i = value; char* p = buf;// 201 分别取数字 1 0 2 do { int lsd = static_cast<int>(i % 10); i /= 10; *p++...
2019-04-02 23:42:15
320
翻译 clang Thread Safety Analysis安全线程分析
IntroductionClang Thread Safety Analysis is a C++ language extension which warns about potential race conditions in code. The analysis is completely static (i.e. compile-time); there is no run-time o...
2019-03-27 11:55:23
257
原创 C++ 原子操作
n++类type __sync_fetch_and_add(type *ptr, type value, …); // m+ntype __sync_fetch_and_sub(type *ptr, type value, …); // m-ntype __sync_fetch_and_or(type *ptr, type value, …); // m|ntype __sync_fet...
2019-03-26 23:53:03
505
原创 muduo Mutex.h Condition.h 解析
mutex 配合 线程idclass CAPABILITY("mutex") MutexLock : noncopyable{ public: MutexLock() : holder_(0) { MCHECK(pthread_mutex_init(&mutex_, NULL)); } // 只有未上锁的状态可以析构 ~MutexLock() ...
2019-03-26 13:09:09
345
原创 ubuntu clang llvm使用
安装 clang、llvmsudo apt-get install llvmsudo apt-get install clang配置默认的C++编译sudo update-alternatives --config c++There are 2 choices for the alternative c++ (providing /usr/bin/c++).Selection ...
2019-03-26 10:11:54
1844
原创 muduo CurrentThread 解析
丧心病狂的C++优化 inline int tid() { if (__builtin_expect(t_cachedTid == 0, 0)) { cacheTid(); } return t_cachedTid; }__builtin_expect 宏 的作用:其实__builtin_expect这个宏定义变量本身的值是没有产生影响...
2019-03-25 21:46:23
991
原创 muduo type.h 解析 C++类型转换技巧
初始化// 简化了 memset 的使用inline void memZero(void* p, size_t n){ memset(p, 0, n);}隐式转化// 用于在继承关系中, 子类指针转化为父类指针;隐式转换// inferred 推断,因为 from type 可以被推断出来,所以使用方法和 static_cast 相同template<typename ...
2019-03-25 18:45:11
244
原创 epoll server实例
ServerEpollepoll [enhancement poll]this is a async server base on epoll at Linux这是一个在Linux系统上基于epoll机制的异步服务器class EpollService 主要功能就是,创建一个后台服务器监听指定端口号,并且接受来自客户端的连接请求。EpollService 使用两个 epoll 机制来完成...
2019-03-22 22:12:59
1254
原创 epoll_create和epoll_create1踩坑,导致cpu100%
epoll_create 和 epoll_create1的区别不知道但是在使用的过程中发现:运行后使用htop没有什么异常;但是使用epoll_create1后CPU的使用率直接标到了99%,严重的影响性能。...
2019-03-19 10:55:06
6868
4
原创 mysql在的远程连接访问
登录mysql server使用命令行mysql -h localhost -u root -p-h 指定地址-u 指定角色-p 指定密码出现没有授权给 root@‘local’ 的问题使用命令行root 进入mysql,设置root角色的密码:select user, plugin from mysql.user;设置新的密码update mysql.user set aut...
2019-03-17 15:54:47
186
原创 linux下pthread_cleanup_push和pthread_cleanup_pop
pthread_cleanup_push的使用该函数为当前的线程注册一个清理回调函数,清理函数在使用push的时候压入线程的一个栈中。那么该清理函数何时会被调用呢?记住,pthread_cleanup_push必须和pthread_cleanup_pop同时使用。当push后,在线程退出前使用pop,便会调用清理函数。pthread_cleanup_pop有一个参数,如果在最后调用pop...
2019-03-16 22:00:44
1303
原创 C++面试常见问题
在c++程序中调用被c编译器编译后的函数,为什么要加extern “c”?extern 是c/c++语言中表明函数和全局变量作用范围的关键字,该关键字告诉编译器,其声明的函数或者变量可以在本模块或其他模块中使用。extern “c” 是连接声明(linkage declaration),被修饰的函数和变量是按照c语言编译和连接的。c++支持函数重载,但是c语言则不支持函数重载。函数被c++...
2019-03-14 22:47:57
99
原创 python3调用C动态库函数
制作动态库#include <stdio.h>#include <string.h>#include <string>#include <cmath>#include <iostream>using namespace std; extern "C" float Dist(float ax,float ay,float a...
2019-03-09 10:11:20
3014
3
原创 mosquitto 客户端源码剖析 mosquitto_loop_start()
mosquitto 调用形式源码MainServer.h#ifndef _WJ_MAINSERVER_H#define _WJ_MAINSERVER_H#include <event.h>#include <mosquitto.h>class MainServer{public: // single static MainServer&a...
2019-03-06 11:09:59
7222
原创 git 操作记录
git branch查看当前的分支情况git branch -v查看当前的分支情况,并显示commit 信息git checkout origin test选择 test 分支git checkout -b newBranch创建新的分支,并且切换到新的分支...
2019-03-05 17:30:18
114
原创 makefile笔记
程序的编译和链接将源文件编译成中间代码文件,windows下是.obj,linux环境下是.o文件。编译时,只需要语法的正确。把大量的Object File文件合成执行文件,这个动作叫链接。库文件由于源文件太多导致生成的中间目标文件大多,而在链接时需要明显的指出中间目标文件,这很不方便,所以将中间目标文件打包,在unix下是Archive File,也就是.a文件...
2019-03-04 23:45:39
95
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人