QT MSVC中文编译出错error: C2001: 常量中有换行符 — 解决方案

一直提示 warning: C4819: 该文件包含不能在当前代码页(936)中表示的字符。请将该文件保存为 Unicode 格式以防止数据丢失. 这如何忍的了

warning: C4819: 该文件包含不能在当前代码页(936)中表示的字符。请将该文件保存为 Unicode 格式以防止数据丢失

error: C2001: 常量中有换行符

error: C2146: 语法错误: 缺少“)”(在标识符“label”的前面)

error: C2146: 语法错误: 缺少“;”(在标识符“label”的前面)

直接百度搜解决方案 如下:
在msvc编译器下的include/QtCore里修改qglobal.h文件
添加

#pragma execution_character_set("utf-8")

编译mysql for Qt 5.13.1驱动

windows下载qt-everywhere-src-5.13.1.zip源码包

编译mysql for Qt驱动
1.编辑源码:D:\qt-everywhere-src-5.13.1\qtbase\src\plugins\sqldrivers\mysql\mysql.pro

构建项目成功后在D:/plugins/sqldrivers下有
qsqlmysql.dll
qsqlmysql.lib
qsqlmysqld.dll
qsqlmysqld.lib

1、把qt生成的*.dll放入到qt的安装目录下
E:\Qt\Qt5.13.1\5.13.1\msvc2017\plugins\sqldrivers
2、把mysql的dll库D:\mysql-5.7.27-win32\lib下的*.dll复制到qt安装目录下的E:\Qt\Qt5.13.1\5.13.1\msvc2017\bin目录里
否则会报 “Driver not loaded Driver not loaded”

crontab

;每天早晚8点同步授时中心的时间
0 8,20 * * * ntpdate asia.pool.ntp.org >> /script/crontab.log

;每1分钟执行一次shell脚本,检测server的master进程是否存活,如果存在则跳过。如果发现主进程已经挂掉,则执行restart逻辑,先kill掉所有残留的子进程,然后重新启动Server。
*/1 * * * * /script/check_system_server.sh

;每天0点到8点每4小时执行一次reload.sh 保持mysql不断线
* 0-8/4 * * * /script/reload.sh

;每天下午3点执行
* 15 * * * /usr/local/php/bin/php /script/t.php >> /script/crontab.log

说明:
systemctl start crond
systemctl stop crond
systemctl restart crond
systemctl reload crond
systemctl status crond

开机自启动: systemctl enable crond.service

查看当前用户的crontab: 输入crontab -l 或 crontab -l -u root

编辑: crontab -e

格式: Minute Hour DayofMonth Month DayofWeek CommandPath

参数说明:
Minute:每个小时的第几分钟执行该任务;取值范围0-59
Hour:每天的第几个小时执行该任务;取值范围0-23
Day:每月的第几天执行该任务;取值范围1-31
Month:每年的第几个月执行该任务;取值范围1-12
DayOfWeek:每周的第几天执行该任务;取值范围0-6,0表示周末
CommandPath:指定要执行的程序路径

时间格式:
* :表示任意的时刻;如小时位 * 则表示每个小时
n :表示特定的时刻;如小时位 5 就表示5时
n,m :表示特定的几个时刻;如小时位 1,10 就表示1时和10时
n-m :表示一个时间段;如小时位 1-5 就表示1到5点
*/n : 表示每隔多少个时间单位执行一次;如小时位 */1 就表示每隔1个小时执行一次命令,也可以写成 1-23/1

实例:
30 21 * * * /etc/init.d/nginx restart //每晚的21:30重启 nginx。
45 4 1,10,22 * * /etc/init.d/nginx restart //每月1、 10、22日的4 : 45重启nginx。
10 1 * * 6,0 /etc/init.d/nginx restart //每周六、周日的1 : 10重启nginx。
0,30 18-23 * * * /etc/init.d/nginx restart //每天18 : 00至23 : 00之间每隔30分钟重启nginx。
0 23 * * 6 /etc/init.d/nginx restart //每星期六的11 : 00 pm重启nginx。
* */1 * * * /etc/init.d/nginx restart //每一小时重启nginx
* 23-7/1 * * * /etc/init.d/nginx restart //晚上11点到早上7点之间,每 隔一小时重启nginx
0 11 4 * mon-wed /etc/init.d/nginx restart //每月的4号与每周一到周三 的11点重启nginx
0 4 1 jan * /etc/init.d/nginx restart //一月一号的4点重启nginx
*/30 * * * * /usr/sbin/ntpdate 210.72.145.20 //每半小时同步一下时间

c++ 解决TCP粘包问题

#include < iostream>
#include < Windows.h>
#include < vector>
#include < string>
#include < algorithm>
#include < time.h>
#include < exception>

#pragma comment(lib,"ws2_32.lib")
using namespace std;

WSADATA wsaData;
SOCKET hSocket;
SOCKADDR_IN servAddr;

size_t readn(SOCKET socket, void* buf, size_t count)
{
	int left = count; //剩下的字节
	char* ptr = (char*)buf;
	while (left > 0)
	{

		int readBytes = recv(socket, ptr, left, 0);
		if (readBytes < 0)//read函数小于0有两种情况:1中断 2出错
		{
			if (errno == EINTR)//读被中断
			{
				continue;
			}
			return -1;
		}
		if (readBytes == 0)//读到了EOF
		{
			// 服务器关闭
			return count - left;
		}
		left -= readBytes;
		ptr += readBytes;
	}
	return count;
}

/// 从缓冲区中读取指定长度的数据,但不清除缓冲区内容
size_t read_peek(int sockfd, char *buf, size_t len)
{
	while (1)
	{
		int ret = recv(sockfd, buf, len, MSG_PEEK);
		if (ret == 0)
		{
			std::cout << "服务器连接断开!!!" << std::endl;
			return 0;
		}
		if (ret == -1 && errno == EINTR)
		{
			//出现中断
			continue;
		}
		return ret;
	}
}

/// 按行读取数据
/// sockfd : 套接字
/// buf :应用层缓冲区,保存读取到的数据
/// maxline : 所规定的一行的长度
size_t readLine(int sockfd, void *buf, size_t maxline)
{
	int ret;
	int nRead = 0;
	int left = maxline; //剩下的字节数
	char * pbuf = (char *)buf;
	int count = 0;
	while (1)
	{
		ret = read_peek(sockfd, pbuf, left);   //读取长度为left的socket缓冲区内容
		if (ret <= 0)
		{
			return ret;
		}
		nRead = ret;
		for (int i = 0; i < nRead; i++)//看看读取出来的数据中是否有换行符\n
		{
			if (pbuf[i] == '\n')//如果有换行符
			{
				ret = readn(sockfd, pbuf, i + 1);//读取一行
				if (ret != i + 1)	//一定会读到i+1个字符,否则是读取出错
				{
					exit(EXIT_FAILURE);
				}
				return ret + count;
			}
		}
		
		//窥探的数据中并没有换行符
		//把这段没有\n的读出来
		ret = readn(sockfd, pbuf, nRead);
		if (ret != nRead)
		{
			exit(EXIT_FAILURE);
		}
		pbuf += nRead;
		left -= nRead;
		count += nRead;
	}
	return -1;
}

int main()
{
	if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
	{
		cout << "WSAStartup() error" << endl;
		return -1;
	}

	hSocket = socket(PF_INET, SOCK_STREAM, 0);
	if (hSocket == INVALID_SOCKET)
	{
		cout << "hSocket() error" << endl;
		return -1;
	}

	memset(&servAddr, 0, sizeof(servAddr));
	servAddr.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
	servAddr.sin_family = AF_INET;
	servAddr.sin_port = htons(8888);

	if (connect(hSocket, (SOCKADDR*)& servAddr, sizeof(SOCKADDR)))
	{
		cout << "连接失败!" << endl;
		closesocket(hSocket);
		WSACleanup();

		return -1;
	}

	while (true)
	{
		int ret = readLine(hSocket, recvbuf, 1024);
		if (ret == -1)
		{
			cerr << "readline" << endl;
		}
		else if (ret == 0)
		{
			printf("service closed\n");
			break;
		}
		string ttt = recvbuf;
		cout << ttt.substr(0, ret);
		memset(recvbuf, 0, sizeof(recvbuf));
	}
	return 0;
}

c++ windows 创建进程 和 传参

main.cpp

#include <iostream>
#include <vector>
#include <string>

std::vector account_list;
void ThreadTcp(LPVOID lpParam)
{
	cout << "TCP线程开始" << endl;

	vector *account_list = (vector *)lpParam;

	WSADATA wsaData;
	SOCKET hSocket;
	SOCKADDR_IN servAddr;

	int strLen = 0;
	int idx = 0, readLen = 0;
	if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
	{
		std::cout << "WSAStartup() error" << std::endl;
	}

	hSocket = socket(PF_INET, SOCK_STREAM, 0);
	if (hSocket == INVALID_SOCKET)
	{
		std::cout << "hSocket() error" << std::endl;
	}

	memset(&servAddr, 0, sizeof(servAddr));
	servAddr.sin_addr.S_un.S_addr = inet_addr("139.224.118.131");
	servAddr.sin_family = AF_INET;
	servAddr.sin_port = htons(9035);
	int conn_ret = -111;
	if (conn_ret = connect(hSocket, (SOCKADDR*)& servAddr, sizeof(SOCKADDR)))
	{
		std::cout << "连接失败!" << std::endl;
		std::cin.get();
	}

	int n = 0;
	char recvBuf[2000];
	char sendBuf[2000];

	while (true)
	{
		ZeroMemory(recvBuf, 2000);
		recv(hSocket, recvBuf, sizeof(recvBuf), 0);
		if (strlen(recvBuf) == 0)
		{
			std::cout << "断开连接" << std::endl;
			break;
		}
		printf("recv:%s", recvBuf);
		
		for (int i = 0; i < 1000; i++)
		{
			sprintf_s(sendBuf, "Hello, This is client %d\r\n", i);
			//send(hSocket, sendBuf, strlen(sendBuf), 0);
		}

		if (std::count(account_list->begin(), account_list->end(), "100444024"))
		{
			cout << "已有账号" << endl;
		}
		else
		{
			cout << "添加登录账号" << endl;
			account_list->push_back("100444024");

			TThostFtdcBrokerIDType brokerID = "9999";
			TThostFtdcInvestorIDType investerID = "100444024";
			TThostFtdcPasswordType investerPassword = "123456";
			char tradeFrontAddr[] = "tcp://139.129.39.75:1234";
			addAccount(brokerID, investerID, investerPassword, tradeFrontAddr);
		}
	}

	closesocket(hSocket);
	WSACleanup();
}

int main()
{
    HANDLE Thread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)ThreadTcp, &account_list, 0, NULL);
}

c++ hiredis

#include <cstdio>
#include <string.h>
#include <hiredis/hiredis.h>

using namespace std;

int main()
{
    redisContext *conn = redisConnect("127.0.0.1", 6379);
    if (conn != NULL && conn->err)
    {
        printf("connection error: %s\n", conn->errstr);
        return 0;
    }

    redisReply *reply;
    for (int i=0;i<10000;i++)
    {
        reply = (redisReply*)redisCommand(conn, "publish mychannel test:{\"username\":\"BlackMamba\",\"password\":123456}"); // publish message 中不能有空格
        freeReplyObject(reply);
    }
    
    redisFree(conn);
    return 0;
}

//g++ -o redis Redis.cpp -lhiredis -levent

c++11 json 处理

c++11 json的库 https://github.com/dropbox/json11

tcp接收json字符串并处理

void *ThreadTcp(void *ptr)
{
int listenfd, connfd;
struct sockaddr_in servaddr;
char buff[4096];
int n;

if( (listenfd = socket(AF_INET, SOCK_STREAM, 0)) == -1 ){
printf("create socket error: %s(errno: %d)\n", strerror(errno), errno);
return 0;
}

memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(9060);

if( bind(listenfd, (struct sockaddr*)&servaddr, sizeof(servaddr)) == -1){
printf("bind socket error: %s(errno: %d)\n", strerror(errno), errno);
return 0;
}

if( listen(listenfd, 10) == -1){
printf("listen socket error: %s(errno: %d)\n", strerror(errno), errno);
return 0;
}

printf("======waiting for client's request======\n");
while(1){
if( (connfd = accept(listenfd, (struct sockaddr*)NULL, NULL)) == -1){
printf("accept socket error: %s(errno: %d)", strerror(errno), errno);
continue;
}
cout << connfd << endl; n = recv(connfd, buff, MAXLINE, 0); buff[n] = '\0'; //printf("recv msg from client: %s\n", buff); string err_comment; auto recv_data = Json::parse(buff, err_comment); Json::array account_list = recv_data.array_items(); for (auto &account_data : account_list) { cout << "account: " << account_data["account"].string_value() << endl; cout << "password: " << account_data["password"].string_value() << endl; } //close(connfd); } close(listenfd); return 0; }