SocketAddress类

2013年6月10日 由 Creater 留言 »

这个类实际上就是套接口地址结构的封装,正如手册所说:
This class and its subclasses is intended to be used as replacement
for the internal data type ‘ipaddr_t’ and various implementations of
IPv6 addressing found throughout the library.
‘ipaddr_t’ is an IPv4 address in network byte order.
‘port_t’ is the portnumber in host byte order.
‘struct in6_addr’ is an IPv6 address.
‘struct in_addr’ is an IPv4 address.
该类以及其子类用于替换套接字结构,由于该类是抽象基类,所以一般都是从该类进行派生。实际上有两种套接字结构,即是PV4,IPV6两种结构,对应于SocketAddress的派生类Ipv4Address与Ipv6Address。
代码中就是对套接字结构相关字段的设置,可以通过主机名或者ip地址获取套接字结构。

class SocketAddress
{
public:
	virtual ~SocketAddress() {}

	/** Get a pointer to the address struct. */
	virtual operator struct sockaddr *() = 0;

	/** Get length of address struct. */
	virtual operator socklen_t() = 0;

	/** Compare two addresses. */
	virtual bool operator==(SocketAddress&) = 0;

	/** Set port number.
		\param port Port number in host byte order */
	virtual void SetPort(port_t port) = 0;

	/** Get port number.
		\return Port number in host byte order. */
	virtual port_t GetPort() = 0;

	/** Set socket address.
		\param sa Pointer to either 'struct sockaddr_in' or 'struct sockaddr_in6'. */
	virtual void SetAddress(struct sockaddr *sa) = 0;

	/** Convert address to text. */
	virtual std::string Convert(bool include_port) = 0;

	/** Reverse lookup of address. */
	virtual std::string Reverse() = 0;

	/** Get address family. */
	virtual int GetFamily() = 0;

	/** Address structure is valid. */
	virtual bool IsValid() = 0;

	/** Get a copy of this SocketAddress object. */
	virtual std::auto_ptr<SocketAddress> GetCopy() = 0;
};
广告位

发表评论

你必须 登陆 方可发表评论.