网路编程
异步续
异步Demo
Session
- TCP 会话(Session)
- 封装 TCP 套接字
- 异步读写框架
- 数据缓冲区
源码
头文件
class Session
{
public:
Session(io_context& ioc):_socket(ioc){}//构造函数
tcp::socket& Socket() {//返回套接字
return _socket;
}
void Start();//启动会话
private:
void handle_read(const boost_error_code& error,size_t bytes_transferred );//异步读回调函数
void handle_write(const boost_error_code& error);//异步写回调函数
tcp::socket _socket;//套接字
enum {max_length=1024};//缓冲区最大长度
char _data[max_length];//缓冲区
};
实现
void Session::Start()
{
memset(_data, 0, max_length);//初始化缓冲区
_socket.async_read_some(boost::asio::buffer(_data, max_length),
bind(&Session::handle_read, this, std::placeholders::_1, std::placeholders::_2));//异步读
}
void Session::handle_read(const boost_error_code& error, size_t bytes_transferred)//异步读回调函数
{
if (!error)
{
cout << "server receive data is" << _data << endl;
boost::asio::async_write(_socket, boost::asio::buffer(_data, bytes_transferred),
std::bind(&Session::handle_write, this, std::placeholders::_1)); //异步写
}
else
{
cout << "have error in read,error code is" << error.value() << endl;
delete this;
}
}
void Session::handle_write(const boost_error_code& error)//异步写回调函数
{
if (!error)
{
memset(_data, 0, max_length);
_socket.async_read_some(boost::asio::buffer(_data, max_length),
bind(&Session::handle_read, this, std::placeholders::_1, std::placeholders::_2)); //异步读
//也可以使用Start()
}
else
{
cout << "have error in write,error code is" << error.value() << endl;
delete this;
}
}
Server
源码
头文件
class Server
{
public:
Server(io_context& ioc, short port);
private:
void start_accept();
void handle_accept(Session* new_session,const boost_error_code& error);
io_context& _ioc;
tcp::acceptor _acceptor;
};
实现
Server::Server(io_context& ioc, short port) :_ioc(ioc), _acceptor(ioc, tcp::endpoint(tcp::v4(), port))
{
cout << "Server starting" << endl;
start_accept();
}
void Server::start_accept()
{
Session* new_session = new Session(_ioc);
_acceptor.async_accept(new_session->Socket(), std::bind(&Server::handle_accept, this, new_session,
std::placeholders::_1));
}
void Server::handle_accept(Session* new_session, const boost_error_code& error)
{
if (!error)
{
new_session->Start();
}
else {
delete new_session;
}
start_accept();
}
- 只实现应答式的,无法实现全双工
MySQL数据库
5.函数
5.1string字符串
5.1.1CONCAT
CONCAT(s1,s2,s3,……,sn)
- 拼接字符串
5.1.2LOWER
LOWER(str)
- 全部转换成小写
5.1.3UPPER
UPPAER(str)
- 全部转接成大写
5.1.4LPAD
LPAD(str,n,pad)
- 左填充字符串使数据达到n个字符长度
5.1.5RPAD
RPAD(str,b,pad)
- 右填充字符串使数据达到n个字符长度
5.1.6TRIM
TRIM(str)
- 去除首尾空格
5.1.7SUBSTRING
SUBSTRING(str,start,len)
- 截取字符串
5.2数值函数
5.2.1CEIL向上取整
CEIL(x)
5.2.1CEIL向上取整
CEIL(x)
5.2.2FLOOR向下取整
FLOOR(x)
5.2.3MOD返回x/y的模
MOD(x,y)
5.2.4RAND返回0-1的随机数
RAND(x)
5.2.5 ROUND四舍五入
ROUND(x,y)
5.3日期函数
5.3.1CURDATE
CURDATE()
- 获取当前日期
5.3.2CURTIME
CURTIME()
- 获取当前时间
5.3.3NOW
NOW()
- 获取当前的日期和时间
5.3.4YEAR
YEAR(date)
获取指定date的数据
5.3.5MONTH
MONTH(date)
- 获取指定date的月份
5.3.6DAY
DAY(date)
- 获取指定date的日期
5.3.7DATE_ADD
DATE_ADD(date,INTERVAL expr type)
返回一个日期/时间值加上一个时间间隔expr后的时间值
5.3.8DATEDIFF
DATEDIFF(date1,date2)
- 返回起始时间date1和结束时间date2之间的天数
5.4流程函数
5.4.1IF
IF(value,t,f)
- value为ture则返回t,否则返回f
5.4.2IFNULL
IFNULL(value1,value2)
- 如果vakue1不为空则返回value1,否则返回value2
5.4.3CASE WHEN
CASE WHEN [val1] THEN [res1] …… ELSE [default] END
- 如果val1的值为ture则返回res1否则返回默认值
- 使用when[val1]then[val2]进行向后判断
5.4.4CASE [expr] WHEN
CASE [expr] WHEN [val1] THEN [res1] …… ELSE [default] END
- 如果expr的值等于val1返回res1,……否则返回default的默认值
vue3
安装环境
- 1.node.js
- 2.npm create vue@latest
- PS:将会在当前目录下创建vue项目
启动
- npm vue dev
构建成html的
- npm vue build
结构
- 根目录下的index.html是目录开头
- src/apps.vue是被index.html引用
- src/components保存为各种模块的vue
