判断端口是否被占用
Bool PublicTools::IsPortUsed(UInt16 uPort)
{
Bool bRet = False;
struct sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_port = htons(uPort);
inet_pton(AF_INET, "0.0.0.0", &addr.sin_addr);
int fd = socket(AF_INET, SOCK_STREAM, 0);
if (bind(fd, (struct sockaddr *) (&addr), sizeof(sockaddr_in)) < 0)
{
WARN_LOG("SYS: port = " << uPort << " has been used.");
bRet = True;
}
close(fd);
return bRet;
}
域名解析
string PublicTools::ParseDomainName(const string &strDomain)
{
struct hostent h, *result;
char buf[4096];
int nError;
if (gethostbyname_r(strDomain.c_str(), &h, buf, 4096, &result, &nError) != 0)
{
return "";
}
return inet_ntoa(*((struct in_addr *)h.h_addr));
}