PTLib
Version 2.10.10
|
A socket that uses the TCP transport on the Internet Protocol. More...
#include <tcpsock.h>
Public Member Functions | |
virtual PBoolean | Read (void *buf, PINDEX len) |
Low level read from the channel. More... | |
Construction. | |
PTCPSocket (WORD port=0) | |
Create a TCP/IP protocol socket channel. More... | |
PTCPSocket (const PString &service) | |
PTCPSocket (const PString &address, WORD port) | |
PTCPSocket (const PString &address, const PString &service) | |
PTCPSocket (PSocket &socket) | |
PTCPSocket (PTCPSocket &tcpSocket) | |
Overrides from class PObject. | |
virtual PObject * | Clone () const |
Create a copy of the class on the heap. More... | |
Overrides from class PChannel. | |
virtual PBoolean | Write (const void *buf, PINDEX len) |
Low level write to the channel. More... | |
Overrides from class PSocket. | |
virtual PBoolean | Listen (unsigned queueSize=5, WORD port=0, Reusability reuse=AddressIsExclusive) |
Listen on a socket for a remote host on the specified port number. More... | |
virtual PBoolean | Listen (const Address &bind, unsigned queueSize=5, WORD port=0, Reusability reuse=AddressIsExclusive) |
virtual PBoolean | Accept (PSocket &socket) |
Open a socket to a remote host on the specified port number. More... | |
New functions for class. | |
virtual PBoolean | WriteOutOfBand (const void *buf, PINDEX len) |
Write out of band data from the TCP/IP stream. More... | |
virtual void | OnOutOfBand (const void *buf, PINDEX len) |
This is callback function called by the system whenever out of band data from the TCP/IP stream is received. More... | |
![]() | |
virtual PBoolean | Connect (const PString &address) |
Connect a socket to a remote host on the specified port number. More... | |
virtual PBoolean | Connect (const Address &addr) |
virtual PBoolean | Connect (WORD localPort, const Address &addr) |
virtual PBoolean | Connect (const Address &iface, const Address &addr) |
virtual PBoolean | Connect (const Address &iface, WORD localPort, const Address &addr) |
virtual PString | GetName () const |
Get the platform and I/O channel type name of the channel. More... | |
virtual PString | GetLocalAddress () |
Get the Internet Protocol address and port for the local host. More... | |
virtual PBoolean | GetLocalAddress (Address &addr) |
virtual PBoolean | GetLocalAddress (PIPSocketAddressAndPort &addr) |
virtual PBoolean | GetLocalAddress (Address &addr, WORD &port) |
virtual PString | GetPeerAddress () |
Get the Internet Protocol address for the peer host and port the socket is connected to. More... | |
virtual PBoolean | GetPeerAddress (Address &addr) |
virtual PBoolean | GetPeerAddress (PIPSocketAddressAndPort &addr) |
virtual PBoolean | GetPeerAddress (Address &addr, WORD &port) |
PString | GetLocalHostName () |
Get the host name for the local host. More... | |
PString | GetPeerHostName () |
Get the host name for the peer host the socket is connected to. More... | |
PARRAY (RouteTable, RouteEntry) | |
PARRAY (InterfaceTable, InterfaceEntry) | |
PBoolean | SetCaptureReceiveToAddress () |
Set flag to capture interface address for incoming packets. More... | |
PIPSocket::Address | GetLastReceiveToAddress () const |
Return the interface address of the last incoming packet. More... | |
![]() | |
~PSocket () | |
PBoolean | SetOption (int option, int value, int level=SOL_SOCKET) |
Set options on the socket. More... | |
PBoolean | SetOption (int option, const void *valuePtr, PINDEX valueSize, int level=SOL_SOCKET) |
Set options on the socket. More... | |
PBoolean | GetOption (int option, int &value, int level=SOL_SOCKET) |
Get options on the socket. More... | |
PBoolean | GetOption (int option, void *valuePtr, PINDEX valueSize, int level=SOL_SOCKET) |
Get options on the socket. More... | |
virtual PBoolean | Shutdown (ShutdownValue option) |
Close one or both of the data streams associated with a socket. More... | |
virtual WORD | GetPortByService (const PString &service) const |
Get the port number for the specified service name. More... | |
virtual PString | GetServiceByPort (WORD port) const |
Get the service name from the port number. More... | |
void | SetPort (WORD port) |
Set the port number for the channel. More... | |
void | SetPort (const PString &service) |
Set the port number for the channel. More... | |
WORD | GetPort () const |
Get the port the TCP socket channel object instance is using. More... | |
PString | GetService () const |
Get a service name for the port number the TCP socket channel object instance is using. More... | |
![]() | |
PBoolean | SetErrorValues (Errors errorCode, int osError, ErrorGroup group=LastGeneralError) |
Set error values to those specified. More... | |
PChannel () | |
Create the channel. More... | |
~PChannel () | |
Close down the channel. More... | |
virtual Comparison | Compare (const PObject &obj) const |
Get the relative rank of the two strings. More... | |
virtual PINDEX | HashFunction () const |
Calculate a hash value for use in sets and dictionaries. More... | |
virtual PBoolean | IsOpen () const |
Determine if the channel is currently open. More... | |
int | GetHandle () const |
Get the integer operating system handle for the channel. More... | |
virtual PChannel * | GetBaseReadChannel () const |
Get the base channel of channel indirection using PIndirectChannel. More... | |
virtual PChannel * | GetBaseWriteChannel () const |
Get the base channel of channel indirection using PIndirectChannel. More... | |
void | SetReadTimeout (const PTimeInterval &time) |
Set the timeout for read operations. More... | |
PTimeInterval | GetReadTimeout () const |
Get the timeout for read operations. More... | |
virtual PINDEX | GetLastReadCount () const |
Get the number of bytes read by the last Read() call. More... | |
virtual int | ReadChar () |
Read a single 8 bit byte from the channel. More... | |
PBoolean | ReadBlock (void *buf, PINDEX len) |
Read len bytes into the buffer from the channel. More... | |
PString | ReadString (PINDEX len) |
Read len character into a string from the channel. More... | |
virtual PBoolean | ReadAsync (void *buf, PINDEX len) |
Begin an asynchronous read from channel. More... | |
virtual void | OnReadComplete (void *buf, PINDEX len) |
User callback function for when a ReadAsync() call has completed or timed out. More... | |
void | SetWriteTimeout (const PTimeInterval &time) |
Set the timeout for write operations to complete. More... | |
PTimeInterval | GetWriteTimeout () const |
Get the timeout for write operations to complete. More... | |
virtual PBoolean | Write (const void *buf, PINDEX len, const void *mark) |
Low level write to the channel with marker. More... | |
virtual PINDEX | GetLastWriteCount () const |
Get the number of bytes written by the last Write() call. More... | |
PBoolean | WriteChar (int c) |
Write a single character to the channel. More... | |
PBoolean | WriteString (const PString &str) |
Write a string to the channel. More... | |
virtual PBoolean | WriteAsync (const void *buf, PINDEX len) |
Begin an asynchronous write from channel. More... | |
virtual void | OnWriteComplete (const void *buf, PINDEX len) |
User callback function for when a WriteAsync() call has completed or timed out. More... | |
virtual PBoolean | Close () |
Close the channel, shutting down the link to the data source. More... | |
virtual bool | SetLocalEcho (bool localEcho) |
Set local echo mode. More... | |
virtual bool | FlowControl (const void *flowData) |
Flow Control information Pass data to the channel for flowControl determination. More... | |
PBoolean | SetBufferSize (PINDEX newSize) |
Set the iostream buffer size for reads and writes. More... | |
PBoolean | SendCommandString (const PString &command) |
Send a command meta-string. More... | |
void | AbortCommandString () |
Abort a command string that is in progress. More... | |
Errors | GetErrorCode (ErrorGroup group=NumErrorGroups) const |
Get normalised error code. More... | |
int | GetErrorNumber (ErrorGroup group=NumErrorGroups) const |
Get OS errro code. More... | |
virtual PString | GetErrorText (ErrorGroup group=NumErrorGroups) const |
Get error message description. More... | |
virtual PBoolean | Read (const VectorOfSlice &slices) |
Low level scattered read from the channel. More... | |
virtual PBoolean | Write (const VectorOfSlice &slices) |
Low level scattered write to the channel. More... | |
![]() | |
virtual | ~PObject () |
virtual const char * | GetClass (unsigned ancestor=0) const |
Get the current dynamic type of the object instance. More... | |
PBoolean | IsClass (const char *cls) const |
virtual PBoolean | InternalIsDescendant (const char *clsName) const |
Determine if the dynamic type of the current instance is a descendent of the specified class. More... | |
virtual Comparison | CompareObjectMemoryDirect (const PObject &obj) const |
Determine the byte wise comparison of two objects. More... | |
bool | operator== (const PObject &obj) const |
Compare the two objects. More... | |
bool | operator!= (const PObject &obj) const |
Compare the two objects. More... | |
bool | operator< (const PObject &obj) const |
Compare the two objects. More... | |
bool | operator> (const PObject &obj) const |
Compare the two objects. More... | |
bool | operator<= (const PObject &obj) const |
Compare the two objects. More... | |
bool | operator>= (const PObject &obj) const |
Compare the two objects. More... | |
virtual void | PrintOn (ostream &strm) const |
Output the contents of the object to the stream. More... | |
virtual void | ReadFrom (istream &strm) |
Input the contents of the object from the stream. More... | |
Protected Member Functions | |
virtual PBoolean | OpenSocket () |
virtual PBoolean | OpenSocket (int ipAdressFamily) |
Open an IPv4 or IPv6 socket. More... | |
virtual const char * | GetProtocolName () const |
This function returns the protocol name for the socket type. More... | |
![]() | |
PIPSocket () | |
Create a new Internet Protocol socket based on the port number specified. More... | |
![]() | |
PSocket () | |
int | os_close () |
int | os_socket (int af, int type, int proto) |
PBoolean | os_connect (struct sockaddr *sin, PINDEX size) |
PBoolean | os_recvfrom (void *buf, PINDEX len, int flags, struct sockaddr *from, PINDEX *fromlen) |
PBoolean | os_sendto (const void *buf, PINDEX len, int flags, struct sockaddr *to, PINDEX tolen) |
PBoolean | os_accept (PSocket &listener, struct sockaddr *addr, PINDEX *size) |
![]() | |
PChannel (const PChannel &) | |
PChannel & | operator= (const PChannel &) |
virtual PBoolean | ConvertOSError (int libcReturnValue, ErrorGroup group=LastGeneralError) |
Convert an operating system error into platform independent error. More... | |
int | ReadCharWithTimeout (PTimeInterval &timeout) |
Read a character with specified timeout. More... | |
PBoolean | ReceiveCommandString (int nextChar, const PString &reply, PINDEX &pos, PINDEX start) |
PBoolean | PXSetIOBlock (PXBlockType type, const PTimeInterval &timeout) |
int | PXClose () |
![]() | |
PObject () | |
Constructor for PObject, made protected so cannot ever create one on its own. More... | |
Additional Inherited Members | |
![]() | |
enum | Reusability { CanReuseAddress, AddressIsExclusive } |
Flags to reuse of port numbers in Listen() function. More... | |
![]() | |
enum | PXBlockType { PXReadBlock, PXWriteBlock, PXAcceptBlock, PXConnectBlock } |
enum | ShutdownValue { ShutdownRead = 0, ShutdownWrite = 1, ShutdownReadAndWrite = 2 } |
enum | Errors { NoError, NotFound, FileExists, DiskFull, AccessDenied, DeviceInUse, BadParameter, NoMemory, NotOpen, Timeout, Interrupted, BufferTooSmall, Miscellaneous, ProtocolFailure, NumNormalisedErrors } |
Normalised error codes. More... | |
enum | ErrorGroup { LastReadError, LastWriteError, LastGeneralError, NumErrorGroups } |
Error groups. More... | |
typedef std::vector< Slice > | VectorOfSlice |
![]() | |
enum | Comparison { LessThan = -1, EqualTo = 0, GreaterThan = 1 } |
Result of the comparison operation performed by the Compare() function. More... | |
![]() | |
static int | GetDefaultIpAddressFamily () |
Set the default IP address familly. More... | |
static void | SetDefaultIpAddressFamily (int ipAdressFamily) |
static void | SetDefaultIpAddressFamilyV4 () |
static PIPSocket::Address | GetDefaultIpAny () |
static void | SetSuppressCanonicalName (bool suppress) |
Set flag for suppress getting canonical name when doing lookup via hostname. More... | |
static bool | GetSuppressCanonicalName () |
Get flag for suppress getting canonical name when doing lookup via hostname. More... | |
static PString | GetHostName () |
Get the "official" host name for the host specified or if none, the host this process is running on. More... | |
static PString | GetHostName (const PString &hostname) |
static PString | GetHostName (const Address &addr) |
static PBoolean | GetHostAddress (Address &addr) |
Get the Internet Protocol address for the specified host, or if none specified, for the host this process is running on. More... | |
static PBoolean | GetHostAddress (const PString &hostname, Address &addr) |
static PStringArray | GetHostAliases (const PString &hostname) |
Get the alias host names for the specified host. More... | |
static PStringArray | GetHostAliases (const Address &addr) |
static PBoolean | IsLocalHost (const PString &hostname) |
Determine if the specified host is actually the local machine. More... | |
static void | ClearNameCache () |
Clear the name (DNS) cache. More... | |
static PBoolean | GetGatewayAddress (Address &addr, int version=4) |
Get the IP address that is being used as the gateway, that is, the computer that packets on the default route will be sent. More... | |
static PString | GetGatewayInterface (int version=4) |
Get the name for the interface that is being used as the gateway, that is, the interface that packets on the default route will be sent. More... | |
static PIPSocket::Address | GetRouteInterfaceAddress (PIPSocket::Address remoteAddress) |
Get the interface address that will be used to reach the specified remote address. More... | |
static PBoolean | GetRouteTable (RouteTable &table) |
Get the systems route table. More... | |
static RouteTableDetector * | CreateRouteTableDetector () |
Create an object that can wait for a change in the route table or active network interfaces. More... | |
static PBoolean | GetInterfaceTable (InterfaceTable &table, PBoolean includeDown=false) |
Get a list of all interfaces. More... | |
static PBoolean | GetNetworkInterface (PIPSocket::Address &addr) |
Get the address of an interface that corresponds to a real network. More... | |
![]() | |
static int | Select (PSocket &sock1, PSocket &sock2) |
Select a socket with available data. More... | |
static int | Select (PSocket &sock1, PSocket &sock2, const PTimeInterval &timeout) |
Select a socket with available data. More... | |
static Errors | Select (SelectList &read) |
Select a socket with available data. More... | |
static Errors | Select (SelectList &read, const PTimeInterval &timeout) |
Select a socket with available data. More... | |
static Errors | Select (SelectList &read, SelectList &write) |
Select a socket with available data. More... | |
static Errors | Select (SelectList &read, SelectList &write, const PTimeInterval &timeout) |
Select a socket with available data. More... | |
static Errors | Select (SelectList &read, SelectList &write, SelectList &except) |
Select a socket with available data. More... | |
static Errors | Select (SelectList &read, SelectList &write, SelectList &except, const PTimeInterval &timeout) |
Select a socket with available data. More... | |
static WORD | Host2Net (WORD v) |
Convert from host to network byte order. More... | |
static DWORD | Host2Net (DWORD v) |
Convert from host to network byte order. More... | |
static WORD | Net2Host (WORD v) |
Convert from network to host byte order. More... | |
static DWORD | Net2Host (DWORD v) |
Convert from network to host byte order. More... | |
static WORD | GetProtocolByName (const PString &name) |
Get the number of the protocol associated with the specified name. More... | |
static PString | GetNameByProtocol (WORD proto) |
Get the name of the protocol number specified. More... | |
static WORD | GetPortByService (const char *protocol, const PString &service) |
Get the port number for the specified service name. More... | |
static PString | GetServiceByPort (const char *protocol, WORD port) |
Get the service name from the port number. More... | |
![]() | |
static PBoolean | ConvertOSError (int libcReturnValue, Errors &lastError, int &osError) |
Convert an operating system error into platform independent error. More... | |
static PString | GetErrorText (Errors lastError, int osError=0) |
Get error message description. More... | |
![]() | |
static const char * | Class () |
Get the name of the class as a C string. More... | |
static Comparison | InternalCompareObjectMemoryDirect (const PObject *obj1, const PObject *obj2, PINDEX size) |
Internal function caled from CompareObjectMemoryDirect() More... | |
![]() | |
WORD | port |
Port to be used by the socket when opening the channel. More... | |
![]() | |
int | os_handle |
The operating system file handle return by standard open() function. More... | |
Errors | lastErrorCode [NumErrorGroups+1] |
The platform independant error code. More... | |
int | lastErrorNumber [NumErrorGroups+1] |
The operating system error number (eg as returned by errno). More... | |
PINDEX | lastReadCount |
Number of byte last read by the Read() function. More... | |
PINDEX | lastWriteCount |
Number of byte last written by the Write() function. More... | |
PTimeInterval | readTimeout |
Timeout for read operations. More... | |
PTimeInterval | writeTimeout |
Timeout for write operations. More... | |
PString | channelName |
PMutex | px_threadMutex |
PXBlockType | px_lastBlockType |
PThread * | px_readThread |
PThread * | px_writeThread |
PMutex | px_writeMutex |
PThread * | px_selectThread [3] |
PMutex | px_selectMutex [3] |
A socket that uses the TCP transport on the Internet Protocol.
PTCPSocket::PTCPSocket | ( | WORD | port = 0 | ) |
Create a TCP/IP protocol socket channel.
If a remote machine address or a "listening" socket is specified then the channel is also opened.
Note that what looks like a "copy" constructor here is really a the accept of a "listening" socket the same as the PSocket & parameter version constructor.
port | Port number to use for the connection. |
PTCPSocket::PTCPSocket | ( | const PString & | service | ) |
service | Service name to use for the connection. |
PTCPSocket::PTCPSocket | ( | const PString & | address, |
WORD | port | ||
) |
address | Address of remote machine to connect to. |
port | Port number to use for the connection. |
address | Address of remote machine to connect to. |
service | Service name to use for the connection. |
PTCPSocket::PTCPSocket | ( | PSocket & | socket | ) |
socket | Listening socket making the connection. |
PTCPSocket::PTCPSocket | ( | PTCPSocket & | tcpSocket | ) |
tcpSocket | Listening socket making the connection. |
Open a socket to a remote host on the specified port number.
This is an "accepting" socket. When a "listening" socket has a pending connection to make, this will accept a connection made by the "connecting" socket created to establish a link.
The port that the socket uses is the one used in the Listen()
command of the socket
parameter.
Note that this function will block until a remote system connects to the port number specified in the "listening" socket.
socket | Listening socket making the connection. |
Reimplemented from PSocket.
Reimplemented in PSocksSocket, and PTelnetSocket.
|
virtual |
Create a copy of the class on the heap.
The exact semantics of the descendent class determine what is required to make a duplicate of the instance. Not all classes can even {do} a clone operation.
The main user of the clone function is the PDictionary
class as it requires copies of the dictionary keys.
The default behaviour is for this function to assert.
Reimplemented from PObject.
Reimplemented in PSocks5Socket, and PSocks4Socket.
|
protectedvirtual |
This function returns the protocol name for the socket type.
Implements PSocket.
|
virtual |
Listen on a socket for a remote host on the specified port number.
This may be used for server based applications. A "connecting" socket begins a connection by initiating a connection to this socket. An active socket of this type is then used to generate other "accepting" sockets which establish a two way communications channel with the "connecting" socket.
The Listen() method is only used once in the entire lifetime of the socket. In contrast, the Accept() may be used many more times. Thus, if you have a socket to collect incoming connections, do a Listen() on it to get things ready, and then Accept(). With each incoming connection, you will do Accept again.
If the port
parameter is zero then the port number as defined by the object instance construction or the PIPSocket::SetPort()
function.
queueSize | Number of pending accepts that may be queued. |
port | Port number to use for the connection. |
reuse | Can/Can't listen more than once. |
Reimplemented from PIPSocket.
Reimplemented in PSocksSocket.
|
virtual |
bind | Local interface address to bind to. |
queueSize | Number of pending accepts that may be queued. |
port | Port number to use for the connection. |
reuse | Can/Can't listen more than once. |
Reimplemented from PIPSocket.
|
virtual |
This is callback function called by the system whenever out of band data from the TCP/IP stream is received.
A descendent class may interpret this data according to the semantics of the high level protocol.
The default behaviour is for the out of band data to be ignored.
buf | Data to be received as URGENT TCP data. |
len | Number of bytes pointed to by buf . |
Reimplemented in PTelnetSocket.
|
protectedvirtual |
Open an IPv4 or IPv6 socket.
Implements PIPSocket.
|
virtual |
Low level read from the channel.
This function may block until the requested number of characters were read or the read timeout was reached. The GetLastReadCount() function returns the actual number of bytes read.
The GetErrorCode() function should be consulted after Read() returns false to determine what caused the failure.
Reimplemented from PSocket.
Reimplemented in PTelnetSocket.
|
virtual |
Low level write to the channel.
This function will block until the requested number of characters are written or the write timeout is reached. The GetLastWriteCount() function returns the actual number of bytes written.
The GetErrorCode() function should be consulted after Write() returns false to determine what caused the failure.
This override repeatedly writes if there is no error until all of the requested bytes have been written.
buf | Pointer to a block of memory to write. |
len | Number of bytes to write. |
Reimplemented from PChannel.
Reimplemented in PTelnetSocket.
|
virtual |
Write out of band data from the TCP/IP stream.
This data is sent as TCP URGENT data which does not follow the usual stream sequencing of the normal channel data.
This is subject to the write timeout and sets the lastWriteCount
member variable in the same way as usual PChannel::Write()
function.
buf | Data to be written as URGENT TCP data. |
len | Number of bytes pointed to by buf . |