in demo_example/asio/asio/detail/impl/socket_ops.ipp [3214:3291]
inline int gai_serv(addrinfo_type* aihead,
const addrinfo_type* hints, const char* serv)
{
using namespace std;
int num_found = 0;
if (
#if defined(AI_NUMERICSERV)
(hints->ai_flags & AI_NUMERICSERV) ||
#endif
isdigit(static_cast<unsigned char>(serv[0])))
{
int port = htons(atoi(serv));
if (hints->ai_socktype)
{
// Caller specifies socket type.
int rc = gai_port(aihead, port, hints->ai_socktype);
if (rc < 0)
return EAI_MEMORY;
num_found += rc;
}
else
{
// Caller does not specify socket type.
int rc = gai_port(aihead, port, SOCK_STREAM);
if (rc < 0)
return EAI_MEMORY;
num_found += rc;
rc = gai_port(aihead, port, SOCK_DGRAM);
if (rc < 0)
return EAI_MEMORY;
num_found += rc;
}
}
else
{
// Try service name with TCP first, then UDP.
if (hints->ai_socktype == 0 || hints->ai_socktype == SOCK_STREAM)
{
servent* sptr = getservbyname(serv, "tcp");
if (sptr != 0)
{
int rc = gai_port(aihead, sptr->s_port, SOCK_STREAM);
if (rc < 0)
return EAI_MEMORY;
num_found += rc;
}
}
if (hints->ai_socktype == 0 || hints->ai_socktype == SOCK_DGRAM)
{
servent* sptr = getservbyname(serv, "udp");
if (sptr != 0)
{
int rc = gai_port(aihead, sptr->s_port, SOCK_DGRAM);
if (rc < 0)
return EAI_MEMORY;
num_found += rc;
}
}
}
if (num_found == 0)
{
if (hints->ai_socktype == 0)
{
// All calls to getservbyname() failed.
return EAI_NONAME;
}
else
{
// Service not supported for socket type.
return EAI_SERVICE;
}
}
return 0;
}