1
/****************************************************************************
3
** Copyright (C) 1992-2005 Trolltech AS. All rights reserved.
5
** This file is part of the Qt 3 compatibility classes of the Qt Toolkit.
7
** This file may be distributed under the terms of the Q Public License
8
** as defined by Trolltech AS of Norway and appearing in the file
9
** LICENSE.QPL included in the packaging of this file.
11
** This file may be distributed and/or modified under the terms of the
12
** GNU General Public License version 2 as published by the Free Software
13
** Foundation and appearing in the file LICENSE.GPL included in the
14
** packaging of this file.
16
** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
17
** information about Qt Commercial License Agreements.
18
** See http://www.trolltech.com/qpl/ for QPL licensing information.
19
** See http://www.trolltech.com/gpl/ for GPL licensing information.
21
** Contact info@trolltech.com if any conditions of this licensing are
24
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
25
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
27
****************************************************************************/
29
#include "q3serversocket.h"
33
#include "qsocketnotifier.h"
35
class Q3ServerSocketPrivate {
37
Q3ServerSocketPrivate(): s(0), n(0) {}
38
~Q3ServerSocketPrivate() { delete n; delete s; }
45
\class Q3ServerSocket qserversocket.h
46
\brief The Q3ServerSocket class provides a TCP-based server.
50
This class is a convenience class for accepting incoming TCP
51
connections. You can specify the port or have Q3ServerSocket pick
52
one, and listen on just one address or on all the machine's
55
Using the API is very simple: subclass Q3ServerSocket, call the
56
constructor of your choice, and implement newConnection() to
57
handle new incoming connections. There is nothing more to do.
59
(Note that due to lack of support in the underlying APIs,
60
Q3ServerSocket cannot accept or reject connections conditionally.)
62
\sa Q3Socket, Q3SocketDevice, QHostAddress, QSocketNotifier
67
Creates a server socket object, that will serve the given \a port
68
on all the addresses of this host. If \a port is 0, Q3ServerSocket
69
will pick a suitable port in a system-dependent manner. Use \a
70
backlog to specify how many pending connections the server can
73
The \a parent and \a name arguments are passed on to the QObject
76
\warning On Tru64 Unix systems a value of 0 for \a backlog means
77
that you don't accept any connections at all; you should specify a
81
Q3ServerSocket::Q3ServerSocket( Q_UINT16 port, int backlog,
82
QObject *parent, const char *name )
83
: QObject( parent, name )
85
d = new Q3ServerSocketPrivate;
86
init( QHostAddress(), port, backlog );
91
Creates a server socket object, that will serve the given \a port
92
only on the given \a address. Use \a backlog to specify how many
93
pending connections the server can have.
95
The \a parent and \a name arguments are passed on to the QObject
98
\warning On Tru64 Unix systems a value of 0 for \a backlog means
99
that you don't accept any connections at all; you should specify a
103
Q3ServerSocket::Q3ServerSocket( const QHostAddress & address, Q_UINT16 port,
105
QObject *parent, const char *name )
106
: QObject( parent, name )
108
d = new Q3ServerSocketPrivate;
109
init( address, port, backlog );
114
Construct an empty server socket.
116
This constructor, in combination with setSocket(), allows us to
117
use the Q3ServerSocket class as a wrapper for other socket types
118
(e.g. Unix Domain Sockets under Unix).
120
The \a parent and \a name arguments are passed on to the QObject
126
Q3ServerSocket::Q3ServerSocket( QObject *parent, const char *name )
127
: QObject( parent, name )
129
d = new Q3ServerSocketPrivate;
134
Returns true if the construction succeeded; otherwise returns false.
136
bool Q3ServerSocket::ok() const
142
The common bit of the constructors.
144
void Q3ServerSocket::init( const QHostAddress & address, Q_UINT16 port, int backlog )
146
d->s = new Q3SocketDevice( Q3SocketDevice::Stream, address.isIPv4Address()
147
? Q3SocketDevice::IPv4 : Q3SocketDevice::IPv6, 0 );
148
#if !defined(Q_OS_WIN32)
149
// Under Unix, we want to be able to use the port, even if a socket on the
150
// same address-port is in TIME_WAIT. Under Windows this is possible anyway
151
// -- furthermore, the meaning of reusable is different: it means that you
152
// can use the same address-port for multiple listening sockets.
153
d->s->setAddressReusable( true );
155
if ( d->s->bind( address, port )
156
&& d->s->listen( backlog ) )
158
d->n = new QSocketNotifier( d->s->socket(), QSocketNotifier::Read,
159
this, "accepting new connections" );
160
connect( d->n, SIGNAL(activated(int)),
161
this, SLOT(incomingConnection(int)) );
163
qWarning( "Q3ServerSocket: failed to bind or listen to the socket" );
173
This causes any backlogged connections (connections that have
174
reached the host, but not yet been completely set up by calling
175
Q3SocketDevice::accept()) to be severed.
177
Existing connections continue to exist; this only affects the
178
acceptance of new connections.
180
Q3ServerSocket::~Q3ServerSocket()
187
\fn void Q3ServerSocket::newConnection( int socket )
189
This pure virtual function is responsible for setting up a new
190
incoming connection. \a socket is the fd (file descriptor) for the
191
newly accepted connection.
195
void Q3ServerSocket::incomingConnection( int )
197
int fd = d->s->accept();
204
Returns the port number on which this server socket listens. This
205
is always non-zero; if you specify 0 in the constructor,
206
Q3ServerSocket will pick a non-zero port itself. ok() must be true
207
before calling this function.
209
\sa address() Q3SocketDevice::port()
211
Q_UINT16 Q3ServerSocket::port() const
220
Returns the operating system socket.
222
int Q3ServerSocket::socket() const
227
return d->s->socket();
231
Returns the address on which this object listens, or 0.0.0.0 if
232
this object listens on more than one address. ok() must be true
233
before calling this function.
235
\sa port() Q3SocketDevice::address()
237
QHostAddress Q3ServerSocket::address() const
240
return QHostAddress();
242
return d->s->address();
247
Returns a pointer to the internal socket device. The returned
248
pointer is 0 if there is no connection or pending connection.
250
There is normally no need to manipulate the socket device directly
251
since this class does all the necessary setup for most client or
252
server socket applications.
254
Q3SocketDevice *Q3ServerSocket::socketDevice()
264
Sets the socket to use \a socket. bind() and listen() should
265
already have been called for \a socket.
267
This allows us to use the Q3ServerSocket class as a wrapper for
268
other socket types (e.g. Unix Domain Sockets).
270
void Q3ServerSocket::setSocket( int socket )
273
d = new Q3ServerSocketPrivate;
274
d->s = new Q3SocketDevice( socket, Q3SocketDevice::Stream );
275
d->n = new QSocketNotifier( d->s->socket(), QSocketNotifier::Read,
276
this, "accepting new connections" );
277
connect( d->n, SIGNAL(activated(int)),
278
this, SLOT(incomingConnection(int)) );
281
#endif //QT_NO_NETWORK