Experience in ACE: Get dynamic allocation listening port numbers

xiaoxiao2021-03-05  24

I don't know if you have encountered such a problem, the code is as follows: ACE_ASYNCH_ACCEPTOR Acceptor; ACE_INET_ADDR Addr = ACE_INET_ADDR ("Hastatus"); if (Acceptor.Open (addr, initial_read_size, 1) == -1) Return -1; How can I get the port number of the listener? I tossed an afternoon, I finally solved.

First, I tried to take the port number through the Winsocket API direct operation, because the Acceptor object provides a get_handler () method

sockaddr_in addr1; int addr1len = sizeof (struct sockaddr); if (! ACE_OS :: getsockname (acceptor.get_handle (), (sockaddr *) & addr1, & addr1len) = 0) {ACE_DEBUG ((LM_DEBUG, "Error!")); } ACE_DEBUG ((LM_Debug, "Port:% D /N", ADDR1.SIN_PORT));

The code is running correctly, the port number also appears in the console, but an invalid port number, that is, the above code is wrong.

Looking for N for a long time online until a class comes with a class comes with a class comes with a class that comes with the ACE, it is finally calculated as follows: ACE_SOCK_SEQPACK_ASSOCIATION A = ACE_SOCK_SEQPACK_ASSOCIATION (Acceptor.get_Handle ()); size_t Addr_size = 1; A.GET_LOCAL_ADDRS (& addr, addr_size); ACE_DEBUG ((LM_DEBUG, "Port:% D / N", addr.get_port_number ()));

This is the true port number in the listener.

I wanted to give up, but I still didn't give up.

转载请注明原文地址:https://www.9cbs.com/read-35499.html

New Post(0)