Hi,

I don't know if this solves the problem, but you must initialize peer_len with 
sizeof(peer_addr) before calling getpeername.

Also, it is possible that the peer address is not an IPv4 address. You should 
better use sockaddr_storage instead of sockaddr_in and check 
peer_addr.ss_family.

Best regards
Simon

-----Ursprüngliche Nachricht-----
Von: zeromq-dev <zeromq-dev-boun...@lists.zeromq.org> Im Auftrag von Yan, 
Liming (NSB - CN/Hangzhou)
Gesendet: Mittwoch, 24. Oktober 2018 04:25
An: zeromq-dev@lists.zeromq.org
Betreff: Re: [zeromq-dev] getpeername() couldn't resolve the peer address with 
underlying fd achieved from zmq_socket_monitor ZMQ_EVENT_ACCEPTED event?

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@lists.zeromq.org' <zeromq-dev@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 
https://emea01.safelinks.protection.outlook.com/?url=http%3A%2F%2Fapi.zeromq.org%2F4-1%3Azmq-socket-monitor&amp;data=02%7C01%7Csimon.giesecke%40btc-ag.com%7Ccb41745a80fc4815d0d808d63957f56e%7Cc064efb078954eebb406a40bc377bc7d%7C0%7C0%7C636759447274350077&amp;sdata=a0%2BSyH05OinbAGsIx5whElJ9d%2FlMqDA4LEaiq4AA8jE%3D&amp;reserved=0
 . 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


_______________________________________________
zeromq-dev mailing list
zeromq-dev@lists.zeromq.org
https://emea01.safelinks.protection.outlook.com/?url=https%3A%2F%2Flists.zeromq.org%2Fmailman%2Flistinfo%2Fzeromq-dev&amp;data=02%7C01%7Csimon.giesecke%40btc-ag.com%7Ccb41745a80fc4815d0d808d63957f56e%7Cc064efb078954eebb406a40bc377bc7d%7C0%7C0%7C636759447274350077&amp;sdata=C9DGVZ62v%2F2w3ynVrzldljyF0wX7WcBVseVw6c6KfEg%3D&amp;reserved=0
_______________________________________________
zeromq-dev mailing list
zeromq-dev@lists.zeromq.org
https://lists.zeromq.org/mailman/listinfo/zeromq-dev

Reply via email to