[zeromq-dev] getpeername() couldn't resolve the peer address with underlying fd achieved from zmq_socket_monitor ZMQ_EVENT_ACCEPTED event?

Yan, Liming (NSB - CN/Hangzhou) liming.yan at nokia-sbell.com
Wed Oct 24 04:25:02 CEST 2018


HI,
   Could anybody respond to this issue?  

BR
Yan Limin


_____________________________________________
From: Yan, Liming (NSB - CN/Hangzhou) 
Sent: Tuesday, October 16, 2018 4:49 PM
To: 'zeromq-dev at lists.zeromq.org' <zeromq-dev at lists.zeromq.org>
Subject: getpeername() couldn't resolve the peer address with underlying fd achieved from zmq_socket_monitor ZMQ_EVENT_ACCEPTED event?


Hi,
   I created a monitor on a router socket and monitor the ZMQ_EVENT_ACCEPTED event. Each time when a new client accepted, I can get the event and underlying fd. The code is just as the example in page http://api.zeromq.org/4-1:zmq-socket-monitor . Then I need to use getpeername to know who is the peer ip address.  But this seems not work. I can only get 0.0.0.0.  Does this underlying fd provided by monitor support getpeername()? Or anything wrong with me?  Thanks.

{
        char ip[128];
        struct sockaddr_in peer_addr;
        unsigned int peer_len;
        int ret, fd = (int)monitor_event->value;

        ret = getpeername(fd, (struct sockaddr *)&peer_addr, &peer_len);
        if (ret == -1) {
            printf("Get peer name failed. fd=%d. err=%d:%s.\n", fd, errno, strerror(errno));
        }
        else {
            inet_ntop(AF_INET, &peer_addr.sin_addr, ip, sizeof(ip));
            printf("New client accepted: %s:%d. Local socket:%s. fd=%d.\n", ip, ntohs(peer_addr.sin_port),
                         monitor_event->socket_addr, fd);
        }
}
   New client accepted: 0.0.0.0:29186. Local socket:tcp://169.254.0.10:48000. fd=34.

ZMQ_EVENT_ACCEPTED
The socket has accepted a connection from a remote peer. The event value is the FD of the underlying network socket. Warning: there is no guarantee that the FD is still valid by the time your code receives this event.



BR
Yan Limin





More information about the zeromq-dev mailing list