Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ZeroMQ server/client with AWS EC2

Hello I wrote a very simple ZMQ server and client application. It works fine on my local machine and it also works fine if I run both apps from within AWS (same security group). I have tried opening the correct port in the AWS security group (both inbound and outbound). I even opened them to All traffic on any port/address. But I don't receive the published messages on my client.

My client code looks like this

#include <string>
#include <iostream>
#include "zmq.hpp"

static std::string s_recv (zmq::socket_t &socket) 
{
    zmq::message_t message;
    socket.recv(&message);
    return std::string(static_cast<char*>(message.data()), message.size()-1);
}

int main (int argc, char* argv[])
{
     std::string adrStr = argv[1];
     zmq::context_t context (1);
     zmq::socket_t subscriber (context, ZMQ_SUB);

     std::cout << "Connecting to server..." << std::endl;
     subscriber.connect(adrStr.c_str());
     const char *filter ="Sending ";

     subscriber.setsockopt(ZMQ_SUBSCRIBE, filter ,strlen(filter));

     int update_nbr=0;
     for (update_nbr=0; update_nbr<100; update_nbr++)
     {
          zmq::message_t update;
          std::string msg_s=s_recv(subscriber);
          std::cout<<"Received something..."<<msg_s<<std::endl;
     }

     return 0;
}

My server side code looks like this

#include <string>
#include <iostream>
#include "zmq.hpp"

int main () 
{
    //  Prepare our context and socket
    zmq::context_t context (1);
    zmq::socket_t publisher (context, ZMQ_PUB);

    std::cout << "Binding to port ..." << std::endl;

    publisher.bind("tcp://*:5556");

    int cnt =0;
    while(1)
    {
         char buffer[30];
         int msgn=0;
         msn = sprint_s(buffer, "Sending number %d", cnt);
         std::cout<<buffer<<std::endl;
         zmq::message_t message(30);
         memcpy((void*) message.data(), buffer,30);

         publisher.send(message);

         Sleep(1);
         cnt++;
     }

     return 0;
 }

In the client app command line I tried passing in the public DNS correctly formatted as "TCP://public-dns:5556" I also tried with the public TCP/IP

Anyone knows what I might be missing ?

like image 453
user3228912 Avatar asked Jan 25 '26 01:01

user3228912


1 Answers

okay so thanks to a friend who helped me figuring out what was going on here is the answer :

basically the application was running on a Windows Server 2012 instance and the windows firewall was preventing any traffic towards my application. Adding the application to the allowed applications list for the windows firewall did the trick.

so here is a recap on what to do to make it work

my server app is now bind to tcp://*:5556 and my client connects to tcp://public-ip:5556

via the EC2 console management , add the security group rule

Custom TCP Aywhere on port 5556

on the server make sure you add the server executable to the allowed applications list.

like image 185
user3228912 Avatar answered Jan 26 '26 17:01

user3228912



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!