src/network/socket/qabstractsocket.cpp
branchRCL_3
changeset 8 3f74d0d4af4c
parent 4 3b1da2848fc7
child 14 c0432d11811c
equal deleted inserted replaced
6:dee5afe5301f 8:3f74d0d4af4c
   153     However, in a GUI application, blocking sockets should only be
   153     However, in a GUI application, blocking sockets should only be
   154     used in non-GUI threads, to avoid freezing the user interface.
   154     used in non-GUI threads, to avoid freezing the user interface.
   155     See the \l network/fortuneclient and \l network/blockingfortuneclient
   155     See the \l network/fortuneclient and \l network/blockingfortuneclient
   156     examples for an overview of both approaches.
   156     examples for an overview of both approaches.
   157 
   157 
       
   158     \note We discourage the use of the blocking functions together
       
   159     with signals. One of the two possibilities should be used.
       
   160 
   158     QAbstractSocket can be used with QTextStream and QDataStream's
   161     QAbstractSocket can be used with QTextStream and QDataStream's
   159     stream operators (operator<<() and operator>>()). There is one
   162     stream operators (operator<<() and operator>>()). There is one
   160     issue to be aware of, though: You must make sure that enough data
   163     issue to be aware of, though: You must make sure that enough data
   161     is available before attempting to read it using operator>>().
   164     is available before attempting to read it using operator>>().
   162 
   165 
   167     \fn void QAbstractSocket::hostFound()
   170     \fn void QAbstractSocket::hostFound()
   168 
   171 
   169     This signal is emitted after connectToHost() has been called and
   172     This signal is emitted after connectToHost() has been called and
   170     the host lookup has succeeded.
   173     the host lookup has succeeded.
   171 
   174 
       
   175     \note Since Qt 4.6.3 QAbstractSocket may emit hostFound()
       
   176     directly from the connectToHost() call since a DNS result could have been
       
   177     cached.
       
   178 
   172     \sa connected()
   179     \sa connected()
   173 */
   180 */
   174 
   181 
   175 /*!
   182 /*!
   176     \fn void QAbstractSocket::connected()
   183     \fn void QAbstractSocket::connected()
   177 
   184 
   178     This signal is emitted after connectToHost() has been called and
   185     This signal is emitted after connectToHost() has been called and
   179     a connection has been successfully established.
   186     a connection has been successfully established.
       
   187 
       
   188     \note On some operating systems the connected() signal may
       
   189     be directly emitted from the connectToHost() call for connections
       
   190     to the localhost.
   180 
   191 
   181     \sa connectToHost(), disconnected()
   192     \sa connectToHost(), disconnected()
   182 */
   193 */
   183 
   194 
   184 /*!
   195 /*!
   349 */
   360 */
   350 
   361 
   351 #include "qabstractsocket.h"
   362 #include "qabstractsocket.h"
   352 #include "qabstractsocket_p.h"
   363 #include "qabstractsocket_p.h"
   353 
   364 
       
   365 #include "private/qhostinfo_p.h"
       
   366 
   354 #include <qabstracteventdispatcher.h>
   367 #include <qabstracteventdispatcher.h>
   355 #include <qdatetime.h>
   368 #include <qdatetime.h>
   356 #include <qhostaddress.h>
   369 #include <qhostaddress.h>
   357 #include <qhostinfo.h>
   370 #include <qhostinfo.h>
   358 #include <qmetaobject.h>
   371 #include <qmetaobject.h>
  1364         // the proxy supports connection by name, so use it
  1377         // the proxy supports connection by name, so use it
  1365         d->startConnectingByName(hostName);
  1378         d->startConnectingByName(hostName);
  1366         return;
  1379         return;
  1367 #endif
  1380 #endif
  1368     } else {
  1381     } else {
  1369         if (d->threadData->eventDispatcher)
  1382         if (d->threadData->eventDispatcher) {
  1370             d->hostLookupId = QHostInfo::lookupHost(hostName, this, SLOT(_q_startConnecting(QHostInfo)));
  1383             // this internal API for QHostInfo either immediatly gives us the desired
       
  1384             // QHostInfo from cache or later calls the _q_startConnecting slot.
       
  1385             bool immediateResultValid = false;
       
  1386             QHostInfo hostInfo = qt_qhostinfo_lookup(hostName,
       
  1387                                                      this,
       
  1388                                                      SLOT(_q_startConnecting(QHostInfo)),
       
  1389                                                      &immediateResultValid,
       
  1390                                                      &d->hostLookupId);
       
  1391             if (immediateResultValid) {
       
  1392                 d->hostLookupId = -1;
       
  1393                 d->_q_startConnecting(hostInfo);
       
  1394             }
       
  1395         }
  1371     }
  1396     }
  1372 
  1397 
  1373 #if defined(QABSTRACTSOCKET_DEBUG)
  1398 #if defined(QABSTRACTSOCKET_DEBUG)
  1374     qDebug("QAbstractSocket::connectToHost(\"%s\", %i) == %s%s", hostName.toLatin1().constData(), port,
  1399     qDebug("QAbstractSocket::connectToHost(\"%s\", %i) == %s%s", hostName.toLatin1().constData(), port,
  1375            (d->state == ConnectedState) ? "true" : "false",
  1400            (d->state == ConnectedState) ? "true" : "false",
  1680 
  1705 
  1681     \snippet doc/src/snippets/code/src_network_socket_qabstractsocket.cpp 0
  1706     \snippet doc/src/snippets/code/src_network_socket_qabstractsocket.cpp 0
  1682 
  1707 
  1683     If msecs is -1, this function will not time out.
  1708     If msecs is -1, this function will not time out.
  1684 
  1709 
  1685     Note: This function may wait slightly longer than \a msecs,
  1710     \note This function may wait slightly longer than \a msecs,
  1686     depending on the time it takes to complete the host lookup.
  1711     depending on the time it takes to complete the host lookup.
       
  1712 
       
  1713     \note Multiple calls to this functions do not accumulate the time.
       
  1714     If the function times out, the connecting process will be aborted.
  1687 
  1715 
  1688     \sa connectToHost(), connected()
  1716     \sa connectToHost(), connected()
  1689 */
  1717 */
  1690 bool QAbstractSocket::waitForConnected(int msecs)
  1718 bool QAbstractSocket::waitForConnected(int msecs)
  1691 {
  1719 {
  1720         QHostInfo::abortHostLookup(d->hostLookupId);
  1748         QHostInfo::abortHostLookup(d->hostLookupId);
  1721         d->hostLookupId = -1;
  1749         d->hostLookupId = -1;
  1722         d->_q_startConnecting(QHostInfo::fromName(d->hostName));
  1750         d->_q_startConnecting(QHostInfo::fromName(d->hostName));
  1723     }
  1751     }
  1724     if (state() == UnconnectedState)
  1752     if (state() == UnconnectedState)
  1725         return false;
  1753         return false; // connect not im progress anymore!
  1726 
  1754 
  1727     bool timedOut = true;
  1755     bool timedOut = true;
  1728 #if defined (QABSTRACTSOCKET_DEBUG)
  1756 #if defined (QABSTRACTSOCKET_DEBUG)
  1729     int attempt = 1;
  1757     int attempt = 1;
  1730 #endif
  1758 #endif