STTNet
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
Public Member Functions | List of all members
stt::network::TcpClient Class Reference

TCP client operation class. More...

#include <sttnet_English.h>

Inheritance diagram for stt::network::TcpClient:
stt::network::TcpFDHandler stt::network::HttpClient stt::network::WebSocketClient

Public Member Functions

 TcpClient (const bool &TLS=false, const char *ca="", const char *cert="", const char *key="", const char *passwd="")
 Constructor of TcpClient class. More...
 
bool connect (const std::string &ip, const int &port)
 Initiate a TCP connection to the server. More...
 
void resetCTX (const bool &TLS=false, const char *ca="", const char *cert="", const char *key="", const char *passwd="")
 Reset TLS encryption parameters for the first time or reconfigure. More...
 
bool close ()
 If the object has a socket connection, close and release the connection and socket, and create a new socket. More...
 
 ~TcpClient ()
 Destructor of TcpClient, closes and releases the socket and its connection. More...
 
std::string getServerIP ()
 Return the IP of the connected server. More...
 
int getServerPort ()
 Return the port of the connected client. More...
 
bool isConnect ()
 Return the connection status of the object. More...
 
- Public Member Functions inherited from stt::network::TcpFDHandler
void setFD (const int &fd, SSL *ssl, const bool &flag1=false, const bool &flag2=false, const int &sec=-1)
 Initialize the object with a socket. More...
 
int getFD ()
 Get the socket of this object. More...
 
SSL * getSSL ()
 Get the encrypted SSL handle of this object. More...
 
void close (const bool &cle=true)
 Close the object. More...
 
void blockSet (const int &sec=-1)
 Set the socket in the object to blocking mode. More...
 
void unblockSet ()
 Set the socket in the object to non-blocking mode. More...
 
bool multiUseSet ()
 Set the socket in the object to SO_REUSEADDR mode. More...
 
bool isConnect ()
 Determine if the object has a socket bound. More...
 
int sendData (const std::string &data, const bool &block=true)
 Send string data to the connected socket. More...
 
int sendData (const char *data, const uint64_t &length, const bool &block=true)
 Send a specified length of binary data to the connected socket. More...
 
int recvDataByLength (std::string &data, const uint64_t &length, const int &sec=2)
 Blocking receive of specified length of data from a connected socket into a string. More...
 
int recvDataByLength (char *data, const uint64_t &length, const int &sec=2)
 Blocking receive of specified length of data from a connected socket into a char* container. More...
 
int recvData (std::string &data, const uint64_t &length)
 Receive data once from a connected socket into a string container. More...
 
int recvData (char *data, const uint64_t &length)
 Receive data once from a connected socket into a char* container. More...
 

Additional Inherited Members

- Public Attributes inherited from stt::network::TcpFDHandler
bool flag3 = false
 if the connection lost while using sendData function with block=true,this flag will be set to true More...
 
- Protected Attributes inherited from stt::network::TcpFDHandler
int fd = -1
 
bool flag1 = false
 
bool flag2 = false
 
SSL * ssl = nullptr
 
int sec =-1
 

Detailed Description

TCP client operation class.

Note
Blocking mode by default

Constructor & Destructor Documentation

stt::network::TcpClient::TcpClient ( const bool &  TLS = false,
const char *  ca = "",
const char *  cert = "",
const char *  key = "",
const char *  passwd = "" 
)

Constructor of TcpClient class.

Parameters
TLStrue: Enable TLS encryption, false: Do not enable TLS encryption (default is false)
caPath to CA root certificate (must be filled if TLS encryption is enabled, default is empty)
certPath to client certificate (optional, default is empty)
keyPath to client private key (optional, default is empty)
passwdPassword for private key decryption (optional, default is empty)
Note
  • ca is used to verify whether the server's certificate is trustworthy (can use the operating system's built-in root certificate for verification)
  • If TLS encryption is enabled, ca is required, others are optional
  • If the server requires client authentication (two-way TLS/SSL), you need to provide a valid client certificate.
stt::network::TcpClient::~TcpClient ( )
inline

Destructor of TcpClient, closes and releases the socket and its connection.

Member Function Documentation

bool stt::network::TcpClient::close ( )

If the object has a socket connection, close and release the connection and socket, and create a new socket.

Returns
true: Closed successfully and new socket created successfully, false: Closed successfully but new socket creation failed
bool stt::network::TcpClient::connect ( const std::string &  ip,
const int &  port 
)

Initiate a TCP connection to the server.

Parameters
ipServer IP
portServer port
Returns
true: Connection successful, false: Connection failed
std::string stt::network::TcpClient::getServerIP ( )
inline

Return the IP of the connected server.

Returns
IP of the connected server
int stt::network::TcpClient::getServerPort ( )
inline

Return the port of the connected client.

Returns
Port of the connected server
bool stt::network::TcpClient::isConnect ( )
inline

Return the connection status of the object.

Returns
true: Connected, false: Not connected
void stt::network::TcpClient::resetCTX ( const bool &  TLS = false,
const char *  ca = "",
const char *  cert = "",
const char *  key = "",
const char *  passwd = "" 
)

Reset TLS encryption parameters for the first time or reconfigure.

Note
The set TLS parameters will accompany the entire lifecycle unless this function is called to reset
Parameters
TLStrue: Enable TLS encryption, false: Do not enable TLS encryption (default is false)
caPath to CA root certificate (must be filled if TLS encryption is enabled, default is empty)
certPath to client certificate (optional, default is empty)
keyPath to client private key (optional, default is empty)
passwdPassword for private key decryption (optional, default is empty)
Note
  • ca is used to verify whether the server's certificate is trustworthy
  • If TLS encryption is enabled, ca is required, others are optional
  • If the server requires client authentication (two-way TLS/SSL), you need to provide a valid client certificate.

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