Q3ServerSocket Class Reference

#include <q3serversocket.h>

Inheritance diagram for Q3ServerSocket:

Inheritance graph
[legend]
Collaboration diagram for Q3ServerSocket:

Collaboration graph
[legend]
List of all members.

Detailed Description

The Q3ServerSocket class provides a TCP-based server.

This class is a convenience class for accepting incoming TCP connections. You can specify the port or have Q3ServerSocket pick one, and listen on just one address or on all the machine's addresses.

Using the API is very simple: subclass Q3ServerSocket, call the constructor of your choice, and implement newConnection() to handle new incoming connections. There is nothing more to do.

(Note that due to lack of support in the underlying APIs, Q3ServerSocket cannot accept or reject connections conditionally.)

See also:
Q3Socket, Q3SocketDevice, QHostAddress, QSocketNotifier

Definition at line 37 of file q3serversocket.h.

Public Member Functions

 Q3ServerSocket (Q_UINT16 port, int backlog=1, QObject *parent=0, const char *name=0)
 Q3ServerSocket (const QHostAddress &address, Q_UINT16 port, int backlog=1, QObject *parent=0, const char *name=0)
 Q3ServerSocket (QObject *parent=0, const char *name=0)
virtual ~Q3ServerSocket ()
bool ok () const
Q_UINT16 port () const
int socket () const
virtual void setSocket (int socket)
QHostAddress address () const
virtual void newConnection (int socket)=0

Protected Member Functions

Q3SocketDevicesocketDevice ()

Private Slots

void incomingConnection (int socket)

Private Member Functions

void init (const QHostAddress &address, Q_UINT16 port, int backlog)

Private Attributes

Q3ServerSocketPrivated


Constructor & Destructor Documentation

Q3ServerSocket::Q3ServerSocket ( Q_UINT16  port,
int  backlog = 1,
QObject parent = 0,
const char *  name = 0 
)

Creates a server socket object, that will serve the given port on all the addresses of this host. If port is 0, Q3ServerSocket will pick a suitable port in a system-dependent manner. Use backlog to specify how many pending connections the server can have.

The parent and name arguments are passed on to the QObject constructor.

Warning:
On Tru64 Unix systems a value of 0 for backlog means that you don't accept any connections at all; you should specify a value larger than 0.

Definition at line 76 of file q3serversocket.cpp.

References d, and init().

00078     : QObject( parent, name )
00079 {
00080     d = new Q3ServerSocketPrivate;
00081     init( QHostAddress(), port, backlog );
00082 }

Here is the call graph for this function:

Q3ServerSocket::Q3ServerSocket ( const QHostAddress address,
Q_UINT16  port,
int  backlog = 1,
QObject parent = 0,
const char *  name = 0 
)

Creates a server socket object, that will serve the given port only on the given address. Use backlog to specify how many pending connections the server can have.

The parent and name arguments are passed on to the QObject constructor.

Warning:
On Tru64 Unix systems a value of 0 for backlog means that you don't accept any connections at all; you should specify a value larger than 0.

Definition at line 98 of file q3serversocket.cpp.

References address(), d, and init().

00101     : QObject( parent, name )
00102 {
00103     d = new Q3ServerSocketPrivate;
00104     init( address, port, backlog );
00105 }

Here is the call graph for this function:

Q3ServerSocket::Q3ServerSocket ( QObject parent = 0,
const char *  name = 0 
)

Construct an empty server socket.

This constructor, in combination with setSocket(), allows us to use the Q3ServerSocket class as a wrapper for other socket types (e.g. Unix Domain Sockets under Unix).

The parent and name arguments are passed on to the QObject constructor.

See also:
setSocket()

Definition at line 121 of file q3serversocket.cpp.

References d.

00122     : QObject( parent, name )
00123 {
00124     d = new Q3ServerSocketPrivate;
00125 }

Q3ServerSocket::~Q3ServerSocket (  )  [virtual]

Destroys the socket.

This causes any backlogged connections (connections that have reached the host, but not yet been completely set up by calling Q3SocketDevice::accept()) to be severed.

Existing connections continue to exist; this only affects the acceptance of new connections.

Definition at line 175 of file q3serversocket.cpp.

References d.

00176 {
00177     delete d;
00178 }


Member Function Documentation

bool Q3ServerSocket::ok (  )  const

Returns true if the construction succeeded; otherwise returns false.

Definition at line 131 of file q3serversocket.cpp.

References d, and Q3ServerSocketPrivate::s.

00132 {
00133     return !!d->s;
00134 }

Q_UINT16 Q3ServerSocket::port (  )  const

Returns the port number on which this server socket listens. This is always non-zero; if you specify 0 in the constructor, Q3ServerSocket will pick a non-zero port itself. ok() must be true before calling this function.

See also:
address() Q3SocketDevice::port()

Definition at line 206 of file q3serversocket.cpp.

References d, Q3SocketDevice::port(), and Q3ServerSocketPrivate::s.

00207 {
00208     if ( !d || !d->s )
00209   return 0;
00210     return d->s->port();
00211 }

Here is the call graph for this function:

int Q3ServerSocket::socket (  )  const

Returns the operating system socket.

Definition at line 217 of file q3serversocket.cpp.

References d, Q3ServerSocketPrivate::s, and Q3SocketDevice::socket().

00218 {
00219     if ( !d || !d->s )
00220   return -1;
00221 
00222     return d->s->socket();
00223 }

Here is the call graph for this function:

void Q3ServerSocket::setSocket ( int  socket  )  [virtual]

Sets the socket to use socket. bind() and listen() should already have been called for socket.

This allows us to use the Q3ServerSocket class as a wrapper for other socket types (e.g. Unix Domain Sockets).

Definition at line 265 of file q3serversocket.cpp.

References QObject::connect(), d, incomingConnection(), Q3ServerSocketPrivate::n, QSocketNotifier::Read, Q3ServerSocketPrivate::s, SIGNAL, SLOT, Q3SocketDevice::socket(), and Q3SocketDevice::Stream.

00266 {
00267     delete d;
00268     d = new Q3ServerSocketPrivate;
00269     d->s = new Q3SocketDevice( socket, Q3SocketDevice::Stream );
00270     d->n = new QSocketNotifier( d->s->socket(), QSocketNotifier::Read,
00271          this, "accepting new connections" );
00272     connect( d->n, SIGNAL(activated(int)),
00273        this, SLOT(incomingConnection(int)) );
00274 }

Here is the call graph for this function:

QHostAddress Q3ServerSocket::address (  )  const

Returns the address on which this object listens, or 0.0.0.0 if this object listens on more than one address. ok() must be true before calling this function.

See also:
port() Q3SocketDevice::address()

Definition at line 232 of file q3serversocket.cpp.

References Q3SocketDevice::address(), d, and Q3ServerSocketPrivate::s.

Referenced by init(), and Q3ServerSocket().

00233 {
00234     if ( !d || !d->s )
00235   return QHostAddress();
00236 
00237     return d->s->address();
00238 }

Here is the call graph for this function:

void Q3ServerSocket::newConnection ( int  socket  )  [pure virtual]

This pure virtual function is responsible for setting up a new incoming connection. socket is the fd (file descriptor) for the newly accepted connection.

Referenced by incomingConnection().

Q3SocketDevice * Q3ServerSocket::socketDevice (  )  [protected]

Returns a pointer to the internal socket device. The returned pointer is 0 if there is no connection or pending connection.

There is normally no need to manipulate the socket device directly since this class does all the necessary setup for most client or server socket applications.

Definition at line 249 of file q3serversocket.cpp.

References d, and Q3ServerSocketPrivate::s.

00250 {
00251     if ( !d )
00252   return 0;
00253 
00254     return d->s;
00255 }

void Q3ServerSocket::incomingConnection ( int  socket  )  [private, slot]

Definition at line 190 of file q3serversocket.cpp.

References Q3SocketDevice::accept(), d, newConnection(), and Q3ServerSocketPrivate::s.

Referenced by init(), and setSocket().

00191 {
00192     int fd = d->s->accept();
00193     if ( fd >= 0 )
00194   newConnection( fd );
00195 }

void Q3ServerSocket::init ( const QHostAddress address,
Q_UINT16  port,
int  backlog 
) [private]

Definition at line 139 of file q3serversocket.cpp.

References address(), Q3SocketDevice::bind(), QObject::connect(), d, incomingConnection(), Q3SocketDevice::IPv4, Q3SocketDevice::IPv6, Q3SocketDevice::listen(), Q3ServerSocketPrivate::n, qWarning(), QSocketNotifier::Read, Q3ServerSocketPrivate::s, Q3SocketDevice::setAddressReusable(), SIGNAL, SLOT, Q3SocketDevice::socket(), and Q3SocketDevice::Stream.

Referenced by Q3ServerSocket().

00140 {
00141     d->s = new Q3SocketDevice( Q3SocketDevice::Stream, address.isIPv4Address()
00142             ? Q3SocketDevice::IPv4 : Q3SocketDevice::IPv6, 0 );
00143 #if !defined(Q_OS_WIN32)
00144     // Under Unix, we want to be able to use the port, even if a socket on the
00145     // same address-port is in TIME_WAIT. Under Windows this is possible anyway
00146     // -- furthermore, the meaning of reusable is different: it means that you
00147     // can use the same address-port for multiple listening sockets.
00148     d->s->setAddressReusable( true );
00149 #endif
00150     if ( d->s->bind( address, port )
00151       && d->s->listen( backlog ) )
00152     {
00153   d->n = new QSocketNotifier( d->s->socket(), QSocketNotifier::Read,
00154             this, "accepting new connections" );
00155   connect( d->n, SIGNAL(activated(int)),
00156      this, SLOT(incomingConnection(int)) );
00157     } else {
00158   qWarning( "Q3ServerSocket: failed to bind or listen to the socket" );
00159   delete d->s;
00160   d->s = 0;
00161     }
00162 }

Here is the call graph for this function:


Member Data Documentation

Q3ServerSocketPrivate* Q3ServerSocket::d [private]

Definition at line 66 of file q3serversocket.h.

Referenced by address(), incomingConnection(), init(), ok(), port(), Q3ServerSocket(), setSocket(), socket(), socketDevice(), and ~Q3ServerSocket().


The documentation for this class was generated from the following files:
Generated on Thu Mar 15 16:16:38 2007 for Qt 4.2 User's Guide by  doxygen 1.5.1