109 #include "private/qobject_p.h" 121 #define Q_CHECK_SOCKETENGINE(returnValue) do { \ 122 if (!d->socketEngine) { \ 123 return returnValue; \ 146 #ifndef QT_NO_NETWORKPROXY 156 #ifndef QT_NO_NETWORKPROXY 179 #ifndef QT_NO_NETWORKPROXY 222 #if defined (QTCPSERVER_DEBUG) 223 qDebug(
"QTcpServerPrivate::_q_processIncomingConnection() too many connections");
231 if (descriptor == -1)
233 #if defined (QTCPSERVER_DEBUG) 234 qDebug(
"QTcpServerPrivate::_q_processIncomingConnection() accepted socket %i", descriptor);
236 q->incomingConnection(descriptor);
239 emit q->newConnection();
285 qWarning(
"QTcpServer::listen() called when already listening");
291 #ifdef QT_NO_NETWORKPROXY 297 delete d->socketEngine;
299 if (!
d->socketEngine) {
301 d->serverSocketErrorString =
tr(
"Operation on socket is not supported");
304 #ifndef QT_NO_BEARERMANAGEMENT 306 d->socketEngine->setProperty(
"_q_networksession",
property(
"_q_networksession"));
309 d->serverSocketError =
d->socketEngine->error();
310 d->serverSocketErrorString =
d->socketEngine->errorString();
314 #if defined(Q_OS_UNIX) 326 if (!
d->socketEngine->bind(address, port)) {
327 d->serverSocketError =
d->socketEngine->error();
328 d->serverSocketErrorString =
d->socketEngine->errorString();
332 if (!
d->socketEngine->listen()) {
333 d->serverSocketError =
d->socketEngine->error();
334 d->serverSocketErrorString =
d->socketEngine->errorString();
338 d->socketEngine->setReceiver(
d);
339 d->socketEngine->setReadNotificationEnabled(
true);
342 d->address =
d->socketEngine->localAddress();
343 d->port =
d->socketEngine->localPort();
345 #if defined (QTCPSERVER_DEBUG) 346 qDebug(
"QTcpServer::listen(%i, \"%s\") == true (listening on port %i)", port,
376 d->pendingConnections.clear();
378 if (
d->socketEngine) {
379 d->socketEngine->close();
381 d->socketEngine->deleteLater();
382 }
QT_CATCH(
const std::bad_alloc &) {
405 return d->socketEngine->socketDescriptor();
421 qWarning(
"QTcpServer::setSocketDescriptor() called when already listening");
426 delete d->socketEngine;
428 if (!
d->socketEngine) {
430 d->serverSocketErrorString =
tr(
"Operation on socket is not supported");
433 #ifndef QT_NO_BEARERMANAGEMENT 435 d->socketEngine->setProperty(
"_q_networksession",
property(
"_q_networksession"));
438 d->serverSocketError =
d->socketEngine->error();
439 d->serverSocketErrorString =
d->socketEngine->errorString();
440 #if defined (QTCPSERVER_DEBUG) 441 qDebug(
"QTcpServer::setSocketDescriptor(%i) failed (%s)", socketDescriptor,
442 d->serverSocketErrorString.toLatin1().constData());
447 d->socketEngine->setReceiver(
d);
448 d->socketEngine->setReadNotificationEnabled(
true);
450 d->state =
d->socketEngine->state();
451 d->address =
d->socketEngine->localAddress();
452 d->port =
d->socketEngine->localPort();
454 #if defined (QTCPSERVER_DEBUG) 455 qDebug(
"QTcpServer::setSocketDescriptor(%i) succeeded.", socketDescriptor);
470 return d->socketEngine->localPort();
483 return d->socketEngine->localAddress();
511 if (!
d->socketEngine->waitForRead(msec, timedOut)) {
512 d->serverSocketError =
d->socketEngine->error();
513 d->serverSocketErrorString =
d->socketEngine->errorString();
517 if (timedOut && *timedOut)
520 d->readNotification();
533 return !d_func()->pendingConnections.isEmpty();
557 if (
d->pendingConnections.isEmpty())
560 if (!
d->socketEngine->isReadNotificationEnabled())
561 d->socketEngine->setReadNotificationEnabled(
true);
563 return d->pendingConnections.takeFirst();
591 #if defined (QTCPSERVER_DEBUG) 592 qDebug(
"QTcpServer::incomingConnection(%i)", socketDescriptor);
613 d_func()->pendingConnections.append(socket);
633 d_func()->maxConnections = numConnections;
644 return d_func()->maxConnections;
654 return d_func()->serverSocketError;
665 return d_func()->serverSocketErrorString;
668 #ifndef QT_NO_NETWORKPROXY 687 d->proxy = networkProxy;
706 #endif // QT_NO_NETWORKPROXY 710 #include "moc_qtcpserver.cpp" QNetworkProxy::ProxyType type() const
Returns the proxy type for this instance.
virtual bool isReadNotificationEnabled() const =0
QString serverSocketErrorString
#define QT_END_NAMESPACE
This macro expands to.
void setMaxPendingConnections(int numConnections)
Sets the maximum number of pending accepted connections to numConnections.
static QAbstractSocketEngine * createSocketEngine(QAbstractSocket::SocketType socketType, const QNetworkProxy &, QObject *parent)
bool listen(const QHostAddress &address=QHostAddress::Any, quint16 port=0)
Tells the server to listen for incoming connections on address address and port port.
static QString tr(const char *sourceText, const char *comment=0, int n=-1)
int count(const T &t) const
Returns the number of occurrences of value in the list.
NetworkLayerProtocol
This enum describes the network layer protocol values used in Qt.
QString errorString() const
Returns a human readable description of the last error that occurred.
The QString class provides a Unicode character string.
The QObject class is the base class of all Qt objects.
static QList< QNetworkProxy > proxyForQuery(const QNetworkProxyQuery &query)
This function takes the query request, query, examines the details of the type of socket or request a...
bool isListening() const
Returns true if the server is currently listening for incoming connections; otherwise returns false...
The QPointer class is a template class that provides guarded pointers to QObject. ...
QAbstractSocketEngine * socketEngine
virtual void incomingConnection(int handle)
This virtual function is called by QTcpServer when a new connection is available. ...
SocketState
This enum describes the different states in which a socket can be.
Q_CORE_EXPORT void qDebug(const char *,...)
The QNetworkProxy class provides a network layer proxy.
#define QT_BEGIN_NAMESPACE
This macro expands to.
virtual bool hasPendingConnections() const
Returns true if the server has a pending connection; otherwise returns false.
QAbstractSocket::SocketError serverSocketError
Capabilities capabilities() const
Returns the capabilities of this proxy server.
void connectionNotification()
virtual QTcpSocket * nextPendingConnection()
Returns the next pending connection as a connected QTcpSocket object.
bool setSocketDescriptor(int socketDescriptor, SocketState state=ConnectedState, OpenMode openMode=ReadWrite)
Initializes QAbstractSocket with the native socket descriptor socketDescriptor.
SocketError
This enum describes the socket errors that can occur.
Q_CORE_EXPORT void qWarning(const char *,...)
int maxPendingConnections() const
Returns the maximum number of pending accepted connections.
int socketDescriptor() const
Returns the native socket descriptor the server uses to listen for incoming instructions, or -1 if the server is not listening.
#define Q_CHECK_SOCKETENGINE(returnValue)
The QTcpSocket class provides a TCP socket.
QString toString() const
Returns the address as a string.
bool waitForNewConnection(int msec=0, bool *timedOut=0)
Waits for at most msec milliseconds or until an incoming connection is available. ...
QByteArray toLatin1() const Q_REQUIRED_RESULT
Returns a Latin-1 representation of the string as a QByteArray.
The QAuthenticator class provides an authentication object.
The QNetworkProxyQuery class is used to query the proxy settings for a socket.
void addPendingConnection(QTcpSocket *socket)
This function is called by QTcpServer::incomingConnection() to add the socket to the list of pending ...
void proxyAuthenticationRequired(const QNetworkProxy &, QAuthenticator *)
const char * constData() const
Returns a pointer to the data stored in the byte array.
#define Q_DECLARE_PUBLIC(Class)
QAbstractSocket::NetworkLayerProtocol protocol() const
Returns the network layer protocol of the host address.
virtual ~QTcpServer()
Destroys the QTcpServer object.
QAbstractSocket::SocketError serverError() const
Returns an error code for the last error that occurred.
bool setSocketDescriptor(int socketDescriptor)
Sets the socket descriptor this server should use when listening for incoming connections to socketDe...
void setProxy(const QNetworkProxy &networkProxy)
Sets the explicit network proxy for this socket to networkProxy.
The QTcpServer class provides a TCP-based server.
QAbstractSocket::SocketState state
QTcpServer(QObject *parent=0)
Constructs a QTcpServer object.
quint16 serverPort() const
Returns the server's port if the server is listening for connections; otherwise returns 0...
QNetworkProxy proxy() const
Returns the network proxy for this socket.
QVariant property(const char *name) const
Returns the value of the object's name property.
QHostAddress serverAddress() const
Returns the server's address if the server is listening for connections; otherwise returns QHostAddre...
void close()
Closes the server.
The QHostAddress class provides an IP address.
virtual void setReadNotificationEnabled(bool enable)=0
QNetworkProxy resolveProxy(const QHostAddress &address, quint16 port)
Resolve the proxy to its final value.
The QAbstractSocket class provides the base functionality common to all socket types.
QList< QTcpSocket * > pendingConnections
Q_OUTOFLINE_TEMPLATE void qDeleteAll(ForwardIterator begin, ForwardIterator end)
void exceptionNotification()
The QList class is a template class that provides lists.