Game Development Reference
In-Depth Information
unsigned int BaseSocketManager::GetHostByName(const std::string &hostName)
{
struct hostent *pHostEnt = gethostbyname(hostName.c_str());
struct sockaddr_in tmpSockAddr; //placeholder for the ip address
if(pHostEnt == NULL)
{
GCC_ERROR(
Error occurred
);
return 0;
}
memcpy(&tmpSockAddr.sin_addr,pHostEnt->h_addr,pHostEnt->h_length);
return ntohl(tmpSockAddr.sin_addr.s_addr);
}
const char *BaseSocketManager::GetHostByAddr(unsigned int ip)
{
static char host[32];
int netip = htonl(ip);
struct hostent *lpHostEnt = gethostbyaddr((const char *)&netip, 4, PF_INET);
if (lpHostEnt)
{
strcpy(host, lpHostEnt->h_name);
return host;
}
return NULL;
}
The BaseSocketManager class is about 99 percent of what you need to create a
client-side socket manager or a server-side socket manager. Classes that inherit
from it can make it easy to create connections between clients and servers.
Core Client-Side Classes
An easy example of an extension of the BaseSocketManager class is a class to
manage the client side of a game. Its job is to create a single socket that attaches to
a known server.
class ClientSocketManager : public BaseSocketManager
{
std::string m_HostName;
unsigned int m_Port;
public:
ClientSocketManager(const std::string &hostName, unsigned int port)
 
Search WWH ::




Custom Search