This class represents an TCP/IP socket. It extends the basic semantics of sockets to allow non-blocking or time constrained operations.
If this callback is defined, it is invoked periodically while TcpSocket performs any operations, with the frequency 50Hz. This is intended to give user feedback in interactive applications.
static String GetHostName()
Returns the name of computer.
int GetDone() const
Returns number of bytes processed during current operation; intended to be called from WhenWait routine
bool IsOpen() const
Returns true if socket is open.
bool IsEof() const
Returns true if there are no more input data to process. Also returns true if socket is not open, if there was an error or if socket was aborted.
bool IsError() const
Returns true if some previous operations reported error. In that case, all subsequent request are ignored.
Clears the error state.
int GetError() const
Returns errorcode. Errorcodes are either defined by SOCKET API or it can be -1 for other errors.
String GetErrorDesc() const
Returns description of error.
Sets TcpSocket to aborted state. In aborted state, all subsequent request are ignored. Intended to be called from WhenWait routine.
bool IsAbort() const
Returns true is TcpSocket is in aborted state.
Clears the aborted state.
bool IsTimeout() const
Returns true if the last operation time-outed.
SOCKET GetSOCKET() const
Returns socket handle. Note that all TcpSocket sockets are non-blocking from host OS perspective.
String GetPeerAddr() const
Returns the peer address.
void Attach(SOCKET socket)
Attaches socket to TcpSocket. socket must be in non-blocking state.
bool Connect(const char *host, int port)
Connects socket to server at host:port. This operation is blocking with respect to resolving host name. Returns true when connection process is successfully started.
bool Connect(IpAddrInfo& info)
Connects socket to server found at info. Non-blocking.
After Connect returns true, WaitConnect waits for connection to be established. Note that it is only necessary to use WaitConnect if you want to intercept errors before sending/recieving data.
bool Listen(int port, int listen_count = 5, bool ipv6 = false, bool reuse = true, void* addr = NULL)
bool Listen(const IpAddrInfo& addr, int port, int listen_count = 5, bool ipv6 = false, bool reuse = true)
Starts a listening server socket at port with input queue listen_count. ipv6 sets the socket to IPv6 mode, reuse sets SO_REUSEADDR socket option. addr can be used to specify on what interface to listen on. As last void* parameter, it has to be pointer to uint32 that is dereferenced and assigned to sockaddr_in::sin_addr.s_addr for ipv6==true and/or in6_addr to be dereferenced and assigned to sockaddr_in6::sin6_addr for ipv6==true.
bool Accept(TcpSocket& listen_socket)
Accepts a connection from listen_socket.
Closes the socket.
Performs shutdown for write operations. Normally not needed.
Sets TCP_NODELAY option.
void Linger(int msecs)
Sets SO_LINGER option to msecs. If msecs is Null, switches SO_LINGER off.
Same as Linger(Null).
bool Wait(dword events)
Waits for at most timeout for events, which can be a combination of WAIT_READ (wait for more input bytes available), WAIT_WRITE (wait till it is possible to write something to socket). Wait also always returns when socket exception happens. Returns true if wait was successful (data can be written/read after the wait), false on timeout.
Same as Wait(WAIT_READ).
Same as Wait(WAIT_WRITE).
Returns the next input byte without actually removing it from input queue. It at most waits for specified timeout for it, if there is still none, returns -1.
Reads the next input byte. It at most waits for specified timeout for it, if there is still none, returns -1.
int Get(void *buffer, int len)
Reads at most len bytes into buffer, trying to do so at most for specified timeout. Returns the number of bytes actually read.
String Get(int len)
Reads at most len bytes, trying to do so at most for specified timeout. Returns a String with read data.
int Put(const void *s, int len)
Writes at most len bytes from buffer, trying to do so at most for specified timeout. Returns the number of bytes actually written.
int Put(const String& s)
Writes s, trying to do so at most for specified timeout. Returns the number of bytes actually written.
bool GetAll(void *buffer, int len)
Reads exactly len bytes into buffer. If such number of bytes cannot be read until timeout, returns false and sets timeout error for TcpSocket.
String GetAll(int len)
Reads exactly len bytes. If such number of bytes cannot be read until timeout, returns String::GetVoid() and sets timeout error for TcpSocket.
String GetLine(int maxlen = 65536)
Reads single line (ended with '\n', '\r' is ignored). If the whole line cannot be read within timeout or line length is longer than maxlen sets error and returns String::GetVoid().
bool PutAll(const void *s, int len)
Outputs exactly len bytes. If such number of bytes cannot be written in time specified by timeout, sets error and returns false.
bool PutAll(const String& s)
Outputs the whole String. If such number of bytes cannot be written in time specified by timeout, sets error and returns false.
Sets TcpSocket to SSL mode and starts SSL handshake. Core/SSL must be present in project. Returns true if SSL could have been started. Handshake is not finished until SSLHandshake returns false.
bool IsSSL() const
Returns true if TcpSocket is in SSL mode.
Attempts the progress on SSL handshake for at most timeout period. Returns true if SSL handshake is (still) in progress.
void SSLCertificate(const String& cert, const String& pkey, bool asn1)
Sets the SSL certificate. Must be called before StartSSL. (Note that clients usually do not need certificates, this is usually used on accepting sockets.)
void SSLServerNameIndication(const String& name)
Sets SNI for SSL connection.
const SSLInfo *GetSSLInfo() const
Returns information about established (after handshake) SSL connection or NULL if such information is not available.
TcpSocket& Timeout(int ms)
Sets timeout for all operations. Zero means that all operations return immediately (in that case it is usually a good idea to perform some sort of external blocking on socket or socket group using e.g. SocketWaitEvent). Null means operations are blocking (but they still can invoke WhenProgress periodically if defined). Other values specify a number of milliseconds. Note: It is possible to adjust timeout before any single TcpSocket operation. Returns *this.
int GetTimeout() const
Returns current timeout.
TcpSocket& GlobalTimeout(int ms)
Sets the "global timeout". This timeout is in effect over a whole range of operations, until it is canceled by calling this method with Null parameter or by setting a new global timeout. If global timeout is exceeded, operation during which it happened fails and socket error code is set to ERROR_GLOBAL_TIMEOUT.
Same as GlobalTimeout(Null).
Same as Timeout(Null). Returns *this.
TcpSocket& WaitStep(int ms)
Sets the periodicity of calling WhenWait in millisecond between calls. Default is 10ms (100hz).
int GetWaitStep() const
Retruns current periodicity of calling WhenWait.
This structure is used to pass information about established SSL connection.
Certificate is available.
Certificate is not valid before this date.
Certificate is not valid after this date.
Version of certificate.
Serial number of certificate.