📄 socketaddress.cpp
字号:
#include "stdafx.h"
#include "socketaddress.h"
namespace acl
{
///////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
// Function Name : ctor
//////////////////////////////////////////////////////////////////////////////
SocketAddress::SocketAddress()
: m_host("")
{
memset(&m_addr, 0, sizeof(sockaddr_in));
}
//////////////////////////////////////////////////////////////////////////////
// Function Name : ctor
// Description : create address with hostname(or ip) and port
//////////////////////////////////////////////////////////////////////////////
SocketAddress::SocketAddress(const char *host, u_short port)
{
create(host, port);
}
//////////////////////////////////////////////////////////////////////////////
// Function Name : copy ctor
//////////////////////////////////////////////////////////////////////////////
SocketAddress::SocketAddress(const SocketAddress &rht)
: m_host(rht.m_host)
{
memcpy(&m_addr, &rht.m_addr, sizeof(sockaddr_in));
}
//////////////////////////////////////////////////////////////////////////////
// Function Name : assign operator
//////////////////////////////////////////////////////////////////////////////
SocketAddress & SocketAddress::operator = (const SocketAddress &rht)
{
if(this != &rht)
{
m_host = rht.m_host;
memcpy(&m_addr, &rht.m_addr, sizeof(sockaddr_in));
}
return *this;
}
//////////////////////////////////////////////////////////////////////////////
// Function Name : create
// Description : create address with hostname(or ip) and port
//////////////////////////////////////////////////////////////////////////////
void SocketAddress::create(const char *host, u_short port)
{
if(host)
m_host = host;
else
m_host = "";
memset(&m_addr, 0, sizeof(sockaddr_in));
m_addr.sin_port = htons(port);
resolve();
}
//////////////////////////////////////////////////////////////////////////////
// Function Name : directCreate
// Description : create from sockaddr_in
//////////////////////////////////////////////////////////////////////////////
void SocketAddress::directCreate(sockaddr_in &addr)
{
//just copy the sockaddr_in structure
memcpy(&m_addr, &addr, sizeof(sockaddr_in));
//use the ip address as the host string
m_host = inet_ntoa(addr.sin_addr);
}
//////////////////////////////////////////////////////////////////////////////
// Function Name : resolve
// Description : try to resolve the m_host, if it's a hostname
// and set the address, if m_host is an ip address then use direct!
// if m_host is empty then address from INADDR_ANY
// otherwise
//////////////////////////////////////////////////////////////////////////////
bool SocketAddress::resolve()
{
m_addr.sin_family = AF_INET;
if(!m_host.empty())
{
m_addr.sin_addr.s_addr = inet_addr(getHost());
//not ip address but host name string
//try to resolve the host name's ip address
if(m_addr.sin_addr.s_addr == INADDR_NONE)
{
struct hostent *host = NULL;
host = gethostbyname(getHost());
if(host)
{
memcpy(&m_addr.sin_addr, host->h_addr_list[0], host->h_length);
}
else
{
return false;
}
}
}
else
{
m_addr.sin_addr.s_addr = htonl(INADDR_ANY);
}
return true;
}
//////////////////////////////////////////////////////////////////////////////
// Function Name : bindWith
// Description : bind the address with a socket
// Input Parameters : socket -- the socket
// Return Value : bind ok/fail
//////////////////////////////////////////////////////////////////////////////
bool SocketAddress::bindWith(SOCKET socket)
{
return ::bind(socket, (sockaddr *)&m_addr, sizeof(m_addr)) != SOCKET_ERROR;
}
//////////////////////////////////////////////////////////////////////////////
// Function Name : connectWith
// Description : connect a socket to the address
// Input Parameters : socket -- the socket
// Return Value : connect ok/fail
bool SocketAddress::connectWith(SOCKET socket)
{
return ::connect(socket, (sockaddr *)&m_addr, sizeof(m_addr)) != SOCKET_ERROR;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -