您的位置:首页 > 编程语言 > C语言/C++

【RLIB】Socket类 的C++简易实现

2012-02-02 15:47 381 查看
相关源码参见RLIB开源项目

下面是RLIB1.x的头文件:

namespace System
{
	namespace Net
	{
		/// <summary>
		/// 提供对Winsock的标准访问
		/// </summary>
		class export Sockets
		{
		protected:
			SOCKET       m_socket;
			sockaddr_in  m_addr;
			Exception    *m_error;
		public:
			Sockets(SOCKET s = INVALID_SOCKET);
			~Sockets();
			RLIB_ClassNewDel;
		public:
			/// <summary>
			/// 方法允许您获取SOCKET以便进行更多的操作
			/// 您亦可继承并拓展Sockets类的功能
			/// </summary>
			operator SOCKET();
			/// <summary>
			/// 设置发送超时
			/// </summary>
			bool SetSndTimeO(int timeout);
			/// <summary>
			/// 获取发送缓冲区
			/// </summary>
			int GetSndBuf();
			/// <summary>
			/// 设置接收超时
			/// </summary>
			bool SetRcvTimeO(int timeout);
			/// <summary>
			/// 获取接收缓冲区
			/// </summary>
			int GetRcvBuf();
			/// <summary>
			/// Closes an existing socket
			/// </summary>
			bool Close();
			/// <summary>
			/// Establishes a connection to a specified socket
			/// </summary>
			bool Connect(char *host, unsigned int host_len, u_short port);
			/// <summary>
			/// Sends data on a connected socket
			/// 当发生错误时,size用来接收已发送长度
			/// </summary>
			bool Send(char *data, int *size);
			/// <summary>
			/// Receives data from a connected or bound socket
			/// 当发生错误时,size用来接收已接收长度
			/// </summary>
			int RecvEx(char *buf, int *size);  
			/// <summary>
			/// Receives data from a connected or bound socket
			/// 该方法不保证收完指定 size
			/// </summary>
			int Recv(char *buf, int size);  
			/// <summary>
			/// associates a local address with a socket
			/// </summary>
			int Bind(ULONG addr, u_short port);  
			/// <summary>
			/// associates a local address with a socket
			/// 方法绑定到所有地址上
			/// </summary>
			int Bind(u_short port);  
			/// <summary>
			/// Places a socket a state where it is listening for an incoming connection
			/// </summary>
			int Listen(int backlog = SOMAXCONN);  
			/// <summary>
			/// Permits an incoming connection attempt on a socket
			/// An optional pointer to a buffer that receives the address of the connecting entity,
			/// as known to the communications layer.
			/// </summary>
			Sockets *Accept(sockaddr_in *addr = NULL, int *addrlen = NULL);  
			/// <summary>
			/// Disables sends or receives on a socket
			/// </summary>
			int Shutdown(int how = SD_BOTH);
			/// <summary>
			/// 获取Wincock发生的异常信息
			/// 该方法仅提供于实例对象
			/// </summary>
			Exception *GetLastException(); 
			/// <summary>
			/// 获取一个值,该值指示是否与 Internet 已建立连接
			/// </summary>
			bool IsConnect; 
		public:
			/// <summary>
			/// 获取本地计算机名称
			/// </summary>
			static bool GetLocalHost(char *host, int hostlen);
			/// <summary>
			/// 获取本地计算机地址
			/// </summary>
			static bool GetLocalIpAddress(char *host, PADDRINFOA *addrinfo);
			/// <summary>
			/// 释放由GetLocalIpAddress获得的addrinfo
			/// </summary>
			static void FreeLocalIpAddress(PADDRINFOA *addrinfo);
			/// <summary>
			/// Converts a string containing an (Ipv4) Internet Protocol dotted address into a proper address for the in_addr structure
			/// </summary>
			static unsigned long Ipv4StringToAddress(const char *cp);
			/// <summary>
			/// Converts an (IPv4) Internet network address into a string in Internet standard dotted format
			/// </summary>
			static char *Ipv4AddressToString(in_addr in);
			/// <summary>
			/// 获取Wincock发生的错误说明
			/// </summary>
			static bool GetLastError(LPVOID buffer, size_t size, int err = 0);
			/// <summary>
			/// 当不再使用Winsock的时候调用此方法会释放网络模块内存
			/// </summary>
			static void Dispose();
		};
	}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: