【RLIB】Socket类 的C++简易实现
2012-02-02 15:47
381 查看
相关源码参见RLIB开源项目
下面是RLIB1.x的头文件:
下面是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(); }; } }
相关文章推荐
- 【RLIB】String 的简易C++实现
- 【RLIB】线程安全内存池(Memory) 的简易C++实现
- 【RLIB】内存流(Stream) 的简易C++实现
- 用c++实现一个简易的vector
- c++实现一个简易vector加强版
- 行编辑器的简易C++实现
- C++ 简易string类实现(二)-引用计数
- 计算器之C++简易实现
- C++小记:C++实现简易单链表模板类,重载输出流“<<”。
- C++(也许算是吧)+Linux 简易web服务器实现(with EPOLL&THREAD POLL)(1)
- C++ 简易string类实现(三)-抽离引用计数
- C++利用链表模板类实现一个简易队列
- C++ 简易string类实现(一)
- C++实现一个简易的内存池分配器
- C++实现简易通讯录系统
- C++: 实现一个简易的银行排号叫号系统
- C++ 引用计数 智能指针简易实现
- C++实现的简易链表
- C++实现简易表达式的计算(操作室为10以内)
- 用Win32 SDK & C++实现的一个简易版局域网聊天软件