// SockAddr.h // // Encapsulates the socket address structure // // Revision Log // // Date Who SAR Notes // ========== === ======= ===================================== // 2001-10-12 mph Initial coding by Mark Henri of // MPH Software - markhenri@attbi.com // // // #ifndef __SOCKADDR_H__ #define __SOCKADDR_H__ #include #include using namespace std; class CSockAddr : public sockaddr_in { public: // constructors CSockAddr() { sin_family = AF_INET; sin_port = 0; sin_addr.s_addr = INADDR_ANY; } // Default CSockAddr(const SOCKADDR& sa) { memcpy(this, &sa, sizeof(SOCKADDR)); } CSockAddr(const SOCKADDR_IN& sin) { memcpy(this, &sin, sizeof(SOCKADDR_IN)); } CSockAddr(const ULONG ulAddr, const USHORT ushPort = 0) // parms are host byte ordered { sin_family = AF_INET; sin_port = htons(ushPort); sin_addr.s_addr = htonl(ulAddr); } CSockAddr(const char* pchIP, const USHORT ushPort = 0) // dotted IP addr string { sin_family = AF_INET; sin_port = htons(ushPort); if (pchIP[0] != '\0') sin_addr.s_addr = inet_addr(pchIP); else sin_addr.s_addr = INADDR_ANY; } // already network byte ordered void SetPort(const USHORT ushPort=0) { sin_port = htons(ushPort); } void SetPort(servent& s) { sin_port = s.s_port; } void SetIPAddress(ULONG ulAddr) { sin_addr.s_addr = htonl(ulAddr); } void SetIPAddress(const char* pchIP) { if (pchIP[0] != '\0') sin_addr.s_addr = inet_addr(pchIP); else sin_addr.s_addr = INADDR_ANY; } // Return the address in dotted-decimal format const char* DottedDecimal() { _s = inet_ntoa(sin_addr); return _s.c_str(); } // Get port and address(even though they're public) USHORT Port() const { return ntohs(sin_port); } ULONG IPAddr() const { return ntohl(sin_addr.s_addr); } // operators added for efficiency const CSockAddr& operator=(const SOCKADDR& sa) { memcpy(this, &sa, sizeof(SOCKADDR)); return *this; } const CSockAddr& operator=(const SOCKADDR_IN& sin) { memcpy(this, &sin, sizeof(SOCKADDR_IN)); return *this; } operator SOCKADDR() { return *((LPSOCKADDR) this); } operator LPSOCKADDR() { return(LPSOCKADDR) this; } operator LPSOCKADDR_IN() { return(LPSOCKADDR_IN) this; } private: string _s; // temp variable }; #endif //__SOCKADDR_H__