CSocketAddressIn Class


Click here to change the theme.

I have developed a class to help working with sockets addresses. It is derived from the sockaddr_in structure (see sockaddr). It is not complete but can be quite useful as it now exits. The majority of the class exists in the class definition, which is:

class CSocketAddressIn : protected sockaddr_in {
public:
	CSocketAddressIn()
		{ZeroMemory(this, sizeof sockaddr_in);sin_family = AF_INET;};
	CSocketAddressIn(u_long Address, u_short Port = 0);
	void SetAddress(u_long Address) {sin_addr.s_addr = htonl(Address);};
	void SetHostAddress(u_long Address) {sin_addr.s_addr = Address;};
	void GetAddress(string &s) {s=inet_ntoa(sin_addr);};
	void SetPort(u_short Port) {sin_port = htons(Port);};
	unsigned short GetPort() {return ntohs(sin_port);};
	operator sockaddr *() const {return (struct sockaddr *)this;}
};

The remainder of the class is implemented as follows:

CSocketAddressIn::CSocketAddressIn(u_long Address, u_short Port) {
sin_family = AF_INET;
sin_addr.s_addr = htonl(Address);
sin_port = htons(Port);
ZeroMemory(sin_zero, sizeof sin_zero);
}