Win+VisualStudio+vcpkg+Zeromq安装方法

1. 缘由

因为工作上要用到Windows上的zeromq来收发消息,所以我在网上搜集了一些资料最终成功地在Visual Studio2022中用c的libzmq库实现了zmq的收发。

2. 基本资料

2.1 ZeroMQ基本介绍

ZeroMQ官网介绍,因为我也不是专门搞网络和通信的,就不过多赘述了。至于为什么要用ZMQ,老板的任务罢了......

ZeroMQ (also spelled ØMQ, 0MQ or ZMQ) is a high-performance asynchronous messaging library, aimed at use in distributed or concurrent applications. It provides a message queue, but unlike message-oriented middleware, a ZeroMQ system can run without a dedicated message broker.

ZeroMQ supports common messaging patterns (pub/sub, request/reply, client/server and others) over a variety of transports (TCP, in-process, inter-process, multicast, WebSocket and more), making inter-process messaging as simple as inter-thread messaging. This keeps your code clear, modular and extremely easy to scale.

ZeroMQ is developed by a large community of contributors. There are third-party bindings for many popular programming languages and native ports for C# and Java.

2.2 vcpkg基本介绍

vcpkg官网介绍,大概就是一个跨平台安装开源库的工具:

vcpkg is a cross-platform package manager for C and C++ developers. Using vcpkg you can quickly get thousands of high quality open source libraries to empower your application and internally share collections of private components.

3. 安装

3.1 安装vcpkg

环境要求:

  • Windows 7 or newer
  • Git
  • Visual Studio 2015 Update 3 or newer
#clone vcpkg repo 如果访问github不成功,请自行解决
git clone https://github.com/Microsoft/vcpkg.git

#bootstrap
.\vcpkg\bootstrap-vcpkg.bat #for powershell
.\vcpkg\bootstrap-vcpkg.sh #for cmd

3.2 安装libzmq

进入vcpkg安装目录,执行以下命令:

(不同语言的zmq封装库不同,这里选择的是c语言版本的libzmq库)

#install libzmq
./vcpkg install zeromq:x64-windows #for x64
./vcpkg install zeromq #for x86

3.3 配置VisualStudio工程文件

3.3.1 Nuget包配置方法

进入到VisualStudio工程下打开终端:

vcpkg integrate project #生成项目配置

工程Nuget包配置方法详见:Visual Studio 2019配置 vcpkg C++包管理工具

3.3.2 cmake配置方法

cmake配置vcpkg方法:

cmake -B [build directory] -S . -DCMAKE_TOOLCHAIN_FILE=[path to vcpkg]/scripts/buildsystems/vcpkg.cmake

#build
cmake --build [build directory]

CMakeLists.txt文件编写:

cmake_minimum_required (VERSION 3.8)


add_executable (zmp_server "zmp_server.cpp" "zmp_server.h")


include_directories("C:/Users/oceanstar/vcpkg/win/vcpkg/installed/x64-windows/include")
link_directories("C:/Users/oceanstar/vcpkg/win/vcpkg/installed/x64-windows/lib")



find_package(ZeroMQ CONFIG REQUIRED)
target_link_libraries(${PROJECT_NAME} PRIVATE libzmq libzmq-static)

4. 测试

参考官网给出的测试程序,稍微改了个测试程序:

  • 服务端:
//zmq_server.cpp
#include 
#include 
#include 
#include 
#ifndef _WIN32
#include 
#else
#include 
#endif

int main(void)
{
    //  Socket to talk to clients
    void* context = zmq_ctx_new();
    void* responder = zmq_socket(context, ZMQ_REP);
    int rc = zmq_bind(responder, "tcp://*:4396");
    assert(rc == 0);

    while (1) {
        char* buffer = new char[40];
        zmq_recv(responder, buffer, 40, 0);
        printf("%s\n",buffer);
        zmq_send(responder, "World", 5, 0);
    }
    return 0;
}
  • 客户端:
     
//zmq_client.cpp
#define _CRT_SECURE_NO_WARNINGS
#include 
#include 
#include 
#ifndef _WIN32
#include 
#else
#include 
#endif



int main(void)
{
    printf("Connecting to hello world server¡­\n");
    void* context = zmq_ctx_new();
    void* requester = zmq_socket(context, ZMQ_REQ);
    zmq_connect(requester, "tcp://localhost:4396");

    int request_nbr;
    for (request_nbr = 0; request_nbr != 10; request_nbr++) {
        char buffer[10];
        char* TLresult = new char[40];
        strcpy(TLresult, "Received from client!");
        //printf("%s\n", TLresult);
        zmq_send(requester, TLresult , 40, 0);
        zmq_recv(requester, buffer, 10, 0);
    }
    zmq_close(requester);
    zmq_ctx_destroy(context);
    return 0;
}
  • 同时启动server和client,在server端可以受到client端发来的字符串,即配置成功:

Win+VisualStudio+vcpkg+Zeromq安装方法_第1张图片

 由于网上资料比较乱,作者本人对VS的了解也有限,总体写得比较粗糙,如有错误遗漏欢迎大家指出。

参考:

  • window安装ZMQ

  • C/C++编程:ZeroMQ安装以及使用(windows+centos)

  • zeromq/libzmq

  • vcpkg

  • zmq_recv – 从一个socket上接收一个消息帧

你可能感兴趣的:(通信开发,环境配置,开发语言,c语言,visualstudio,tcp/ip,信息与通信)