C++实现websocket单server单client全双工通信(基于boost!!!)

news/2025/1/15 23:03:58/

  自身环境:ubuntu18.04+gcc7.5.0+boost1.7,3

环境配置

  gcc或者g++一般都有,这里主要介绍一下boost的配置方法
  执行如下代码:

wget https://boostorg.jfrog.io/artifactory/main/release/1.73.0/source/boost_1_73_0.tar.bz2 --no-check-certificate
tar xvf boost_1_73_0.tar.bz2
cd boost_1_73_0
./bootstrap.sh --prefix=/usr 
./b2 
sudo ./b2 install
cat /usr/include/boost/version.hpp | grep "BOOST_LIB_VERSION"

  装完后发现还是会报错:#include <boost/beast/core.hpp> no such file
  这个时候再加一个:

sudo apt-get install libboost-all-dev

  然后编译执行代码就可以了,在这里说明下我不太确定是否要执行那个apt-get,理论上前面是源码编译就没问题了,后面估计是默认库的安装,但是我当时没太注意顺序,两个都做了一下才成功、
boost安装参考:https://blog.csdn.net/HandsomeHong/article/details/128813619

  顺道提一下Linux下查看boost的版本方法:

dpkg -S /usr/include/boost/version.hpp

示例源码

客户端——client.h

#ifndef WEBSOCKET_CLIENT_H
#define WEBSOCKET_CLIENT_H
#include <boost/beast/core.hpp>
#include <boost/beast/websocket.hpp>
#include <boost/asio/ip/tcp.hpp>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <string>
#include <thread>
#include <codecvt>
#include <wchar.h>
#include <locale.h>
#include <stdlib.h>namespace beast = boost::beast;         // from <boost/beast.hpp>
namespace http = beast::http;           // from <boost/beast/http.hpp>
namespace websocket = beast::websocket; // from <boost/beast/websocket.hpp>
namespace net = boost::asio;            // from <boost/asio.hpp>
using tcp = boost::asio::ip::tcp;       // from <boost/asio/ip/tcp.hpp>class Client {public:static std::wstring string_to_wstring(const std::string& str);static std::string wstring_to_string(const std::wstring& ws);static std::string ansi_to_utf8(const std::string& s);static std::string utf8_to_ansi(const std::string& s);static void connect(std::string IP, const char *port_s);static void send(std::string message);static void listen(std::string &out);static void disconnect();
};#endif

客户端——client.cpp

#include "client.h"namespace beast = boost::beast;         // from <boost/beast.hpp>
namespace http = beast::http;           // from <boost/beast/http.hpp>
namespace websocket = beast::websocket; // from <boost/beast/websocket.hpp>
namespace net = boost::asio;            // from <boost/asio.hpp>
using tcp = boost::asio::ip::tcp;       // from <boost/asio/ip/tcp.hpp>net::io_context ioc;
tcp::resolver resolver{ ioc };
websocket::stream<tcp::socket> ws{ ioc };std::wstring Client::string_to_wstring(const std::string& str) {std::wstring r;const char *source = str.c_str();wchar_t *dest = NULL;int len = 0;int ret = 0;len = strlen(source) + 1;if(len <= 1)return 0;dest = new wchar_t[len];ret = mbstowcs(dest, source, len);r = std::wstring(dest);delete[] dest;return r;
}std::string Client::wstring_to_string(const std::wstring& ws) {std::string r = "";const wchar_t *source = ws.c_str();char *dest = NULL;int len = 0;int ret = 0;len = wcslen(source) + 1;if(len <= 1)return 0;dest = new char[len*sizeof(wchar_t)];ret = wcstombs(dest, source, len*sizeof(wchar_t));r = std::string(dest);delete[] dest;return r;
}std::string Client::ansi_to_utf8(const std::string& s) {static std::wstring_convert<std::codecvt_utf8<wchar_t> > conv;return conv.to_bytes(string_to_wstring(s));
}std::string Client::utf8_to_ansi(const std::string& s) {static std::wstring_convert<std::codecvt_utf8<wchar_t> > conv;return wstring_to_string(conv.from_bytes(s));
}void Client::connect(std::string IP, const char *port_s) {try {auto const address = net::ip::make_address(IP); //服务器地址auto const port = static_cast<unsigned short>(std::atoi(port_s));//服务器端口号tcp::endpoint endpoint{ address, port };auto const results = resolver.resolve(endpoint);// 在我们从查找中获得的IP地址上建立连接net::connect(ws.next_layer(), results.begin(), results.end());ws.set_option(websocket::stream_base::decorator([](websocket::request_type& req){req.set(http::field::user_agent,std::string(BOOST_BEAST_VERSION_STRING) +" websocket-client-coro");}));std::cout << "The port is:" << port_s << std::endl;ws.handshake(IP, "/"); //发送握手消息std::cout << "The port:" << port_s << " finish!" << std::endl;}	catch (std::exception const& e){std::cerr << "Error: " << e.what() << std::endl;return  ;}
}void Client::send(std::string message) {while (1) {//std::cout << "log:" << message << std::endl;ws.write(net::buffer(ansi_to_utf8(message)));sleep(1);}
}void Client::listen(std::string &out) {while (1) {beast::flat_buffer buffer;//创建一个缓冲区用于存放接收到的消息	ws.read(buffer);// 读取一条消息到缓冲区out = beast::buffers_to_string(buffer.cdata());//std::cout << utf8_to_ansi(out) << std::endl; //输出消息到控制台显示}
}void Client::disconnect() {ws.close(websocket::close_code::normal);// 关闭WebSocket连接
}

服务端——server.h

#ifndef WEBSOCKET_SERVER_H
#define WEBSOCKET_SERVER_H
#include <boost/beast/core.hpp>
#include <boost/beast/websocket.hpp>
#include <boost/asio/ip/tcp.hpp>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <thread>
#include <codecvt>
#include <wchar.h>
#include <locale.h>
namespace beast = boost::beast;         // from <boost/beast.hpp>
namespace http = beast::http;           // from <boost/beast/http.hpp>
namespace websocket = beast::websocket; // from <boost/beast/websocket.hpp>
namespace net = boost::asio;            // from <boost/asio.hpp>
using tcp = boost::asio::ip::tcp;       // from <boost/asio/ip/tcp.hpp>class Server {public:static std::wstring string_to_wstring(const std::string& str);static std::string wstring_to_string(const std::wstring& ws);static std::string ansi_to_utf8(const std::string& s);static std::string utf8_to_ansi(const std::string& s);static void do_session(tcp::socket& socket);static void initlization(std::string IP);static void initlization1(std::string IP);
};
#endif

服务端——server.cpp

#include "server.h"std::string send_message("server to client");std::wstring Server::string_to_wstring(const std::string& str) {std::wstring r;const char *source = str.c_str();wchar_t *dest = NULL;int len = 0;int ret = 0;len = strlen(source) + 1;if(len <= 1)return 0;dest = new wchar_t[len];ret = mbstowcs(dest, source, len);r = std::wstring(dest);delete[] dest;return r;
}std::string Server::wstring_to_string(const std::wstring& ws) {std::string r = "";const wchar_t *source = ws.c_str();char *dest = NULL;int len = 0;int ret = 0;len = wcslen(source) + 1;if(len <= 1)return 0;dest = new char[len*sizeof(wchar_t)];ret = wcstombs(dest, source, len*sizeof(wchar_t));r = std::string(dest);delete[] dest;return r;
}std::string Server::ansi_to_utf8(const std::string& s) {static std::wstring_convert<std::codecvt_utf8<wchar_t> > conv;return conv.to_bytes(string_to_wstring(s));
}std::string Server::utf8_to_ansi(const std::string& s) {static std::wstring_convert<std::codecvt_utf8<wchar_t> > conv;return wstring_to_string(conv.from_bytes(s));
}// socket 20000  socket1 19999
void Server::do_session(tcp::socket& socket) {try{websocket::stream<tcp::socket> ws{ std::move(socket) };ws.set_option(websocket::stream_base::decorator([](websocket::response_type& res){res.set(http::field::server,std::string(BOOST_BEAST_VERSION_STRING) +" websocket-server-sync");}));ws.accept();//等待客户端连接for (;;){if (send_message != "")ws.write(net::buffer(ansi_to_utf8(send_message)));beast::flat_buffer buffer;// 这个缓冲区将保存传入的消息ws.read(buffer);// 读取一条消息auto out = beast::buffers_to_string(buffer.cdata());if (out != "") {std::cout << utf8_to_ansi(out) << std::endl;}sleep(1); //等待1秒}}catch (beast::system_error const& se){if (se.code() != websocket::error::closed)std::cerr << "Error: " << se.code().message() << std::endl;}catch (std::exception const& e){std::cerr << "Error: " << e.what() << std::endl;}
}void Server::initlization(std::string IP) {try{auto const address = net::ip::make_address(IP);//绑定ip地址auto const port = static_cast<unsigned short>(std::atoi("20000"));//绑定端口号net::io_context ioc{ 1 };tcp::acceptor acceptor{ ioc,{ address, port } };for (;;){tcp::socket socket{ ioc };acceptor.accept(socket);// 开启线程等待客户端的连接请求std::thread{ std::bind(&do_session,std::move(socket)) }.detach();}}catch (const std::exception & e) {std::cerr << "Error: " << e.what() << std::endl;return ;}
}

客户端调用——main_client.cpp

#include "client.h"Client client1_listen;
Client client1_send;
Client client;
std::string listen_message = "";
std::string IP_server("192.168.1.116");
std::string send_message("client to server");
const char *port = "20000";void listenThread() {// client1_listen.connect(IP_server, port_client1_listen);  client.listen(listen_message);
}void sendThread() {// client1_send.connect(IP_server, port_client1_send);client.send(send_message);
}void outputThread() {while (1) {if (listen_message != "") {std::cout << listen_message << std::endl;}sleep(1);}
}int main() { client.connect(IP_server, port);std::thread sendThreadObj(sendThread);std::thread listenThreadObj(listenThread);std::thread outputThreadObj(outputThread);listenThreadObj.join();sendThreadObj.join();outputThreadObj.join();return 0;
}

服务端调用——main_server.cpp

#include "server.h"
#include <iostream>
#include <thread>// 20000      19999
int main() {Server server;std::string IP("192.168.1.116");server.initlization(IP);return 0;
}

执行步骤

g++ main_client.cpp client.cpp client.h -o client -std=c++11 -lpthread
g++ main_server.cpp server.cpp server.h -o server -std=c++11 -lpthread必须先server再client
./server
./client收发使用20000端口(可自行更改)

http://www.ppmy.cn/news/695328.html

相关文章

如何通过Python下载GSMap数据集(解决.dat无法打开的问题)?

目录 01 前言 02 GSMap-MVK的存储方式和数据集介绍 03 代码实现 01 前言 这么晚了我还是希望将这篇博客写一下&#xff0c;记录生活。 我所下载的数据集为GSMap-MVK数据集&#xff0c;延迟大概2.5月左右我记得.边下载我就想着先处理着吧。 例如&#xff0c;其中一个文件如…

MACBOOK快捷键输入

快捷键输入 通用 Command Z 撤销Command X 剪切Command C 拷贝&#xff08;Copy&#xff09;Command V 粘贴Command A 全选&#xff08;All&#xff09;Command S 保存&#xff08;Save)Command F 查找&#xff08;Find&#xff09; 截图 Command Shift 3 截取全部…

Macbook常用快捷键

1. Mac常用快捷键符号 2. 快捷键操作 2.1 基本 操作 Command-Z 撤销Command-X 剪切  Command-C 拷贝&#xff08;Copy&#xff09;  Command-V 粘贴  Command-A 全选&#xff08;All&#xff09;Command-S 保存&#xff08;Save) Command-F 查找&#xff08;Find&…

Incase City 不同尺寸背包选择,根据不同 Macbook Pro 尺寸,尺寸汇总

Incase City 不同尺寸背包选择&#xff0c;根据不同 Macbook Pro 尺寸&#xff0c;尺寸汇总 最近才发现 Incase 这个品牌&#xff0c;背包确实好看。不像那些烂大街的方方正正的背包&#xff0c;太jb难看了。 打算买一个 Incase&#xff0c;去官网 Incase 背包 看了半天&#x…

为什么你值得入手一个 Macbook?

在开始看正文之前&#xff0c;首先明确 Macbook 适合的人群&#xff1a;学生、白领、文字工作者等需要高生产力工具的人群。如果你本身是一个游戏爱好者&#xff0c;PC 对你来说最大的应用场景就是游戏&#xff0c;那么你也许有更好的选择&#xff01; 此文写给即将步入大学学…

ios研发Mini LED技术 iPad/MacBook尺寸将更大

著名苹果分析师郭明錤(Ming-Chi Kuo)的研究称,苹果在未来几年可能推出几款新产品,而这几款新产品使用的是一种新显示技术,能够更好地显示颜色和对比度。 下一代“mini LED”技术的这几款新产品是: 一款“全新设计”的31.6英寸显示器,它将于2019年第二季度或第三季度推出;…

笙默考试管理系统-MySelfTest(18)

笙默考试管理系统-MySelfTest(18&#xff09; 目录 一、 笙默考试管理系统-MySelfTest 二、 笙默考试管理系统-MySelfTest 三、 笙默考试管理系统-MySelfTest 四、 笙默考试管理系统-MySelfTest 五、 笙默考试管理系统-MySelfTest 笙默考试管理系统-MySelfTest /* $(…

在Django中,如何使用ORM?

让我们来了解一下什么是ORM。ORM&#xff0c;就是对象关系映射&#xff08;Object Relational Mapping&#xff09;&#xff0c;是一种将数据库表映射到Python对象的技术。在Django中&#xff0c;ORM是由框架自动生成的&#xff0c;你不需要手动写SQL语句&#xff0c;只需要使用…