• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • tdecore
 

tdecore

  • KNetwork
  • TDEServerSocket
Signals | Public Member Functions | Protected Member Functions | List of all members
KNetwork::TDEServerSocket Class Reference

#include <kserversocket.h>

Inheritance diagram for KNetwork::TDEServerSocket:
KNetwork::KPassiveSocketBase KNetwork::TDESocketBase

Signals

void gotError (int code)
 
void hostFound ()
 
void bound (const KResolverEntry &local)
 
void closed ()
 
void readyAccept ()
 

Public Member Functions

 TDEServerSocket (TQObject *parent=0L, const char *name=0L)
 
 TDEServerSocket (const TQString &service, TQObject *parent=0L, const char *name=0L)
 
 TDEServerSocket (const TQString &node, const TQString &service, TQObject *parent=0L, const char *name=0L)
 
 ~TDEServerSocket ()
 
KResolver & resolver () const
 
const KResolverResults & resolverResults () const
 
void setResolutionEnabled (bool enable)
 
void setFamily (int families)
 
void setAddress (const TQString &service)
 
void setAddress (const TQString &node, const TQString &service)
 
void setTimeout (int msecs)
 
virtual bool lookup ()
 
virtual bool bind (const TQString &node, const TQString &service)
 
virtual bool bind (const TQString &service)
 
virtual bool bind ()
 
virtual bool bind (const KResolverEntry &address)
 
virtual bool listen (int backlog=5)
 
virtual void close ()
 
void setAcceptBuffered (bool enable)
 
virtual KActiveSocketBase * accept ()
 
virtual TDESocketAddress localAddress () const
 
virtual TDESocketAddress externalAddress () const
 
- Public Member Functions inherited from KNetwork::KPassiveSocketBase
 KPassiveSocketBase ()
 
virtual ~KPassiveSocketBase ()
 
virtual bool bind (const KResolverEntry &address)=0
 
virtual bool listen (int backlog)=0
 
virtual void close ()=0
 
virtual KActiveSocketBase * accept ()=0
 
virtual TDESocketAddress localAddress () const =0
 
virtual TDESocketAddress externalAddress () const =0
 
- Public Member Functions inherited from KNetwork::TDESocketBase
 TDESocketBase ()
 
virtual ~TDESocketBase ()
 
virtual bool setBlocking (bool enable)
 
bool blocking () const
 
virtual bool setAddressReuseable (bool enable)
 
bool addressReuseable () const
 
virtual bool setIPv6Only (bool enable)
 
bool isIPv6Only () const
 
virtual bool setBroadcast (bool enable)
 
bool broadcast () const
 
TDESocketDevice * socketDevice () const
 
virtual void setSocketDevice (TDESocketDevice *device)
 
int setRequestedCapabilities (int add, int remove=0)
 
SocketError error () const
 
TQString errorString () const
 
TQMutex * mutex () const
 

Protected Member Functions

virtual bool setSocketOptions (int opts)
 
void copyError ()
 
- Protected Member Functions inherited from KNetwork::TDESocketBase
virtual bool setSocketOptions (int opts)
 
virtual int socketOptions () const
 
bool hasDevice () const
 
void setError (SocketError error)
 

Additional Inherited Members

- Public Types inherited from KNetwork::TDESocketBase
enum  SocketOptions {
  Blocking = 0x01 , AddressReuseable = 0x02 , IPv6Only = 0x04 , Keepalive = 0x08 ,
  Broadcast = 0x10
}
 
enum  SocketError {
  NoError = 0 , LookupFailure , AddressInUse , AlreadyCreated ,
  AlreadyBound , AlreadyConnected , NotConnected , NotBound ,
  NotCreated , WouldBlock , ConnectionRefused , ConnectionTimedOut ,
  InProgress , NetFailure , NotSupported , Timeout ,
  UnknownError , RemotelyDisconnected
}
 
- Static Public Member Functions inherited from KNetwork::TDESocketBase
static TQString errorString (SocketError code)
 
static bool isFatalError (int code)
 

Detailed Description

A server socket for accepting connections.

This class provides functionality for creating a socket to listen for incoming connections and subsequently accept them.

To use this class, you must first set the parameters for the listening socket's address, then place it in listening mode.

A typical example would look like:

TQString service = "http";
TDEServerSocket *ss = new TDEServerSocket(service);
connect(ss, TQ_SIGNAL(readyAccept()), this, TQ_SLOT(slotReadyAccept()));
connect(ss, TQ_SIGNAL(gotError(int)), this, TQ_SLOT(slotSocketError(int)));
ss->listen();
KNetwork::TDEServerSocket
A server socket for accepting connections.
Definition: kserversocket.h:108
KNetwork::TDEServerSocket::readyAccept
void readyAccept()
This signal is emitted whenever the socket is ready for accepting – i.e., there is at least one conne...
KNetwork::TDEServerSocket::gotError
void gotError(int code)
This signal is emitted when this object finds an error.
KNetwork::TDEServerSocket::listen
virtual bool listen(int backlog=5)
Puts this socket into listening mode.
Definition: kserversocket.cpp:232
KNetwork::TDEServerSocket::TDEServerSocket
TDEServerSocket(TQObject *parent=0L, const char *name=0L)
Default constructor.
Definition: kserversocket.cpp:61

In this case, this class will place the socket into listening mode on the service pointed to by service and will emit the readyAccept signal when a connection is ready for accepting. The called slot is responsible for calling accept.

The location of the services file (where service is looked up) is defined by _PATH_SERVICES in /usr/include/netdb.h. This is usually set to /etc/services. See RFC 1700 for more information on services. You can specify service as a port number directly, rather than as a service name. This is discouraged as it prevents the end user from easily modifying the port number.

For another example of usage, this below code attempts to make a connection on any port within a range:

TDEServerSocket *ss = new TDEServerSocket();
ss->setFamily(KResolver::InetFamily);
bool found = false;
for( unsigned int port = firstport; port <= lastport; ++port) {
ss->setAddress( TQString::number( port ) );
bool success = ss->listen();
if( found = ( success && ss->error() ==
TDESocketBase::NoError ) )
break;
ss->close();
}
if( !found ) {
// Couldn't connect to any port.
} else {
connect(ss, TQ_SIGNAL(readyAccept()), this, TQ_SLOT(slotReadyAccept()));
connect(ss, TQ_SIGNAL(gotError(int)), this, TQ_SLOT(slotSocketError(int)));
ss->listen();
}
KNetwork::TDEServerSocket::close
virtual void close()
Closes this socket.
Definition: kserversocket.cpp:269
KNetwork::TDEServerSocket::setFamily
void setFamily(int families)
Sets the allowed families for the resolutions.
Definition: kserversocket.cpp:118
KNetwork::TDEServerSocket::setAddress
void setAddress(const TQString &service)
Sets the address on which we will listen.
Definition: kserversocket.cpp:123
KNetwork::TDESocketBase::error
SocketError error() const
Retrieves the socket error code.
Definition: tdesocketbase.cpp:160

The called slot slotReadyAccept() is responsible for calling accept.

It is important to note that accept can return either an object of type KNetwork::KStreamSocket or KNetwork::TDEBufferedSocket (default). If you want to accept a non-buffered socket, you must first call setAcceptBuffered.

Warning
If you use TDEServerSocket in an auxiliary (non-GUI) thread, you need to accept only KNetwork::KStreamSocket objects.
See also
KNetwork::KStreamSocket, KNetwork::TDEBufferedSocket
Author
Thiago Macieira thiag.nosp@m.o@kd.nosp@m.e.org

Definition at line 107 of file kserversocket.h.

Constructor & Destructor Documentation

◆ TDEServerSocket() [1/3]

TDEServerSocket::TDEServerSocket ( TQObject *  parent = 0L,
const char *  name = 0L 
)

Default constructor.

If the binding address isn't changed by setAddress, this socket will bind to all interfaces on this node and the port will be selected by the operating system.

Parameters
parentthe parent TQObject object
namethe name of this object

Definition at line 61 of file kserversocket.cpp.

◆ TDEServerSocket() [2/3]

TDEServerSocket::TDEServerSocket ( const TQString &  service,
TQObject *  parent = 0L,
const char *  name = 0L 
)

Construct this object specifying the service to listen on.

If the binding address isn't changed by setAddress, this socket will bind to all interfaces and will listen on the port specified by service. This is either a service name (e.g. 'www') or a port number (e.g. '80').

The location of the services file (where service is looked up) is defined by _PATH_SERVICES in /usr/include/netdb.h. This is usually set to /etc/services. See RFC 1700 for more information on services.

Parameters
servicethe service name to listen on
parentthe parent TQObject object
namethe name of this object

Definition at line 68 of file kserversocket.cpp.

◆ TDEServerSocket() [3/3]

TDEServerSocket::TDEServerSocket ( const TQString &  node,
const TQString &  service,
TQObject *  parent = 0L,
const char *  name = 0L 
)

Construct this object specifying the node and service names to listen on.

If the binding address isn't changed by setAddress, this socket will bind to the interface specified by node and the port specified by service. This is either a service name (e.g. 'www') or a port number (e.g. '80').

The location of the services file (where service is looked up) is defined by _PATH_SERVICES in /usr/include/netdb.h. This is usually set to /etc/services. See RFC 1700 for more information on services.

Parameters
nodethe node to bind to
servicethe service port to listen on
parentthe parent TQObject object
namethe name of this object

Definition at line 76 of file kserversocket.cpp.

◆ ~TDEServerSocket()

TDEServerSocket::~TDEServerSocket ( )

Destructor.

This will close the socket, if open.

Note, however, that accepted sockets do not get closed when this object closes.

Definition at line 85 of file kserversocket.cpp.

Member Function Documentation

◆ accept()

KActiveSocketBase * TDEServerSocket::accept ( )
virtual

Accepts one incoming connection and return the associated, open socket.

If this function cannot accept a new connection, it will return NULL. The specific object class returned by this function may vary according to the implementation: derived classes may return specialised objects descended from KStreamSocket.

Note
This function should return a KStreamSocket object, but compiler deficiencies prevent such an adjustment. Therefore, we return the base class for active sockets, but it is guaranteed that the object will be a KStreamSocket or derived from it.
See also
TDEBufferedSocket
setAcceptBuffered

Implements KNetwork::KPassiveSocketBase.

Definition at line 283 of file kserversocket.cpp.

◆ bind() [1/4]

bool TDEServerSocket::bind ( )
virtual

Binds the socket to the addresses previously set with setAddress.

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 208 of file kserversocket.cpp.

◆ bind() [2/4]

bool TDEServerSocket::bind ( const KResolverEntry &  address)
virtual

Connect this socket to this specific address.

Reimplemented from TDESocketBase.

Unlike bind(const TQString&, const TQString&) above, this function really does bind the socket. No lookup is performed. The bound signal will be emitted.

Implements KNetwork::KPassiveSocketBase.

Definition at line 182 of file kserversocket.cpp.

◆ bind() [3/4]

bool TDEServerSocket::bind ( const TQString &  node,
const TQString &  service 
)
virtual

Binds this socket to the given nodename and service, or use the default ones if none are given.

Upon successful binding, the bound signal will be emitted. If an error is found, the gotError signal will be emitted.

This function returns true on success.

Parameters
nodethe nodename
servicethe service

Definition at line 196 of file kserversocket.cpp.

◆ bind() [4/4]

bool TDEServerSocket::bind ( const TQString &  service)
virtual

Binds the socket to the given service name.

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Parameters
servicethe service

Definition at line 202 of file kserversocket.cpp.

◆ bound

void KNetwork::TDEServerSocket::bound ( const KResolverEntry &  local)
signal

This signal is emitted when the socket successfully binds to an address.

Parameters
localthe local address we bound to

◆ close()

void TDEServerSocket::close ( )
virtual

Closes this socket.

Implements KNetwork::KPassiveSocketBase.

Definition at line 269 of file kserversocket.cpp.

◆ closed

void KNetwork::TDEServerSocket::closed ( )
signal

This signal is emitted when the socket completes the closing/shut down process.

◆ copyError()

void TDEServerSocket::copyError ( )
protected

Convenience function to set this object's error code to match that of the socket device.

Definition at line 370 of file kserversocket.cpp.

◆ externalAddress()

TDESocketAddress TDEServerSocket::externalAddress ( ) const
virtual

Returns this socket's externally-visible address if know.

Implements KNetwork::KPassiveSocketBase.

Definition at line 342 of file kserversocket.cpp.

◆ gotError

void KNetwork::TDEServerSocket::gotError ( int  code)
signal

This signal is emitted when this object finds an error.

The code parameter contains the error code that can also be found by calling error.

◆ hostFound

void KNetwork::TDEServerSocket::hostFound ( )
signal

This signal is emitted when the lookup is successfully completed.

◆ listen()

bool TDEServerSocket::listen ( int  backlog = 5)
virtual

Puts this socket into listening mode.

Reimplemented from KPassiveSocketBase.

Placing a socket into listening mode means it will be able to receive incoming connections through the accept method.

If you do not call this method but call accept directly, the socket will be placed into listening mode automatically.

Parameters
backlogthe number of connection the system is to queue without accept being called
Returns
true if the socket is now in listening mode.

Implements KNetwork::KPassiveSocketBase.

Definition at line 232 of file kserversocket.cpp.

◆ localAddress()

TDESocketAddress TDEServerSocket::localAddress ( ) const
virtual

Returns this socket's local address.

Implements KNetwork::KPassiveSocketBase.

Definition at line 337 of file kserversocket.cpp.

◆ lookup()

bool TDEServerSocket::lookup ( )
virtual

Starts the lookup for peer and local hostnames as well as their services.

If the blocking mode for this object is on, this function will wait for the lookup results to be available (by calling the KResolver::wait method on the resolver objects).

When the lookup is done, the signal hostFound will be emitted (only once, even if we're doing a double lookup). If the lookup failed (for any of the two lookups) the gotError signal will be emitted with the appropriate error condition (see TDESocketBase::SocketError).

This function returns true on success and false on error. Note that this is not the lookup result!

Definition at line 146 of file kserversocket.cpp.

◆ readyAccept

void KNetwork::TDEServerSocket::readyAccept ( )
signal

This signal is emitted whenever the socket is ready for accepting – i.e., there is at least one connection waiting to be accepted.

◆ resolver()

KResolver & TDEServerSocket::resolver ( ) const

Returns the internal KResolver object used for looking up the host name and service.

This can be used to set extra options to the lookup process other than the default values, as well as obtaining the error codes in case of lookup failure.

Definition at line 100 of file kserversocket.cpp.

◆ resolverResults()

const KResolverResults & TDEServerSocket::resolverResults ( ) const

Returns the internal list of resolved results for the binding address.

Definition at line 105 of file kserversocket.cpp.

◆ setAcceptBuffered()

void TDEServerSocket::setAcceptBuffered ( bool  enable)

Toggles whether the accepted socket will be buffered or not.

That is, the accept function will always return a KStreamSocket object or descended from it. If buffering is enabled, the class to be returned will be TDEBufferedSocket.

By default, this flag is set to true.

Parameters
enablewhether to set the accepted socket to buffered mode

Definition at line 278 of file kserversocket.cpp.

◆ setAddress() [1/2]

void TDEServerSocket::setAddress ( const TQString &  node,
const TQString &  service 
)

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. Sets the address on which we will listen.

This will cause the socket to listen only on the interface given by node and on the port given by service. service can either be a service name (e.g. 'www') or a port number (e.g. '80').

The location of the services file (where service is looked up) is defined by _PATH_SERVICES in /usr/include/netdb.h. This is usually set to /etc/services. See RFC 1700 for more information on services.

Parameters
nodethe node to bind to
servicethe service port to listen on

Definition at line 132 of file kserversocket.cpp.

◆ setAddress() [2/2]

void TDEServerSocket::setAddress ( const TQString &  service)

Sets the address on which we will listen.

The port to listen on is given by service, and we will bind to all interfaces. To let the operating system choose a port, set the service to "0". service can either be a service name (e.g. 'www') or a port number (e.g. '80').

The location of the services file (where service is looked up) is defined by _PATH_SERVICES in /usr/include/netdb.h. This is usually set to /etc/services. See RFC 1700 for more information on services.

Parameters
servicethe service name to listen on

Definition at line 123 of file kserversocket.cpp.

◆ setFamily()

void TDEServerSocket::setFamily ( int  families)

Sets the allowed families for the resolutions.

Parameters
familiesthe families that we want/accept
See also
KResolver::SocketFamilies for possible values

Definition at line 118 of file kserversocket.cpp.

◆ setResolutionEnabled()

void TDEServerSocket::setResolutionEnabled ( bool  enable)

Enables or disables name resolution.

If this flag is set to true, the bind operation will trigger name lookup operations (i.e., converting a hostname into its binary form). If the flag is set to false, those operations will instead try to convert a string representation of an address without attempting name resolution.

This is useful, for instance, when IP addresses are in their string representation (such as "1.2.3.4") or come from other sources like TDESocketAddress.

Parameters
enablewhether to enable

Definition at line 110 of file kserversocket.cpp.

◆ setSocketOptions()

bool TDEServerSocket::setSocketOptions ( int  opts)
protectedvirtual

Sets the socket options.

Reimplemented from TDESocketBase.

Reimplemented from KNetwork::TDESocketBase.

Definition at line 91 of file kserversocket.cpp.

◆ setTimeout()

void TDEServerSocket::setTimeout ( int  msecs)

Sets the timeout for accepting.

When you call accept, it will wait at most msecs milliseconds or return with an error (returning a NULL object).

Parameters
msecsthe time in milliseconds to wait, 0 to wait forever

Definition at line 141 of file kserversocket.cpp.


The documentation for this class was generated from the following files:
  • kserversocket.h
  • kserversocket.cpp

tdecore

Skip menu "tdecore"
  • Main Page
  • Modules
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

tdecore

Skip menu "tdecore"
  • arts
  • dcop
  • dnssd
  • interfaces
  •   kspeech
  •     interface
  •     library
  •   tdetexteditor
  • kate
  • kded
  • kdoctools
  • kimgio
  • kjs
  • libtdemid
  • libtdescreensaver
  • tdeabc
  • tdecmshell
  • tdecore
  • tdefx
  • tdehtml
  • tdeinit
  • tdeio
  •   bookmarks
  •   httpfilter
  •   kpasswdserver
  •   kssl
  •   tdefile
  •   tdeio
  •   tdeioexec
  • tdeioslave
  •   http
  • tdemdi
  •   tdemdi
  • tdenewstuff
  • tdeparts
  • tdeprint
  • tderandr
  • tderesources
  • tdespell2
  • tdesu
  • tdeui
  • tdeunittest
  • tdeutils
  • tdewallet
Generated for tdecore by doxygen 1.9.4
This website is maintained by Timothy Pearson.