本文整理汇总了C++中CAddress::ToSA方法的典型用法代码示例。如果您正苦于以下问题:C++ CAddress::ToSA方法的具体用法?C++ CAddress::ToSA怎么用?C++ CAddress::ToSA使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类CAddress
的用法示例。
在下文中一共展示了CAddress::ToSA方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: Bind
bool CUTPSocketListner::Bind(uint16 Port, const CAddress& IP)
{
ASSERT(m_Socket == INVALID_SOCKET);
sockaddr_in6 sa;
int sa_len = 0;
if(IP.AF() == AF_INET6)
{
m_bIPv6 = true;
sa_len = sizeof(sockaddr_in6);
if(!IP.IsNull())
IP.ToSA((sockaddr*)&sa, &sa_len, Port);
else
{
memset(&sa, 0, sa_len);
((sockaddr_in6*)&sa)->sin6_family = AF_INET6;
((sockaddr_in6*)&sa)->sin6_addr = in6addr_any;
((sockaddr_in6*)&sa)->sin6_port = htons((u_short)Port);
}
}
else
{
m_bIPv6 = false;
sa_len = sizeof(sockaddr_in);
if(!IP.IsNull())
IP.ToSA((sockaddr*)&sa, &sa_len, Port);
else
{
memset(&sa, 0, sizeof(sockaddr_in));
((sockaddr_in*)&sa)->sin_family = AF_INET;
((sockaddr_in*)&sa)->sin_addr.s_addr = INADDR_ANY;
((sockaddr_in*)&sa)->sin_port = htons((u_short)Port);
}
}
m_Socket = socket(IP.AF(), SOCK_DGRAM, IPPROTO_IP);
if (::bind(m_Socket, (sockaddr*)&sa, sa_len) < 0)
{
LogLine(LOG_ERROR, L"UTP - UDP port bind(%d) failed: %d %S\n", m_Port, errno, strerror(errno));
return false;
}
// Mark to hold a couple of megabytes
int iSize = 2 * 1024 * 1024;
if (setsockopt(m_Socket, SOL_SOCKET, SO_RCVBUF, (const char*)&iSize, sizeof(iSize)) < 0)
LogLine(LOG_ERROR, L"UTP - UDP setsockopt(SO_RCVBUF, %d) failed: %d %S\n", iSize, errno, strerror(errno));
if (setsockopt(m_Socket, SOL_SOCKET, SO_SNDBUF, (const char*)&iSize, sizeof(iSize)) < 0)
LogLine(LOG_ERROR, L"UTP - UDP setsockopt(SO_SNDBUF, %d) failed: %d %S\n", iSize, errno, strerror(errno));
// make socket non blocking
#ifdef _WIN32
u_long iMode = 1;
ioctlsocket(m_Socket, FIONBIO, &iMode);
#else
int iMode = fcntl(m_Socket, F_GETFL, 0);
fcntl(m_Socket, F_SETFL, iMode | O_NONBLOCK);
#endif
m_Port = Port;
LogLine(LOG_SUCCESS, L"%s Socket is listening at port %d", m_bIPv6 ? L"UTPv6" : L"UTP", m_Port);
return true;
}