C/C++ ØMQ Server Client Example, ZeroMQ Sender Receiver Pair

C/C++ ØMQ Server Client Example, ZeroMQ Sender Receiver Pair

First, download source files from ZeroMq Download Page and build the project in builds\msvc for Windows to produce required library and dll files. Then:

1. Open a new project.

2. Add ØMQ Include directory and Library directory from your project configuration.

3. Add libzmq.lib to your Linker additional dependencies.

4. Copy libzmq.dll to your build directory.

5. Build and run your first ØMQ applications.

ØMQ Receiver Code

#include "zmq.h"

int main()
{
    void *context = zmq_ctx_new();
    void *responder = zmq_socket(context, ZMQ_PAIR);
    int rc = zmq_bind(responder, "tcp://*:5555");

    printf("Receiver: Started\n");

    char buffer[128];

    while (true)
    {
        int num = zmq_recv(responder, buffer, 128, 0);
       
        if (num > 0)
        {
            buffer[num] = '\0';
            printf("Receiver: Received (%s)\n", buffer);
        }
    }

    return 0;
}

ØMQ Sender Code

#include "zmq.h"

#ifndef _WIN32
#include "unistd.h"
#else
#include "windows.h"
#endif

int main()
{
    char buffer[32];

    void *context = zmq_ctx_new();
    void *requester = zmq_socket(context, ZMQ_PAIR);
    int rc = zmq_connect(requester, "tcp://localhost:5555");

    printf("Sender: Started\n");

    for (int i = 0; i < 10; ++i)
    {
#ifndef _WIN32
        sleep(1000);
#else
        Sleep(1000);
#endif
        sprintf(buffer, "Message %d\0", i + 1);
        printf("Sender: Sending (%s)\n", buffer);
        int rc = zmq_send(requester, buffer, strlen(buffer), 0);
    }

    zmq_close(requester);
    zmq_ctx_destroy(context);

    return 0;
}

Download ZeroMQ Sender and Receiver and try running the application for windows operating systems.


C/C++ ØMQ Server Client Example, ZeroMQ Sender Receiver Pair
added 10 years 7 months ago

Contents related to 'C/C++ ØMQ Server Client Example, ZeroMQ Sender Receiver Pair'

C# ØMQ Server Client Example, C# ZeroMQ Sender Receiver Pair: Example C# zeromq sender receiver source code, C# ØMQ server client model source code examples.

Coordinating C/C++ ØMQ and .NET ØMQ: This page gives source code examples of C/C++ ØMQ and .NET ØMQ that can run in conjunction.

- C# Parse Tree
- How to create xml document in C#
- C# ØMQ Server Client Example, C# ZeroMQ Sender Receiver Pair
- C/C++ ØMQ Server Client Example, ZeroMQ Sender Receiver Pair