本文整理汇总了C++中Address::c_str方法的典型用法代码示例。如果您正苦于以下问题:C++ Address::c_str方法的具体用法?C++ Address::c_str怎么用?C++ Address::c_str使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Address
的用法示例。
在下文中一共展示了Address::c_str方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: addressErrorToString
std::string ErrorReporter::addressErrorToString(const Address & address,
const std::string & errorString)
{
std::ostringstream out;
out << address.c_str() << ": " << errorString;
return out.str();
}
示例2: recreateListeningSocket
//-------------------------------------------------------------------------------------
bool NetworkInterface::recreateListeningSocket(const char* pEndPointName, uint16 listeningPort_min, uint16 listeningPort_max,
const char * listeningInterface, EndPoint* pEP, ListenerReceiver* pLR)
{
KBE_ASSERT(listeningInterface && pEP && pLR);
if (pEP->good())
{
this->dispatcher().deregisterFileDescriptor(*pEP);
pEP->close();
pEP->detach();
}
Address address;
address.ip = 0;
address.port = 0;
pEP->socket(SOCK_STREAM);
if (!pEP->good())
{
ERROR_MSG("NetworkInterface::recreateListeningSocket(%s): couldn't create a socket\n",
pEndPointName);
return false;
}
this->dispatcher().registerFileDescriptor(*pEP, pLR);
char ifname[IFNAMSIZ];
u_int32_t ifaddr = INADDR_ANY;
bool listeningInterfaceEmpty =
(listeningInterface == NULL || listeningInterface[0] == 0);
// Query kbemachined over the local interface (dev: lo) for what it
// believes the internal interface is.
if (listeningInterface &&
(strcmp(listeningInterface, USE_KBEMACHINED) == 0))
{
INFO_MSG( "NetworkInterface::recreateListeningSocket(%s): "
"Querying KBEMachined for interface\n", pEndPointName);
// 没有实现, 向KBEMachined查询接口
}
else if (pEP->findIndicatedInterface(listeningInterface, ifname) == 0)
{
INFO_MSG( "NetworkInterface::recreateListeningSocket(%s): "
"Creating on interface '%s' (= %s)\n",
pEndPointName, listeningInterface, ifname );
if (pEP->getInterfaceAddress( ifname, ifaddr ) != 0)
{
WARNING_MSG( "NetworkInterface::recreateListeningSocket(%s): "
"Couldn't get addr of interface %s so using all interfaces\n",
pEndPointName, ifname );
}
}
else if (!listeningInterfaceEmpty)
{
WARNING_MSG( "NetworkInterface::recreateListeningSocket(%s): "
"Couldn't parse interface spec '%s' so using all interfaces\n",
pEndPointName, listeningInterface );
}
bool foundport = false;
uint32 listeningPort = listeningPort_min;
if(listeningPort_min != listeningPort_max)
{
for(int lpIdx=ntohs(listeningPort_min); lpIdx<ntohs(listeningPort_max); lpIdx++)
{
listeningPort = htons(lpIdx);
if (pEP->bind(listeningPort, ifaddr) != 0)
{
continue;
}
else
{
foundport = true;
break;
}
}
}
else
{
if (pEP->bind(listeningPort, ifaddr) == 0)
{
foundport = true;
}
}
if(!foundport)
{
ERROR_MSG("NetworkInterface::recreateListeningSocket(%s): "
"Couldn't bind the socket to %s (%s)\n",
pEndPointName, address.c_str(), kbe_strerror());
pEP->close();
pEP->detach();
return false;
}
pEP->getlocaladdress( (u_int16_t*)&address.port,
(u_int32_t*)&address.ip );
//.........这里部分代码省略.........
示例3: recreateListeningSocket
//.........这里部分代码省略.........
if (pEP->bind(listeningPort, ifIPAddr) != 0)
{
continue;
}
else
{
foundport = true;
break;
}
}
}
else
{
if (pEP->bind(listeningPort, ifIPAddr) == 0)
{
foundport = true;
}
}
// 如果无法绑定到合适的端口那么报错返回,进程将退出
if(!foundport)
{
ERROR_MSG(fmt::format("NetworkInterface::recreateListeningSocket({}): "
"Couldn't bind the socket to {}:{} ({})\n",
pEndPointName, inet_ntoa((struct in_addr&)ifIPAddr), ntohs(listeningPort), kbe_strerror()));
pEP->close();
return false;
}
// 获得当前绑定的地址,如果是INADDR_ANY这里获得的IP是0
pEP->getlocaladdress( (u_int16_t*)&address.port,
(u_int32_t*)&address.ip );
if (0 == address.ip)
{
u_int32_t addr;
if(0 == pEP->getDefaultInterfaceAddress(addr))
{
address.ip = addr;
char szIp[MAX_IP] = {0};
Address::ip2string(address.ip, szIp);
INFO_MSG(fmt::format("NetworkInterface::recreateListeningSocket({}): "
"bound to all interfaces with default route "
"interface on {} ( {} )\n",
pEndPointName, szIp, address.c_str()));
}
else
{
ERROR_MSG(fmt::format("NetworkInterface::recreateListeningSocket({}): "
"Couldn't determine ip addr of default interface\n", pEndPointName));
pEP->close();
return false;
}
}
pEP->setnonblocking(true);
pEP->setnodelay(true);
pEP->addr(address);
if(rbuffer > 0)
{
if (!pEP->setBufferSize(SO_RCVBUF, rbuffer))
{
WARNING_MSG(fmt::format("NetworkInterface::recreateListeningSocket({}): "
"Operating with a receive buffer of only {} bytes (instead of {})\n",
pEndPointName, pEP->getBufferSize(SO_RCVBUF), rbuffer));
}
}
if(wbuffer > 0)
{
if (!pEP->setBufferSize(SO_SNDBUF, wbuffer))
{
WARNING_MSG(fmt::format("NetworkInterface::recreateListeningSocket({}): "
"Operating with a send buffer of only {} bytes (instead of {})\n",
pEndPointName, pEP->getBufferSize(SO_SNDBUF), wbuffer));
}
}
int backlog = Network::g_SOMAXCONN;
if(backlog < 5)
backlog = 5;
if(pEP->listen(backlog) == -1)
{
ERROR_MSG(fmt::format("NetworkInterface::recreateListeningSocket({}): "
"listen to {} ({})\n",
pEndPointName, address.c_str(), kbe_strerror()));
pEP->close();
return false;
}
INFO_MSG(fmt::format("NetworkInterface::recreateListeningSocket({}): address {}, SOMAXCONN={}.\n",
pEndPointName, address.c_str(), backlog));
return true;
}