Qt 4.8
qnativesocketengine.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtNetwork module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 //#define QNATIVESOCKETENGINE_DEBUG
43 
103 #include <qsocketnotifier.h>
104 #include <qnetworkinterface.h>
105 
106 #include "qnativesocketengine_p.h"
107 #include <private/qthread_p.h>
108 #include <private/qobject_p.h>
109 
110 #if !defined(QT_NO_NETWORKPROXY)
111 # include "qnetworkproxy.h"
112 # include "qabstractsocket.h"
113 # include "qtcpserver.h"
114 #endif
115 
117 
118 //#define QNATIVESOCKETENGINE_DEBUG
119 
120 #define Q_VOID
121 
122 // Common constructs
123 #define Q_CHECK_VALID_SOCKETLAYER(function, returnValue) do { \
124  if (!isValid()) { \
125  qWarning(""#function" was called on an uninitialized socket device"); \
126  return returnValue; \
127  } } while (0)
128 #define Q_CHECK_INVALID_SOCKETLAYER(function, returnValue) do { \
129  if (isValid()) { \
130  qWarning(""#function" was called on an already initialized socket device"); \
131  return returnValue; \
132  } } while (0)
133 #define Q_CHECK_STATE(function, checkState, returnValue) do { \
134  if (d->socketState != (checkState)) { \
135  qWarning(""#function" was not called in "#checkState); \
136  return (returnValue); \
137  } } while (0)
138 #define Q_CHECK_NOT_STATE(function, checkState, returnValue) do { \
139  if (d->socketState == (checkState)) { \
140  qWarning(""#function" was called in "#checkState); \
141  return (returnValue); \
142  } } while (0)
143 #define Q_CHECK_STATES(function, state1, state2, returnValue) do { \
144  if (d->socketState != (state1) && d->socketState != (state2)) { \
145  qWarning(""#function" was called" \
146  " not in "#state1" or "#state2); \
147  return (returnValue); \
148  } } while (0)
149 #define Q_CHECK_TYPE(function, type, returnValue) do { \
150  if (d->socketType != (type)) { \
151  qWarning(#function" was called by a" \
152  " socket other than "#type""); \
153  return (returnValue); \
154  } } while (0)
155 #define Q_TR(a) QT_TRANSLATE_NOOP(QNativeSocketEngine, a)
156 
168  socketDescriptor(-1),
169  readNotifier(0),
170  writeNotifier(0),
171  exceptNotifier(0)
172 {
173 }
174 
182 {
183 }
184 
195 {
196  if (hasSetSocketError) {
197  // Only set socket errors once for one engine; expect the
198  // socket to recreate its engine after an error. Note: There's
199  // one exception: SocketError(11) bypasses this as it's purely
200  // a temporary internal error condition.
201  // Another exception is the way the waitFor*() functions set
202  // an error when a timeout occurs. After the call to setError()
203  // they reset the hasSetSocketError to false
204  return;
205  }
206  if (error != QAbstractSocket::SocketError(11))
207  hasSetSocketError = true;
208 
209  socketError = error;
210 
211  switch (errorString) {
213  socketErrorString = QNativeSocketEngine::tr("Unable to initialize non-blocking socket");
214  break;
216  socketErrorString = QNativeSocketEngine::tr("Unable to initialize broadcast socket");
217  break;
218  case NoIpV6ErrorString:
219  socketErrorString = QNativeSocketEngine::tr("Attempt to use IPv6 socket on a platform with no IPv6 support");
220  break;
222  socketErrorString = QNativeSocketEngine::tr("The remote host closed the connection");
223  break;
224  case TimeOutErrorString:
225  socketErrorString = QNativeSocketEngine::tr("Network operation timed out");
226  break;
227  case ResourceErrorString:
228  socketErrorString = QNativeSocketEngine::tr("Out of resources");
229  break;
231  socketErrorString = QNativeSocketEngine::tr("Unsupported socket operation");
232  break;
234  socketErrorString = QNativeSocketEngine::tr("Protocol type not supported");
235  break;
237  socketErrorString = QNativeSocketEngine::tr("Invalid socket descriptor");
238  break;
240  socketErrorString = QNativeSocketEngine::tr("Host unreachable");
241  break;
243  socketErrorString = QNativeSocketEngine::tr("Network unreachable");
244  break;
245  case AccessErrorString:
246  socketErrorString = QNativeSocketEngine::tr("Permission denied");
247  break;
249  socketErrorString = QNativeSocketEngine::tr("Connection timed out");
250  break;
252  socketErrorString = QNativeSocketEngine::tr("Connection refused");
253  break;
255  socketErrorString = QNativeSocketEngine::tr("The bound address is already in use");
256  break;
258  socketErrorString = QNativeSocketEngine::tr("The address is not available");
259  break;
261  socketErrorString = QNativeSocketEngine::tr("The address is protected");
262  break;
264  socketErrorString = QNativeSocketEngine::tr("Datagram was too large to send");
265  break;
267  socketErrorString = QNativeSocketEngine::tr("Unable to send a message");
268  break;
270  socketErrorString = QNativeSocketEngine::tr("Unable to receive a message");
271  break;
272  case WriteErrorString:
273  socketErrorString = QNativeSocketEngine::tr("Unable to write");
274  break;
275  case ReadErrorString:
276  socketErrorString = QNativeSocketEngine::tr("Network error");
277  break;
279  socketErrorString = QNativeSocketEngine::tr("Another socket is already listening on the same port");
280  break;
282  socketErrorString = QNativeSocketEngine::tr("Operation on non-socket");
283  break;
285  socketErrorString = QNativeSocketEngine::tr("The proxy type is invalid for this operation");
286  break;
288  socketErrorString = QNativeSocketEngine::tr("Unknown error");
289  break;
290  }
291 }
292 
294 {
295  if (address == QHostAddress::LocalHost || address == QHostAddress::LocalHostIPv6)
296  return true;
297 
298 #if !defined(QT_NO_NETWORKPROXY)
299  QObject *parent = q_func()->parent();
300  QNetworkProxy proxy;
301  if (QAbstractSocket *socket = qobject_cast<QAbstractSocket *>(parent)) {
302  proxy = socket->proxy();
303  } else if (QTcpServer *server = qobject_cast<QTcpServer *>(parent)) {
304  proxy = server->proxy();
305  } else {
306  // no parent -> no proxy
307  return true;
308  }
309 
310  if (proxy.type() == QNetworkProxy::DefaultProxy)
312 
313  if (proxy.type() != QNetworkProxy::DefaultProxy &&
314  proxy.type() != QNetworkProxy::NoProxy) {
315  // QNativeSocketEngine doesn't do proxies
318  return false;
319  }
320 #endif
321 
322  return true;
323 }
324 
332 {
333 }
334 
339 {
340  close();
341 }
342 
355 {
357  if (isValid())
358  close();
359 
360 #if defined(QT_NO_IPV6)
361  if (protocol == QAbstractSocket::IPv6Protocol) {
364  return false;
365  }
366 #endif
367 
368  // Create the socket
369  if (!d->createNewSocket(socketType, protocol)) {
370 #if defined (QNATIVESOCKETENGINE_DEBUG)
371  QString typeStr = QLatin1String("UnknownSocketType");
372  if (socketType == QAbstractSocket::TcpSocket) typeStr = QLatin1String("TcpSocket");
373  else if (socketType == QAbstractSocket::UdpSocket) typeStr = QLatin1String("UdpSocket");
374  QString protocolStr = QLatin1String("UnknownProtocol");
375  if (protocol == QAbstractSocket::IPv4Protocol) protocolStr = QLatin1String("IPv4Protocol");
376  else if (protocol == QAbstractSocket::IPv6Protocol) protocolStr = QLatin1String("IPv6Protocol");
377  qDebug("QNativeSocketEngine::initialize(type == %s, protocol == %s) failed: %s",
378  typeStr.toLatin1().constData(), protocolStr.toLatin1().constData(), d->socketErrorString.toLatin1().constData());
379 #endif
380  return false;
381  }
382 
383  // Make the socket nonblocking.
387  close();
388  return false;
389  }
390 
391  // Set the broadcasting flag if it's a UDP socket.
392  if (socketType == QAbstractSocket::UdpSocket
396  close();
397  return false;
398  }
399 
400 
401  // Make sure we receive out-of-band data
402  if (socketType == QAbstractSocket::TcpSocket
404  qWarning("QNativeSocketEngine::initialize unable to inline out-of-band data");
405  }
406 
407  // Before Qt 4.6, we always set the send and receive buffer size to 49152 as
408  // this was found to be an optimal value. However, modern OS
409  // all have some kind of auto tuning for this and we therefore don't set
410  // this explictly anymore.
411  // If it introduces any performance regressions for Qt 4.6.x (x > 0) then
412  // it will be put back in.
413  //
414  // You can use tests/manual/qhttpnetworkconnection to test HTTP download speed
415  // with this.
416  //
417  // pre-4.6:
418  // setReceiveBufferSize(49152);
419  // setSendBufferSize(49152);
420 
421  d->socketType = socketType;
422  d->socketProtocol = protocol;
423  return true;
424 }
425 
440 {
442 
443  if (isValid())
444  close();
445 
446  d->socketDescriptor = socketDescriptor;
447 
448  // determine socket type and protocol
449  if (!d->fetchConnectionParameters()) {
450 #if defined (QNATIVESOCKETENGINE_DEBUG)
451  qDebug("QNativeSocketEngine::initialize(socketDescriptor == %i) failed: %s",
452  socketDescriptor, d->socketErrorString.toLatin1().constData());
453 #endif
454  d->socketDescriptor = -1;
455  return false;
456  }
457 
458  if (d->socketType != QAbstractSocket::UnknownSocketType) {
459  // Make the socket nonblocking.
463  close();
464  return false;
465  }
466 
467  // Set the broadcasting flag if it's a UDP socket.
468  if (d->socketType == QAbstractSocket::UdpSocket
472  close();
473  return false;
474  }
475  }
476 
477  d->socketState = socketState;
478  return true;
479 }
480 
487 {
488  Q_D(const QNativeSocketEngine);
489  return d->socketDescriptor != -1;
490 }
491 
497 {
498  Q_D(const QNativeSocketEngine);
499  return d->socketDescriptor;
500 }
501 
525 {
528 
529 #if defined (QT_NO_IPV6)
530  if (address.protocol() == QAbstractSocket::IPv6Protocol) {
533  return false;
534  }
535 #endif
536  if (!d->checkProxy(address))
537  return false;
538 
541 
542  d->peerAddress = address;
543  d->peerPort = port;
544  bool connected = d->nativeConnect(address, port);
545  if (connected)
546  d->fetchConnectionParameters();
547 
548  return connected;
549 }
550 
556 {
559 
560  connectToHost(d->peerAddress, d->peerPort);
562  // we changed states
564  }
565 }
566 
576 {
577  Q_UNUSED(name);
578  Q_UNUSED(port);
582  return false;
583 }
584 
595 {
598 
599 #if defined (QT_NO_IPV6)
600  if (address.protocol() == QAbstractSocket::IPv6Protocol) {
603  return false;
604  }
605 #endif
606  if (!d->checkProxy(address))
607  return false;
608 
610 
611  if (!d->nativeBind(address, port))
612  return false;
613 
614  d->fetchConnectionParameters();
615  return true;
616 }
617 
634 {
639 
640  // We're using a backlog of 50. Most modern kernels support TCP
641  // syncookies by default, and if they do, the backlog is ignored.
642  // When there is no support for TCP syncookies, this value is
643  // fine.
644  return d->nativeListen(50);
645 }
646 
655 {
660 
661  return d->nativeAccept();
662 }
663 
664 #ifndef QT_NO_NETWORKINTERFACE
665 
670  const QNetworkInterface &iface)
671 {
676  return d->nativeJoinMulticastGroup(groupAddress, iface);
677 }
678 
683  const QNetworkInterface &iface)
684 {
689  return d->nativeLeaveMulticastGroup(groupAddress, iface);
690 }
691 
694 {
695  Q_D(const QNativeSocketEngine);
698  return d->nativeMulticastInterface();
699 }
700 
703 {
707  return d->nativeSetMulticastInterface(iface);
708 }
709 
710 #endif // QT_NO_NETWORKINTERFACE
711 
721 {
722  Q_D(const QNativeSocketEngine);
725 
726  return d->nativeBytesAvailable();
727 }
728 
735 {
736  Q_D(const QNativeSocketEngine);
740 
741  return d->nativeHasPendingDatagrams();
742 }
743 
751 {
752  Q_D(const QNativeSocketEngine);
755 
756  return d->nativePendingDatagramSize();
757 }
758 
775  quint16 *port)
776 {
780 
781  return d->nativeReceiveDatagram(data, maxSize, address, port);
782 }
783 
803  const QHostAddress &host, quint16 port)
804 {
808  return d->nativeSendDatagram(data, size, host, port);
809 }
810 
816 {
820  return d->nativeWrite(data, size);
821 }
822 
823 
825 {
826  return 0;
827 }
828 
834 {
838 
839  qint64 readBytes = d->nativeRead(data, maxSize);
840 
841  // Handle remote close
842  if (readBytes == 0 && d->socketType == QAbstractSocket::TcpSocket) {
845  close();
846  return -1;
847  } else if (readBytes == -1) {
848  if (!d->hasSetSocketError) {
849  d->hasSetSocketError = true;
850  d->socketError = QAbstractSocket::NetworkError;
851  d->socketErrorString = qt_error_string();
852  }
853  close();
854  return -1;
855  }
856  return readBytes;
857 }
858 
864 {
866  if (d->readNotifier)
867  d->readNotifier->setEnabled(false);
868  if (d->writeNotifier)
869  d->writeNotifier->setEnabled(false);
870  if (d->exceptNotifier)
871  d->exceptNotifier->setEnabled(false);
872 
873  if(d->socketDescriptor != -1) {
874  d->nativeClose();
875  d->socketDescriptor = -1;
876  }
877  d->socketState = QAbstractSocket::UnconnectedState;
878  d->hasSetSocketError = false;
879  d->localPort = 0;
880  d->localAddress.clear();
881  d->peerPort = 0;
882  d->peerAddress.clear();
883  if (d->readNotifier) {
884  qDeleteInEventHandler(d->readNotifier);
885  d->readNotifier = 0;
886  }
887  if (d->writeNotifier) {
888  qDeleteInEventHandler(d->writeNotifier);
889  d->writeNotifier = 0;
890  }
891  if (d->exceptNotifier) {
892  qDeleteInEventHandler(d->exceptNotifier);
893  d->exceptNotifier = 0;
894  }
895 }
896 
912 bool QNativeSocketEngine::waitForRead(int msecs, bool *timedOut)
913 {
914  Q_D(const QNativeSocketEngine);
918 
919  if (timedOut)
920  *timedOut = false;
921 
922  int ret = d->nativeSelect(msecs, true);
923  if (ret == 0) {
924  if (timedOut)
925  *timedOut = true;
928  d->hasSetSocketError = false; // A timeout error is temporary in waitFor functions
929  return false;
930  } else if (state() == QAbstractSocket::ConnectingState) {
931  connectToHost(d->peerAddress, d->peerPort);
932  }
933 
934  return ret > 0;
935 }
936 
952 bool QNativeSocketEngine::waitForWrite(int msecs, bool *timedOut)
953 {
958 
959  if (timedOut)
960  *timedOut = false;
961 
962  int ret = d->nativeSelect(msecs, false);
963  // On Windows, the socket is in connected state if a call to
964  // select(writable) is successful. In this case we should not
965  // issue a second call to WSAConnect()
966 #if defined (Q_WS_WIN)
967  if (ret > 0) {
969  d_func()->fetchConnectionParameters();
970  return true;
971  } else {
972  int value = 0;
973  int valueSize = sizeof(value);
974  if (::getsockopt(d->socketDescriptor, SOL_SOCKET, SO_ERROR, (char *) &value, &valueSize) == 0) {
975  if (value == WSAECONNREFUSED) {
977  d->socketState = QAbstractSocket::UnconnectedState;
978  return false;
979  } else if (value == WSAETIMEDOUT) {
981  d->socketState = QAbstractSocket::UnconnectedState;
982  return false;
983  } else if (value == WSAEHOSTUNREACH) {
985  d->socketState = QAbstractSocket::UnconnectedState;
986  return false;
987  }
988  }
989  }
990 #endif
991 
992  if (ret == 0) {
993  if (timedOut)
994  *timedOut = true;
997  d->hasSetSocketError = false; // A timeout error is temporary in waitFor functions
998  return false;
999  } else if (state() == QAbstractSocket::ConnectingState) {
1000  connectToHost(d->peerAddress, d->peerPort);
1001  }
1002 
1003  return ret > 0;
1004 }
1005 
1006 bool QNativeSocketEngine::waitForReadOrWrite(bool *readyToRead, bool *readyToWrite,
1007  bool checkRead, bool checkWrite,
1008  int msecs, bool *timedOut)
1009 {
1014 
1015  int ret = d->nativeSelect(msecs, checkRead, checkWrite, readyToRead, readyToWrite);
1016  // On Windows, the socket is in connected state if a call to
1017  // select(writable) is successful. In this case we should not
1018  // issue a second call to WSAConnect()
1019 #if defined (Q_WS_WIN)
1020  if (checkWrite && ((readyToWrite && *readyToWrite) || !readyToWrite) && ret > 0) {
1022  d_func()->fetchConnectionParameters();
1023  return true;
1024  } else {
1025  int value = 0;
1026  int valueSize = sizeof(value);
1027  if (::getsockopt(d->socketDescriptor, SOL_SOCKET, SO_ERROR, (char *) &value, &valueSize) == 0) {
1028  if (value == WSAECONNREFUSED) {
1030  d->socketState = QAbstractSocket::UnconnectedState;
1031  return false;
1032  } else if (value == WSAETIMEDOUT) {
1034  d->socketState = QAbstractSocket::UnconnectedState;
1035  return false;
1036  } else if (value == WSAEHOSTUNREACH) {
1038  d->socketState = QAbstractSocket::UnconnectedState;
1039  return false;
1040  }
1041  }
1042  }
1043 #endif
1044  if (ret == 0) {
1045  if (timedOut)
1046  *timedOut = true;
1049  d->hasSetSocketError = false; // A timeout error is temporary in waitFor functions
1050  return false;
1051  } else if (state() == QAbstractSocket::ConnectingState) {
1052  connectToHost(d->peerAddress, d->peerPort);
1053  }
1054 
1055  return ret > 0;
1056 }
1057 
1065 {
1068 }
1069 
1086 {
1089 }
1090 
1097 {
1100 }
1101 
1112 {
1115 }
1116 
1117 
1122 {
1124  return d->setOption(option, value);
1125 }
1126 
1131 {
1132  Q_D(const QNativeSocketEngine);
1133  return d->option(socketOption);
1134 }
1135 
1137 {
1138  Q_D(const QNativeSocketEngine);
1139  return d->readNotifier && d->readNotifier->isEnabled();
1140 }
1141 
1142 /*
1143  \internal
1144  \class QReadNotifier
1145  \brief The QReadNotifer class is used to improve performance.
1146 
1147  QReadNotifier is a private class used for performance reasons vs
1148  connecting to the QSocketNotifier activated() signal.
1149  */
1151 {
1152 public:
1154  : QSocketNotifier(fd, QSocketNotifier::Read, parent)
1155  { engine = parent; }
1156 
1157 protected:
1158  bool event(QEvent *);
1159 
1161 };
1162 
1164 {
1165  if (e->type() == QEvent::SockAct) {
1166  engine->readNotification();
1167  return true;
1168  }
1169  return QSocketNotifier::event(e);
1170 }
1171 
1172 /*
1173  \internal
1174  \class QWriteNotifier
1175  \brief The QWriteNotifer class is used to improve performance.
1176 
1177  QWriteNotifier is a private class used for performance reasons vs
1178  connecting to the QSocketNotifier activated() signal.
1179  */
1181 {
1182 public:
1184  : QSocketNotifier(fd, QSocketNotifier::Write, parent) { engine = parent; }
1185 
1186 protected:
1187  bool event(QEvent *);
1188 
1190 };
1191 
1193 {
1194  if (e->type() == QEvent::SockAct) {
1195  if (engine->state() == QAbstractSocket::ConnectingState)
1196  engine->connectionNotification();
1197  else
1198  engine->writeNotification();
1199  return true;
1200  }
1201  return QSocketNotifier::event(e);
1202 }
1203 
1205 {
1206 public:
1208  : QSocketNotifier(fd, QSocketNotifier::Exception, parent) { engine = parent; }
1209 
1210 protected:
1211  bool event(QEvent *);
1212 
1214 };
1215 
1217 {
1218  if (e->type() == QEvent::SockAct) {
1219  if (engine->state() == QAbstractSocket::ConnectingState)
1220  engine->connectionNotification();
1221  else
1222  engine->exceptionNotification();
1223  return true;
1224  }
1225  return QSocketNotifier::event(e);
1226 }
1227 
1229 {
1231  if (d->readNotifier) {
1232  d->readNotifier->setEnabled(enable);
1233  } else if (enable && d->threadData->eventDispatcher) {
1234  d->readNotifier = new QReadNotifier(d->socketDescriptor, this);
1235  d->readNotifier->setEnabled(true);
1236  }
1237 }
1238 
1240 {
1241  Q_D(const QNativeSocketEngine);
1242  return d->writeNotifier && d->writeNotifier->isEnabled();
1243 }
1244 
1246 {
1248  if (d->writeNotifier) {
1249  d->writeNotifier->setEnabled(enable);
1250  } else if (enable && d->threadData->eventDispatcher) {
1251  d->writeNotifier = new QWriteNotifier(d->socketDescriptor, this);
1252  d->writeNotifier->setEnabled(true);
1253  }
1254 }
1255 
1257 {
1258  Q_D(const QNativeSocketEngine);
1259  return d->exceptNotifier && d->exceptNotifier->isEnabled();
1260 }
1261 
1263 {
1265  if (d->exceptNotifier) {
1266  d->exceptNotifier->setEnabled(enable);
1267  } else if (enable && d->threadData->eventDispatcher) {
1268  d->exceptNotifier = new QExceptionNotifier(d->socketDescriptor, this);
1269  d->exceptNotifier->setEnabled(true);
1270  }
1271 }
1272 
QNetworkProxy::ProxyType type() const
Returns the proxy type for this instance.
double d
Definition: qnumeric_p.h:62
bool setMulticastInterface(const QNetworkInterface &iface)
QString qt_error_string(int errorCode)
Definition: qglobal.cpp:2600
QNativeSocketEngine(QObject *parent=0)
Constructs a QNativeSocketEngine.
qint64 read(char *data, qint64 maxlen)
Reads up to maxSize bytes into data from the socket.
qint64 bytesAvailable() const
Returns the number of bytes that are currently available for reading.
int socketDescriptor() const
Returns the native socket descriptor.
bool setOption(SocketOption option, int value)
Sets the option option to the value value.
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
bool listen()
Prepares a TCP server for accepting incoming connections.
SocketType
This enum describes the transport layer protocol.
#define error(msg)
bool waitForReadOrWrite(bool *readyToRead, bool *readyToWrite, bool checkRead, bool checkWrite, int msecs=30000, bool *timedOut=0)
bool hasPendingDatagrams() const
Returns true if there is at least one datagram pending.
void setReadNotificationEnabled(bool enable)
bool connectToHostByName(const QString &name, quint16 port)
Connects to the remote host name given by name on port port.
static QString tr(const char *sourceText, const char *comment=0, int n=-1)
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
void setState(QAbstractSocket::SocketState state)
NetworkLayerProtocol
This enum describes the network layer protocol values used in Qt.
The QString class provides a Unicode character string.
Definition: qstring.h:83
QReadNotifier(int fd, QNativeSocketEngine *parent)
QExceptionNotifier(int fd, QNativeSocketEngine *parent)
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
The QObject class is the base class of all Qt objects.
Definition: qobject.h:111
virtual bool event(QEvent *)
This virtual function receives events to an object and should return true if the event e was recogniz...
Definition: qobject.cpp:1200
#define Q_D(Class)
Definition: qglobal.h:2482
void setReceiveBufferSize(qint64 bufferSize)
Sets the size of the operating system receive buffer to size.
The QNativeSocketEngine class provides low level access to a socket.
bool waitForRead(int msecs=30000, bool *timedOut=0)
Waits for msecs milliseconds or until the socket is ready for reading.
bool isValid() const
Returns true if the socket is valid; otherwise returns false.
qint64 readDatagram(char *data, qint64 maxlen, QHostAddress *addr=0, quint16 *port=0)
Reads up to maxSize bytes of a datagram from the socket, stores it in data and returns the number of ...
QNativeSocketEngine * engine
The QSocketNotifier class provides support for monitoring activity on a file descriptor.
qint64 sendBufferSize() const
Returns the size of the operating system send buffer.
#define Q_CHECK_TYPE(function, type, returnValue)
Q_GUI_EXPORT QString errorString(EGLint code=eglGetError())
Definition: qegl.cpp:743
SocketState
This enum describes the different states in which a socket can be.
Q_CORE_EXPORT void qDebug(const char *,...)
bool connectToHost(const QHostAddress &address, quint16 port)
Connects to the IP address and port specified by address and port.
static QNetworkProxy applicationProxy()
Returns the application level network proxying.
The QNetworkProxy class provides a network layer proxy.
void setWriteNotificationEnabled(bool enable)
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
static QIntfbScreen * connected
void setSendBufferSize(qint64 bufferSize)
Sets the size of the operating system send buffer to size.
qint64 receiveBufferSize() const
Returns the size of the operating system&#39;s socket receive buffer.
qint64 writeDatagram(const char *data, qint64 len, const QHostAddress &addr, quint16 port)
Writes a UDP datagram of size size bytes to the socket from data to the address host on port port...
int option(SocketOption option) const
Returns the value of the option socketOption.
bool event(QEvent *)
This virtual function receives events to an object and should return true if the event e was recogniz...
const char * name
SocketError
This enum describes the socket errors that can occur.
unsigned short quint16
Definition: qglobal.h:936
Q_CORE_EXPORT void qWarning(const char *,...)
static const char * data(const QByteArray &arr)
bool joinMulticastGroup(const QHostAddress &groupAddress, const QNetworkInterface &iface)
QByteArray toLatin1() const Q_REQUIRED_RESULT
Returns a Latin-1 representation of the string as a QByteArray.
Definition: qstring.cpp:3993
QAbstractSocket::SocketError socketError
__int64 qint64
Definition: qglobal.h:942
~QNativeSocketEngine()
Destructs a QNativeSocketEngine.
#define Q_CHECK_NOT_STATE(function, checkState, returnValue)
#define Q_CHECK_VALID_SOCKETLAYER(function, returnValue)
const char * constData() const
Returns a pointer to the data stored in the byte array.
Definition: qbytearray.h:433
bool event(QEvent *)
This virtual function receives events to an object and should return true if the event e was recogniz...
bool bind(const QHostAddress &address, quint16 port)
Binds the socket to the address address and port port.
static QAuServer & server()
Definition: qsound.cpp:79
QWriteNotifier(int fd, QNativeSocketEngine *parent)
void connectionNotification()
If there&#39;s a connection activity on the socket, process it.
QNativeSocketEngine * engine
bool checkProxy(const QHostAddress &address)
QAbstractSocket::NetworkLayerProtocol protocol() const
Returns the network layer protocol of the host address.
void qDeleteInEventHandler(QObject *o)
Definition: qobject.cpp:4348
#define Q_VOID
QNetworkInterface multicastInterface() const
~QNativeSocketEnginePrivate()
Destructs the private class.
bool waitForWrite(int msecs=30000, bool *timedOut=0)
Waits for msecs milliseconds or until the socket is ready for writing.
QObject * parent() const
Returns a pointer to the parent object.
Definition: qobject.h:273
QAbstractSocket::NetworkLayerProtocol protocol() const
void setError(QAbstractSocket::SocketError error, ErrorString errorString) const
Sets the error and error string if not set already.
#define Q_CHECK_STATE(function, checkState, returnValue)
The QTcpServer class provides a TCP-based server.
Definition: qtcpserver.h:61
int accept()
Accepts a pending connection from the socket, which must be in ListeningState, and returns its socket...
void setExceptionNotificationEnabled(bool enable)
QObject * parent
Definition: qobject.h:92
bool isExceptionNotificationEnabled() const
bool event(QEvent *)
This virtual function receives events to an object and should return true if the event e was recogniz...
bool initialize(QAbstractSocket::SocketType type, QAbstractSocket::NetworkLayerProtocol protocol=QAbstractSocket::IPv4Protocol)
Initializes a QNativeSocketEngine by creating a new socket of type socketType and network layer proto...
The QNetworkInterface class provides a listing of the host&#39;s IP addresses and network interfaces...
bool event(QEvent *)
This virtual function receives events to an object and should return true if the event e was recogniz...
QNativeSocketEnginePrivate()
Constructs the private class and initializes all data members.
The QHostAddress class provides an IP address.
Definition: qhostaddress.h:70
QAbstractSocket::SocketState state() const
QAbstractSocket::SocketType socketType() const
The QEvent class is the base class of all event classes.
Definition: qcoreevent.h:56
Type type() const
Returns the event type.
Definition: qcoreevent.h:303
The QAbstractSocket class provides the base functionality common to all socket types.
#define Q_UNUSED(x)
Indicates to the compiler that the parameter with the specified name is not used in the body of a fun...
Definition: qglobal.h:1729
qint64 write(const char *data, qint64 len)
Writes a block of size bytes from data to the socket.
void close()
Closes the socket.
bool isReadNotificationEnabled() const
bool isWriteNotificationEnabled() const
bool leaveMulticastGroup(const QHostAddress &groupAddress, const QNetworkInterface &iface)
qint64 pendingDatagramSize() const
Returns the size of the pending datagram, or -1 if no datagram is pending.
#define Q_CHECK_STATES(function, state1, state2, returnValue)
QNativeSocketEngine * engine