zeromq / cppzmq

Header-only C++ binding for libzmq
http://www.zeromq.org
MIT License
1.94k stars 757 forks source link

recv called with sender address:port on udp packet: #493

Closed Yuiry-IV closed 3 years ago

Yuiry-IV commented 3 years ago

Hello colleagues,

I've send a plain text data via UDP socket and on service side receive additional recv call with sender address:port as data. Please see more for details.

Environment:

Execute on client side:

nc -u 127.0.0.1 4000

Input:

6912226168095704431694161020163
4198627489852171564193675778487
2906634672860277391853933451413

And receive on cppzmq service side:

wait:
0:16:16->127.0.0.1:58968
1:32:32->6912226168095704431694161020163

2:16:16->127.0.0.1:58968
3:32:32->4198627489852171564193675778487

4:16:16->127.0.0.1:58968
5:32:32->2906634672860277391853933451413

This is my source code:

int main()
{
   try{
       zmq::context_t ctx;
       zmq::socket_t sock(ctx, zmq::socket_type::dgram);
       sock.bind("udp://*:4000");
       std::cout<<"wait:"<<std::endl;

       for(unsigned i=0; ;++i){
         char buf[4200]="";
         zmq::mutable_buffer mbuf(buf,100);

         zmq::recv_buffer_result_t r = sock.recv( mbuf );

         if( r ){         
            std::string s( (char *)mbuf.data(), r.value().size );
            std::cout<<i<<":" <<r.value().size
                        <<":" <<r.value().untruncated_size
                        <<"->"<<s<<"\n";
         }
       }
    }
    catch(std::exception &ex){
      std::cout<<"catch:"<<ex.what()<<std::endl;
    }
    return 0;
}

Could you please help me to solve this issue ?

gummif commented 3 years ago

I don't see what the problem is. This is an undocumented socket type, and apparently each messages has two frames, the sender address and the content. You could use zmq::recv_multipart for easier handler of multipart messages.

Yuiry-IV commented 3 years ago

Hello @gummif,

Thank you, for response. Currently, issue is not critical anymore, due to our internal reasons.