RLib  5.7
RLib - an opensource, lightweight and multi-platform framework for cpp programming
System::Net::Sockets Class Reference

The namespace provides a managed implementation of the Windows Sockets (Winsock) interface for developers who need to tightly control access to the network. More...

Public Types

enum  SSLMode { SSL_NONE, SSL_CLIENT, SSL_SERVER }
 

Public Member Functions

 Sockets (RLIB_SOCKET s=RLIB_INVALID_SOCKET)
 
 Sockets (SSLMode ssl_mode, RLIB_SOCKET s=RLIB_INVALID_SOCKET)
 
 Sockets (int protocol, int type, int af=AF_INET)
 
 operator RLIB_SOCKET ()
 获取 SOCKET 句柄 More...
 
RLIB_SOCKET GetNativeHandle ()
 获取 SOCKET 句柄 More...
 
 RLIB_PROPERTY_GET (RLIB_SOCKET NativeHandle, GetNativeHandle)
 获取 SOCKET 句柄 More...
 
bool SetSendTimeout (int timeout)
 Sets the timeout, in milliseconds, for blocking send calls. More...
 
int GetSendBufferSize ()
 Get value of the SO_SNDBUF option for this Socket, that is the buffer size used by the platform for output on this Socket More...
 
bool SetReceiveTimeout (int timeout)
 Sets the timeout, in milliseconds, for blocking receive calls. More...
 
int GetReceiveBufferSize ()
 Gets the value of the SO_RCVBUF option for this Socket, that is the buffer size used by the platform for input on this Socket More...
 
bool Close ()
 Closes an existing socket More...
 
bool IsConnectable ()
 当 Connect 失败时,返回一个值指示能否继续尝试建立连接 More...
 
bool Connect (unsigned long addr, unsigned short port, const timeval *lptimeout=nullptr)
 Establishes a connection to a specified socket More...
 
bool Connect (LPCTSTR host, int host_len, unsigned short host_port, const timeval *lptimeout=nullptr)
 Establishes a connection to a specified socket More...
 
int Send (LPCVOID data, int size)
 Sends data on a connected socket More...
 
int SendTo (LPCVOID data, int size, unsigned long addr, unsigned short port, int flags=0)
 Sends data to a specific destination More...
 
int SendTo (LPCVOID data, int size, LPCTSTR host, int host_len, unsigned short host_port, int flags=0)
 Sends data to a specific destination More...
 
int Recv (LPVOID buf, int size)
 Receives data from a connected or bound socket More...
 
int RecvFrom (LPVOID buf, int size, sockaddr_in *addr=NULL, int *addrlen=nullptr, int flags=0)
 Receives a datagram and stores the source address More...
 
int Bind (ULONG addr, unsigned short port)
 associates a local address with a socket More...
 
int Bind (unsigned short host_port)
 associates a local address with a socket 方法绑定到所有地址上 More...
 
int Listen (int backlog=RLIB_SOMAXCONN)
 Places a socket a state where it is listening for an incoming connection More...
 
SocketsAccept (sockaddr_in *addr=NULL, int *addrlen=nullptr)
 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. More...
 
SocketsAccept (const timeval &_timeout, sockaddr_in *addr=NULL, int *addrlen=nullptr)
 An extended version of Accept 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. More...
 
int Shutdown (int how=RLIB_SD_BOTH)
 Disables sends or receives on a socket More...
 
ExceptionGetLastException ()
 获取 Wincock 发生的异常信息 该方法仅提供于实例对象 More...
 
void SaveLastException ()
 获取 Wincock 发生的异常信息, 随后可通过 GetLastException 方法访问 More...
 
int GetOption (_In_ int optname)
 Retrieves a socket option, or INT_MAX if failed More...
 
int GetOption (_In_ int optname, _Out_ void *optval, _Inout_ int *optlen)
 Retrieves a socket option More...
 
int SetOption (_In_ int optname, _In_ int val)
 Sets a socket option More...
 
int SetOption (_In_ int optname, _In_ const void *optval, _In_ int optlen)
 Sets a socket option More...
 

Static Public Member Functions

static Ipv4Host ParseIpv4Host (LPCTSTR host, int host_len, unsigned short host_port)
 Parses host and port into Ipv4Host structure More...
 
static int Select (_In_ int nfds, _Inout_opt_ fd_set FAR *readfds, _Inout_opt_ fd_set FAR *writefds, _Inout_opt_ fd_set FAR *exceptfds, _In_opt_ const struct timeval FAR *timeout)
 The function determines the status of one or more sockets, waiting if necessary, to perform synchronous I/O More...
 
static int IoctlSocket (_In_ RLIB_SOCKET s, _In_ long cmd, _Inout_ u_long FAR *argp)
 The function controls the I/O mode of a socket More...
 
static bool Disconnect (_In_ SOCKET s, _In_ LPOVERLAPPED lpOverlapped)
 The function closes a connection on a socket, and allows the socket handle to be reused. More...
 
static bool GetLocalHost (_Out_ char *host, _In_ int hostlen)
 获取本地计算机名称 More...
 
static bool GetLocalIpAddress (_In_ const TCHAR *host, _Out_ AddrInfo **addrinfo)
 获取本地计算机地址 More...
 
static void FreeLocalIpAddress (AddrInfo **addrinfo)
 释放由GetLocalIpAddress获得的addrinfo More...
 
static unsigned long Ipv4StringToAddress (LPCTSTR cp)
 Converts a string containing an (Ipv4) Internet Protocol dotted address into a proper address for the in_addr structure More...
 
static RLIB_DEPRECATED unsigned long Ipv4StringToAddress (LPCTSTR cp, int length)
 Converts a string containing an (Ipv4) Internet Protocol dotted address into a proper address for the in_addr structure More...
 
static unsigned long Sockets::HostStringToAddress (LPCTSTR host)
 Converts a string containing an host name into a proper address for the in_addr structure More...
 
static RLIB_DEPRECATED unsigned long Sockets::HostStringToAddress (LPCTSTR host, int host_len)
 Converts a string containing an host name into a proper address for the in_addr structure More...
 
static bool Ipv4AddressToString (unsigned long addr, LPTSTR pIpv4StringBuf, size_t ipv4StringBufSize)
 Converts an (IPv4) Internet network address into a string in Internet standard dotted format More...
 
static int InitializeNetworkEntry ()
 当首次使用 Sockets 的时候调用此方法初始化网络模块内存 More...
 
static void Dispose ()
 当不再使用 Sockets 的时候调用此方法释放网络模块内存 More...
 
static int GetLastErrorId ()
 获取最近一次错误Id More...
 

Public Attributes

 RLIB_DECLARE_DYNCREATE
 

Protected Attributes

RLIB_SOCKET m_socket
 
sockaddr_in m_addr
 
RLIB_SSL_CONTEXT m_ssl
 
Exception m_error
 

Detailed Description

The namespace provides a managed implementation of the Windows Sockets (Winsock) interface for developers who need to tightly control access to the network.

Member Function Documentation

Sockets * Sockets::Accept ( sockaddr_in *  addr = NULL,
int *  addrlen = nullptr 
)

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.

Sockets * Sockets::Accept ( const timeval &  _timeout,
sockaddr_in *  addr = NULL,
int *  addrlen = nullptr 
)

An extended version of Accept 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.

int System::Net::Sockets::Bind ( ULONG  addr,
unsigned short  port 
)

associates a local address with a socket

int System::Net::Sockets::Bind ( unsigned short  host_port)

associates a local address with a socket 方法绑定到所有地址上

bool Sockets::Close ( )

Closes an existing socket

bool Sockets::Connect ( unsigned long  addr,
unsigned short  port,
const timeval *  lptimeout = nullptr 
)

Establishes a connection to a specified socket

Parameters
portA 16-bit number in TCP/IP network byte order
bool Sockets::Connect ( LPCTSTR  host,
int  host_len,
unsigned short  host_port,
const timeval *  lptimeout = nullptr 
)

Establishes a connection to a specified socket

Parameters
host_portA 16-bit number in host byte order
bool Sockets::Disconnect ( _In_ SOCKET  s,
_In_ LPOVERLAPPED  lpOverlapped 
)
static

The function closes a connection on a socket, and allows the socket handle to be reused.

void Sockets::Dispose ( )
static

当不再使用 Sockets 的时候调用此方法释放网络模块内存

void Sockets::FreeLocalIpAddress ( AddrInfo **  addrinfo)
static

释放由GetLocalIpAddress获得的addrinfo

int Sockets::GetLastErrorId ( )
static

获取最近一次错误Id

Exception * Sockets::GetLastException ( )

获取 Wincock 发生的异常信息 该方法仅提供于实例对象

bool Sockets::GetLocalHost ( _Out_ char *  host,
_In_ int  hostlen 
)
static

获取本地计算机名称

bool Sockets::GetLocalIpAddress ( _In_ const TCHAR *  host,
_Out_ AddrInfo **  addrinfo 
)
static

获取本地计算机地址

RLIB_SOCKET Sockets::GetNativeHandle ( )

获取 SOCKET 句柄

int Sockets::GetOption ( _In_ int  optname)

Retrieves a socket option, or INT_MAX if failed

int Sockets::GetOption ( _In_ int  optname,
_Out_ void *  optval,
_Inout_ int *  optlen 
)

Retrieves a socket option

int Sockets::GetReceiveBufferSize ( )

Gets the value of the SO_RCVBUF option for this Socket, that is the buffer size used by the platform for input on this Socket

int Sockets::GetSendBufferSize ( )

Get value of the SO_SNDBUF option for this Socket, that is the buffer size used by the platform for output on this Socket

int Sockets::InitializeNetworkEntry ( )
static

当首次使用 Sockets 的时候调用此方法初始化网络模块内存

Returns
If successful, the function returns zero. Otherwise, it returns one of the error codes
int Sockets::IoctlSocket ( _In_ RLIB_SOCKET  s,
_In_ long  cmd,
_Inout_ u_long FAR *  argp 
)
static

The function controls the I/O mode of a socket

bool Sockets::Ipv4AddressToString ( unsigned long  addr,
LPTSTR  pIpv4StringBuf,
size_t  ipv4StringBufSize 
)
static

Converts an (IPv4) Internet network address into a string in Internet standard dotted format

Parameters
addrA pointer to an IN_ADDR structure with the IPv4 address to convert
pIpv4StringBufA pointer to a buffer in which to store the NULL-terminated string representation of the IP address. this buffer should be large enough to hold at least 16 characters
ipv4StringBufSizeOn input, the length, in characters, of the buffer pointed to by the pStringBuf parameter
unsigned long Sockets::Ipv4StringToAddress ( LPCTSTR  cp)
static

Converts a string containing an (Ipv4) Internet Protocol dotted address into a proper address for the in_addr structure

unsigned long Sockets::Ipv4StringToAddress ( LPCTSTR  cp,
int  length 
)
static

Converts a string containing an (Ipv4) Internet Protocol dotted address into a proper address for the in_addr structure

bool Sockets::IsConnectable ( )

当 Connect 失败时,返回一个值指示能否继续尝试建立连接

int Sockets::Listen ( int  backlog = RLIB_SOMAXCONN)

Places a socket a state where it is listening for an incoming connection

System::Net::Sockets::operator RLIB_SOCKET ( )

获取 SOCKET 句柄

Ipv4Host Sockets::ParseIpv4Host ( LPCTSTR  host,
int  host_len,
unsigned short  host_port 
)
static

Parses host and port into Ipv4Host structure

Parameters
host_portA 16-bit number in host byte order
int Sockets::Recv ( LPVOID  buf,
int  size 
)

Receives data from a connected or bound socket

int Sockets::RecvFrom ( LPVOID  buf,
int  size,
sockaddr_in *  addr = NULL,
int *  addrlen = nullptr,
int  flags = 0 
)

Receives a datagram and stores the source address

System::Net::Sockets::RLIB_PROPERTY_GET ( RLIB_SOCKET  NativeHandle,
GetNativeHandle   
)

获取 SOCKET 句柄

void Sockets::SaveLastException ( )

获取 Wincock 发生的异常信息, 随后可通过 GetLastException 方法访问

int Sockets::Select ( _In_ int  nfds,
_Inout_opt_ fd_set FAR *  readfds,
_Inout_opt_ fd_set FAR *  writefds,
_Inout_opt_ fd_set FAR *  exceptfds,
_In_opt_ const struct timeval FAR *  timeout 
)
static

The function determines the status of one or more sockets, waiting if necessary, to perform synchronous I/O

int Sockets::Send ( LPCVOID  data,
int  size 
)

Sends data on a connected socket

int Sockets::SendTo ( LPCVOID  data,
int  size,
unsigned long  addr,
unsigned short  port,
int  flags = 0 
)

Sends data to a specific destination

int Sockets::SendTo ( LPCVOID  data,
int  size,
LPCTSTR  host,
int  host_len,
unsigned short  host_port,
int  flags = 0 
)

Sends data to a specific destination

int Sockets::SetOption ( _In_ int  optname,
_In_ int  val 
)

Sets a socket option

int Sockets::SetOption ( _In_ int  optname,
_In_ const void *  optval,
_In_ int  optlen 
)

Sets a socket option

bool Sockets::SetReceiveTimeout ( int  timeout)

Sets the timeout, in milliseconds, for blocking receive calls.

bool Sockets::SetSendTimeout ( int  timeout)

Sets the timeout, in milliseconds, for blocking send calls.

int Sockets::Shutdown ( int  how = RLIB_SD_BOTH)

Disables sends or receives on a socket

static unsigned long System::Net::Sockets::Sockets::HostStringToAddress ( LPCTSTR  host)
static

Converts a string containing an host name into a proper address for the in_addr structure

Returns
INADDR_NONE if failed
static RLIB_DEPRECATED unsigned long System::Net::Sockets::Sockets::HostStringToAddress ( LPCTSTR  host,
int  host_len 
)
static

Converts a string containing an host name into a proper address for the in_addr structure

Returns
INADDR_NONE if failed

The documentation for this class was generated from the following files: