Qt 4.8
Public Slots | Public Functions | List of all members
QNativeSocketEngine Class Reference

The QNativeSocketEngine class provides low level access to a socket. More...

#include <qnativesocketengine_p.h>

Inheritance diagram for QNativeSocketEngine:
QAbstractSocketEngine QObject

Public Slots

void connectionNotification ()
 If there's a connection activity on the socket, process it. More...
 
- Public Slots inherited from QAbstractSocketEngine
void connectionNotification ()
 
void exceptionNotification ()
 
void proxyAuthenticationRequired (const QNetworkProxy &proxy, QAuthenticator *authenticator)
 
void readNotification ()
 
void writeNotification ()
 
- Public Slots inherited from QObject
void deleteLater ()
 Schedules this object for deletion. More...
 

Public Functions

int accept ()
 Accepts a pending connection from the socket, which must be in ListeningState, and returns its socket descriptor. More...
 
bool bind (const QHostAddress &address, quint16 port)
 Binds the socket to the address address and port port. More...
 
qint64 bytesAvailable () const
 Returns the number of bytes that are currently available for reading. More...
 
qint64 bytesToWrite () const
 
void close ()
 Closes the socket. More...
 
bool connectToHost (const QHostAddress &address, quint16 port)
 Connects to the IP address and port specified by address and port. More...
 
bool connectToHostByName (const QString &name, quint16 port)
 Connects to the remote host name given by name on port port. More...
 
bool hasPendingDatagrams () const
 Returns true if there is at least one datagram pending. More...
 
bool initialize (QAbstractSocket::SocketType type, QAbstractSocket::NetworkLayerProtocol protocol=QAbstractSocket::IPv4Protocol)
 Initializes a QNativeSocketEngine by creating a new socket of type socketType and network layer protocol protocol. More...
 
bool initialize (int socketDescriptor, QAbstractSocket::SocketState socketState=QAbstractSocket::ConnectedState)
 Initializes the socket using socketDescriptor instead of creating a new one. More...
 
bool isExceptionNotificationEnabled () const
 
bool isReadNotificationEnabled () const
 
bool isValid () const
 Returns true if the socket is valid; otherwise returns false. More...
 
bool isWriteNotificationEnabled () const
 
bool joinMulticastGroup (const QHostAddress &groupAddress, const QNetworkInterface &iface)
 
bool leaveMulticastGroup (const QHostAddress &groupAddress, const QNetworkInterface &iface)
 
bool listen ()
 Prepares a TCP server for accepting incoming connections. More...
 
QNetworkInterface multicastInterface () const
 
int option (SocketOption option) const
 Returns the value of the option socketOption. More...
 
qint64 pendingDatagramSize () const
 Returns the size of the pending datagram, or -1 if no datagram is pending. More...
 
 QNativeSocketEngine (QObject *parent=0)
 Constructs a QNativeSocketEngine. More...
 
qint64 read (char *data, qint64 maxlen)
 Reads up to maxSize bytes into data from the socket. More...
 
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 bytes read. More...
 
qint64 receiveBufferSize () const
 Returns the size of the operating system's socket receive buffer. More...
 
qint64 sendBufferSize () const
 Returns the size of the operating system send buffer. More...
 
void setExceptionNotificationEnabled (bool enable)
 
bool setMulticastInterface (const QNetworkInterface &iface)
 
bool setOption (SocketOption option, int value)
 Sets the option option to the value value. More...
 
void setReadNotificationEnabled (bool enable)
 
void setReceiveBufferSize (qint64 bufferSize)
 Sets the size of the operating system receive buffer to size. More...
 
void setSendBufferSize (qint64 bufferSize)
 Sets the size of the operating system send buffer to size. More...
 
void setWriteNotificationEnabled (bool enable)
 
int socketDescriptor () const
 Returns the native socket descriptor. More...
 
bool waitForRead (int msecs=30000, bool *timedOut=0)
 Waits for msecs milliseconds or until the socket is ready for reading. More...
 
bool waitForReadOrWrite (bool *readyToRead, bool *readyToWrite, bool checkRead, bool checkWrite, int msecs=30000, bool *timedOut=0)
 
bool waitForWrite (int msecs=30000, bool *timedOut=0)
 Waits for msecs milliseconds or until the socket is ready for writing. More...
 
qint64 write (const char *data, qint64 len)
 Writes a block of size bytes from data to the socket. More...
 
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, and returns the number of bytes written, or -1 if an error occurred. More...
 
 ~QNativeSocketEngine ()
 Destructs a QNativeSocketEngine. More...
 
- Public Functions inherited from QAbstractSocketEngine
QAbstractSocket::SocketError error () const
 
QString errorString () const
 
QHostAddress localAddress () const
 
quint16 localPort () const
 
QHostAddress peerAddress () const
 
quint16 peerPort () const
 
QAbstractSocket::NetworkLayerProtocol protocol () const
 
 QAbstractSocketEngine (QObject *parent=0)
 
void setReceiver (QAbstractSocketEngineReceiver *receiver)
 
QAbstractSocket::SocketType socketType () const
 
QAbstractSocket::SocketState state () const
 
- Public Functions inherited from QObject
bool blockSignals (bool b)
 If block is true, signals emitted by this object are blocked (i.e., emitting a signal will not invoke anything connected to it). More...
 
const QObjectListchildren () const
 Returns a list of child objects. More...
 
bool connect (const QObject *sender, const char *signal, const char *member, Qt::ConnectionType type=Qt::AutoConnection) const
 
bool disconnect (const char *signal=0, const QObject *receiver=0, const char *member=0)
 
bool disconnect (const QObject *receiver, const char *member=0)
 
void dumpObjectInfo ()
 Dumps information about signal connections, etc. More...
 
void dumpObjectTree ()
 Dumps a tree of children to the debug output. More...
 
QList< QByteArraydynamicPropertyNames () const
 Returns the names of all properties that were dynamically added to the object using setProperty(). More...
 
virtual bool event (QEvent *)
 This virtual function receives events to an object and should return true if the event e was recognized and processed. More...
 
virtual bool eventFilter (QObject *, QEvent *)
 Filters events if this object has been installed as an event filter for the watched object. More...
 
template<typename T >
findChild (const QString &aName=QString()) const
 Returns the child of this object that can be cast into type T and that is called name, or 0 if there is no such object. More...
 
template<typename T >
QList< T > findChildren (const QString &aName=QString()) const
 Returns all children of this object with the given name that can be cast to type T, or an empty list if there are no such objects. More...
 
template<typename T >
QList< T > findChildren (const QRegExp &re) const
 
bool inherits (const char *classname) const
 Returns true if this object is an instance of a class that inherits className or a QObject subclass that inherits className; otherwise returns false. More...
 
void installEventFilter (QObject *)
 Installs an event filter filterObj on this object. More...
 
bool isWidgetType () const
 Returns true if the object is a widget; otherwise returns false. More...
 
void killTimer (int id)
 Kills the timer with timer identifier, id. More...
 
virtual const QMetaObjectmetaObject () const
 Returns a pointer to the meta-object of this object. More...
 
void moveToThread (QThread *thread)
 Changes the thread affinity for this object and its children. More...
 
QString objectName () const
 
QObjectparent () const
 Returns a pointer to the parent object. More...
 
QVariant property (const char *name) const
 Returns the value of the object's name property. More...
 
Q_INVOKABLE QObject (QObject *parent=0)
 Constructs an object with parent object parent. More...
 
void removeEventFilter (QObject *)
 Removes an event filter object obj from this object. More...
 
void setObjectName (const QString &name)
 
void setParent (QObject *)
 Makes the object a child of parent. More...
 
bool setProperty (const char *name, const QVariant &value)
 Sets the value of the object's name property to value. More...
 
void setUserData (uint id, QObjectUserData *data)
 
bool signalsBlocked () const
 Returns true if signals are blocked; otherwise returns false. More...
 
int startTimer (int interval)
 Starts a timer and returns a timer identifier, or returns zero if it could not start a timer. More...
 
QThreadthread () const
 Returns the thread in which the object lives. More...
 
QObjectUserDatauserData (uint id) const
 
virtual ~QObject ()
 Destroys the object, deleting all its child objects. More...
 

Additional Inherited Members

- Public Types inherited from QAbstractSocketEngine
enum  SocketOption {
  NonBlockingSocketOption, BroadcastSocketOption, ReceiveBufferSocketOption, SendBufferSocketOption,
  AddressReusable, BindExclusively, ReceiveOutOfBandData, LowDelayOption,
  KeepAliveOption, MulticastTtlOption, MulticastLoopbackOption
}
 
- Signals inherited from QObject
void destroyed (QObject *=0)
 This signal is emitted immediately before the object obj is destroyed, and can not be blocked. More...
 
- Static Public Functions inherited from QAbstractSocketEngine
static QAbstractSocketEnginecreateSocketEngine (QAbstractSocket::SocketType socketType, const QNetworkProxy &, QObject *parent)
 
static QAbstractSocketEnginecreateSocketEngine (int socketDescripter, QObject *parent)
 
- Static Public Functions inherited from QObject
static bool connect (const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
 Creates a connection of the given type from the signal in the sender object to the method in the receiver object. More...
 
static bool connect (const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &method, Qt::ConnectionType type=Qt::AutoConnection)
 
static bool disconnect (const QObject *sender, const char *signal, const QObject *receiver, const char *member)
 Disconnects signal in object sender from method in object receiver. More...
 
static bool disconnect (const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &member)
 
static uint registerUserData ()
 
static QString tr (const char *sourceText, const char *comment=0, int n=-1)
 
static QString trUtf8 (const char *sourceText, const char *comment=0, int n=-1)
 
- Static Public Variables inherited from QObject
static const QMetaObject staticMetaObject
 This variable stores the meta-object for the class. More...
 
- Protected Functions inherited from QAbstractSocketEngine
 QAbstractSocketEngine (QAbstractSocketEnginePrivate &dd, QObject *parent=0)
 
void setError (QAbstractSocket::SocketError error, const QString &errorString) const
 
void setLocalAddress (const QHostAddress &address)
 
void setLocalPort (quint16 port)
 
void setPeerAddress (const QHostAddress &address)
 
void setPeerPort (quint16 port)
 
void setProtocol (QAbstractSocket::NetworkLayerProtocol protocol)
 
void setSocketType (QAbstractSocket::SocketType socketType)
 
void setState (QAbstractSocket::SocketState state)
 
- Protected Functions inherited from QObject
virtual void childEvent (QChildEvent *)
 This event handler can be reimplemented in a subclass to receive child events. More...
 
virtual void connectNotify (const char *signal)
 This virtual function is called when something has been connected to signal in this object. More...
 
virtual void customEvent (QEvent *)
 This event handler can be reimplemented in a subclass to receive custom events. More...
 
virtual void disconnectNotify (const char *signal)
 This virtual function is called when something has been disconnected from signal in this object. More...
 
 QObject (QObjectPrivate &dd, QObject *parent=0)
 
int receivers (const char *signal) const
 Returns the number of receivers connected to the signal. More...
 
QObjectsender () const
 Returns a pointer to the object that sent the signal, if called in a slot activated by a signal; otherwise it returns 0. More...
 
int senderSignalIndex () const
 
virtual void timerEvent (QTimerEvent *)
 This event handler can be reimplemented in a subclass to receive timer events for the object. More...
 
- Protected Variables inherited from QObject
QScopedPointer< QObjectDatad_ptr
 
- Static Protected Variables inherited from QObject
static const QMetaObject staticQtMetaObject
 

Detailed Description

The QNativeSocketEngine class provides low level access to a socket.

Warning
This function is not part of the public interface.
Note
This class or function is reentrant.
Attention
Module: QtNetwork

QtSocketLayer provides basic socket functionality provided by the operating system. It also keeps track of what state the socket is in, and which errors that occur.

The classes QTcpSocket, QUdpSocket and QTcpServer provide a higher level API, and are in general more useful for the common application.

There are two main ways of initializing the a QNativeSocketEngine; either create a new socket by passing the socket type (TcpSocket or UdpSocket) and network layer protocol (IPv4Protocol or IPv6Protocol) to initialize(), or pass an existing socket descriptor and have QNativeSocketEngine determine the type and protocol itself. The native socket descriptor can later be fetched by calling socketDescriptor(). The socket is made non-blocking, but blocking behavior can still be achieved by calling waitForRead() and waitForWrite(). isValid() can be called to check if the socket has been successfully initialized and is ready to use.

To connect to a host, determine its address and pass this and the port number to connectToHost(). The socket can then be used as a TCP or UDP client. Otherwise; bind(), listen() and accept() are used to have the socket function as a TCP or UDP server. Call close() to close the socket.

bytesAvailable() is called to determine how much data is available for reading. read() and write() are used by both TCP and UDP clients to exchange data with the connected peer. UDP clients can also call hasMoreDatagrams(), nextDatagramSize(), readDatagram(), and writeDatagram().

Call state() to determine the state of the socket, for example, ListeningState or ConnectedState. socketType() tells whether the socket is a TCP socket or a UDP socket, or if the socket type is unknown. protocol() is used to determine the socket's network layer protocol.

localAddress(), localPort() are called to find the address and port that are currently bound to the socket. If the socket is connected, peerAddress() and peerPort() determine the address and port of the connected peer.

Finally, if any function should fail, error() and errorString() can be called to determine the cause of the error.

Definition at line 103 of file qnativesocketengine_p.h.

Constructors and Destructors

◆ QNativeSocketEngine()

QNativeSocketEngine::QNativeSocketEngine ( QObject parent = 0)

Constructs a QNativeSocketEngine.

See also
initialize()

Definition at line 330 of file qnativesocketengine.cpp.

◆ ~QNativeSocketEngine()

QNativeSocketEngine::~QNativeSocketEngine ( )

Destructs a QNativeSocketEngine.

Definition at line 338 of file qnativesocketengine.cpp.

339 {
340  close();
341 }
void close()
Closes the socket.

Functions

◆ accept()

int QNativeSocketEngine::accept ( )
virtual

Accepts a pending connection from the socket, which must be in ListeningState, and returns its socket descriptor.

If no pending connections are available, -1 is returned.

See also
bind(), listen()

Implements QAbstractSocketEngine.

Definition at line 654 of file qnativesocketengine.cpp.

655 {
660 
661  return d->nativeAccept();
662 }
double d
Definition: qnumeric_p.h:62
#define Q_D(Class)
Definition: qglobal.h:2482
The QNativeSocketEngine class provides low level access to a socket.
#define Q_CHECK_TYPE(function, type, returnValue)
#define Q_CHECK_VALID_SOCKETLAYER(function, returnValue)
#define Q_CHECK_STATE(function, checkState, returnValue)
int accept()
Accepts a pending connection from the socket, which must be in ListeningState, and returns its socket...

◆ bind()

bool QNativeSocketEngine::bind ( const QHostAddress address,
quint16  port 
)
virtual

Binds the socket to the address address and port port.

Returns true on success; otherwise false is returned. The port may be 0, in which case an arbitrary unused port is assigned automatically by the operating system.

Servers call this function to set up the server's address and port. TCP servers must in addition call listen() after bind().

Implements QAbstractSocketEngine.

Definition at line 594 of file qnativesocketengine.cpp.

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 }
double d
Definition: qnumeric_p.h:62
#define Q_D(Class)
Definition: qglobal.h:2482
The QNativeSocketEngine class provides low level access to a socket.
#define Q_CHECK_VALID_SOCKETLAYER(function, returnValue)
bool bind(const QHostAddress &address, quint16 port)
Binds the socket to the address address and port port.
QAbstractSocket::NetworkLayerProtocol protocol() const
Returns the network layer protocol of the host address.
#define Q_CHECK_STATE(function, checkState, returnValue)

◆ bytesAvailable()

qint64 QNativeSocketEngine::bytesAvailable ( ) const
virtual

Returns the number of bytes that are currently available for reading.

On error, -1 is returned.

For UDP sockets, this function returns the accumulated size of all pending datagrams, and it is therefore more useful for UDP sockets to call hasPendingDatagrams() and pendingDatagramSize().

Implements QAbstractSocketEngine.

Definition at line 720 of file qnativesocketengine.cpp.

721 {
722  Q_D(const QNativeSocketEngine);
725 
726  return d->nativeBytesAvailable();
727 }
double d
Definition: qnumeric_p.h:62
qint64 bytesAvailable() const
Returns the number of bytes that are currently available for reading.
#define Q_D(Class)
Definition: qglobal.h:2482
The QNativeSocketEngine class provides low level access to a socket.
#define Q_CHECK_NOT_STATE(function, checkState, returnValue)
#define Q_CHECK_VALID_SOCKETLAYER(function, returnValue)

◆ bytesToWrite()

qint64 QNativeSocketEngine::bytesToWrite ( ) const
virtual

Implements QAbstractSocketEngine.

Definition at line 824 of file qnativesocketengine.cpp.

825 {
826  return 0;
827 }

◆ close()

void QNativeSocketEngine::close ( )
virtual

Closes the socket.

In order to use the socket again, initialize() must be called.

Implements QAbstractSocketEngine.

Definition at line 863 of file qnativesocketengine.cpp.

Referenced by initialize(), read(), and ~QNativeSocketEngine().

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 }
double d
Definition: qnumeric_p.h:62
#define Q_D(Class)
Definition: qglobal.h:2482
The QNativeSocketEngine class provides low level access to a socket.
void qDeleteInEventHandler(QObject *o)
Definition: qobject.cpp:4348

◆ connectionNotification

void QNativeSocketEngine::connectionNotification ( )
slot

If there's a connection activity on the socket, process it.

Then notify our parent if there really was activity.

Definition at line 555 of file qnativesocketengine.cpp.

556 {
559 
560  connectToHost(d->peerAddress, d->peerPort);
562  // we changed states
564  }
565 }
double d
Definition: qnumeric_p.h:62
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
#define Q_D(Class)
Definition: qglobal.h:2482
The QNativeSocketEngine class provides low level access to a socket.
bool connectToHost(const QHostAddress &address, quint16 port)
Connects to the IP address and port specified by address and port.
QAbstractSocket::SocketState state() const

◆ connectToHost()

bool QNativeSocketEngine::connectToHost ( const QHostAddress address,
quint16  port 
)
virtual

Connects to the IP address and port specified by address and port.

If the connection is established, this function returns true and the socket enters ConnectedState. Otherwise, false is returned.

If false is returned, state() should be called to see if the socket is in ConnectingState. If so, a delayed TCP connection is taking place, and connectToHost() must be called again later to determine if the connection was established successfully or not. The second connection attempt must be made when the socket is ready for writing. This state can be determined either by connecting a QSocketNotifier to the socket descriptor returned by socketDescriptor(), or by calling the blocking function waitForWrite().

Example:

QNativeSocketEngine socketLayer;
// returns false
socketLayer.waitForWrite();
// returns true

Otherwise, error() should be called to determine the cause of the error.

Implements QAbstractSocketEngine.

Definition at line 524 of file qnativesocketengine.cpp.

Referenced by connectionNotification(), waitForRead(), waitForReadOrWrite(), and waitForWrite().

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 }
double d
Definition: qnumeric_p.h:62
#define Q_D(Class)
Definition: qglobal.h:2482
The QNativeSocketEngine class provides low level access to a socket.
bool connectToHost(const QHostAddress &address, quint16 port)
Connects to the IP address and port specified by address and port.
static QIntfbScreen * connected
#define Q_CHECK_VALID_SOCKETLAYER(function, returnValue)
QAbstractSocket::NetworkLayerProtocol protocol() const
Returns the network layer protocol of the host address.
#define Q_CHECK_STATES(function, state1, state2, returnValue)

◆ connectToHostByName()

bool QNativeSocketEngine::connectToHostByName ( const QString name,
quint16  port 
)
virtual

Connects to the remote host name given by name on port port.

When this function is called, the upper-level will not perform a hostname lookup.

The native socket engine does not support this operation, but some other socket engines (notably proxy-based ones) do.

Implements QAbstractSocketEngine.

Definition at line 575 of file qnativesocketengine.cpp.

576 {
577  Q_UNUSED(name);
578  Q_UNUSED(port);
582  return false;
583 }
double d
Definition: qnumeric_p.h:62
#define Q_D(Class)
Definition: qglobal.h:2482
The QNativeSocketEngine class provides low level access to a socket.
#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

◆ hasPendingDatagrams()

bool QNativeSocketEngine::hasPendingDatagrams ( ) const
virtual

Returns true if there is at least one datagram pending.

This function is only called by UDP sockets, where a datagram can have a size of 0. TCP sockets call bytesAvailable().

Implements QAbstractSocketEngine.

Definition at line 734 of file qnativesocketengine.cpp.

735 {
736  Q_D(const QNativeSocketEngine);
740 
741  return d->nativeHasPendingDatagrams();
742 }
double d
Definition: qnumeric_p.h:62
bool hasPendingDatagrams() const
Returns true if there is at least one datagram pending.
#define Q_D(Class)
Definition: qglobal.h:2482
The QNativeSocketEngine class provides low level access to a socket.
#define Q_CHECK_TYPE(function, type, returnValue)
#define Q_CHECK_NOT_STATE(function, checkState, returnValue)
#define Q_CHECK_VALID_SOCKETLAYER(function, returnValue)

◆ initialize() [1/2]

bool QNativeSocketEngine::initialize ( QAbstractSocket::SocketType  socketType,
QAbstractSocket::NetworkLayerProtocol  protocol = QAbstractSocket::IPv4Protocol 
)
virtual

Initializes a QNativeSocketEngine by creating a new socket of type socketType and network layer protocol protocol.

Returns true on success; otherwise returns false.

If the socket was already initialized, this function closes the socket before reeinitializing it.

The new socket is non-blocking, and for UDP sockets it's also broadcast enabled.

Implements QAbstractSocketEngine.

Definition at line 354 of file qnativesocketengine.cpp.

355 {
357  if (isValid())
358  close();
359 
360 #if defined(QT_NO_IPV6)
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.
396  close();
397  return false;
398  }
399 
400 
401  // Make sure we receive out-of-band data
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 }
double d
Definition: qnumeric_p.h:62
bool setOption(SocketOption option, int value)
Sets the option option to the value value.
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
The QString class provides a Unicode character string.
Definition: qstring.h:83
#define Q_D(Class)
Definition: qglobal.h:2482
The QNativeSocketEngine class provides low level access to a socket.
bool isValid() const
Returns true if the socket is valid; otherwise returns false.
Q_CORE_EXPORT void qDebug(const char *,...)
Q_CORE_EXPORT void qWarning(const char *,...)
QByteArray toLatin1() const Q_REQUIRED_RESULT
Returns a Latin-1 representation of the string as a QByteArray.
Definition: qstring.cpp:3993
const char * constData() const
Returns a pointer to the data stored in the byte array.
Definition: qbytearray.h:433
QAbstractSocket::NetworkLayerProtocol protocol() const
QAbstractSocket::SocketType socketType() const
void close()
Closes the socket.

◆ initialize() [2/2]

bool QNativeSocketEngine::initialize ( int  socketDescriptor,
QAbstractSocket::SocketState  socketState = QAbstractSocket::ConnectedState 
)
virtual

Initializes the socket using socketDescriptor instead of creating a new one.

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

The socket type and network layer protocol are determined automatically. The socket's state is set to socketState.

If the socket type is either TCP or UDP, it is made non-blocking. UDP sockets are also broadcast enabled.

Implements QAbstractSocketEngine.

Definition at line 439 of file qnativesocketengine.cpp.

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 }
double d
Definition: qnumeric_p.h:62
int socketDescriptor() const
Returns the native socket descriptor.
bool setOption(SocketOption option, int value)
Sets the option option to the value value.
#define Q_D(Class)
Definition: qglobal.h:2482
The QNativeSocketEngine class provides low level access to a socket.
bool isValid() const
Returns true if the socket is valid; otherwise returns false.
Q_CORE_EXPORT void qDebug(const char *,...)
void close()
Closes the socket.

◆ isExceptionNotificationEnabled()

bool QNativeSocketEngine::isExceptionNotificationEnabled ( ) const
virtual

Implements QAbstractSocketEngine.

Definition at line 1256 of file qnativesocketengine.cpp.

1257 {
1258  Q_D(const QNativeSocketEngine);
1259  return d->exceptNotifier && d->exceptNotifier->isEnabled();
1260 }
double d
Definition: qnumeric_p.h:62
#define Q_D(Class)
Definition: qglobal.h:2482
The QNativeSocketEngine class provides low level access to a socket.

◆ isReadNotificationEnabled()

bool QNativeSocketEngine::isReadNotificationEnabled ( ) const
virtual

Implements QAbstractSocketEngine.

Definition at line 1136 of file qnativesocketengine.cpp.

1137 {
1138  Q_D(const QNativeSocketEngine);
1139  return d->readNotifier && d->readNotifier->isEnabled();
1140 }
double d
Definition: qnumeric_p.h:62
#define Q_D(Class)
Definition: qglobal.h:2482
The QNativeSocketEngine class provides low level access to a socket.

◆ isValid()

bool QNativeSocketEngine::isValid ( ) const
virtual

Returns true if the socket is valid; otherwise returns false.

A socket is valid if it has not been successfully initialized, or if it has been closed.

Implements QAbstractSocketEngine.

Definition at line 486 of file qnativesocketengine.cpp.

Referenced by initialize().

487 {
488  Q_D(const QNativeSocketEngine);
489  return d->socketDescriptor != -1;
490 }
double d
Definition: qnumeric_p.h:62
#define Q_D(Class)
Definition: qglobal.h:2482
The QNativeSocketEngine class provides low level access to a socket.

◆ isWriteNotificationEnabled()

bool QNativeSocketEngine::isWriteNotificationEnabled ( ) const
virtual

Implements QAbstractSocketEngine.

Definition at line 1239 of file qnativesocketengine.cpp.

1240 {
1241  Q_D(const QNativeSocketEngine);
1242  return d->writeNotifier && d->writeNotifier->isEnabled();
1243 }
double d
Definition: qnumeric_p.h:62
#define Q_D(Class)
Definition: qglobal.h:2482
The QNativeSocketEngine class provides low level access to a socket.

◆ joinMulticastGroup()

bool QNativeSocketEngine::joinMulticastGroup ( const QHostAddress groupAddress,
const QNetworkInterface iface 
)
virtual
Since
4.8

Implements QAbstractSocketEngine.

Definition at line 669 of file qnativesocketengine.cpp.

671 {
676  return d->nativeJoinMulticastGroup(groupAddress, iface);
677 }
double d
Definition: qnumeric_p.h:62
#define Q_D(Class)
Definition: qglobal.h:2482
The QNativeSocketEngine class provides low level access to a socket.
#define Q_CHECK_TYPE(function, type, returnValue)
bool joinMulticastGroup(const QHostAddress &groupAddress, const QNetworkInterface &iface)
#define Q_CHECK_VALID_SOCKETLAYER(function, returnValue)
#define Q_CHECK_STATE(function, checkState, returnValue)

◆ leaveMulticastGroup()

bool QNativeSocketEngine::leaveMulticastGroup ( const QHostAddress groupAddress,
const QNetworkInterface iface 
)
virtual
Since
4.8

Implements QAbstractSocketEngine.

Definition at line 682 of file qnativesocketengine.cpp.

684 {
689  return d->nativeLeaveMulticastGroup(groupAddress, iface);
690 }
double d
Definition: qnumeric_p.h:62
#define Q_D(Class)
Definition: qglobal.h:2482
The QNativeSocketEngine class provides low level access to a socket.
#define Q_CHECK_TYPE(function, type, returnValue)
#define Q_CHECK_VALID_SOCKETLAYER(function, returnValue)
#define Q_CHECK_STATE(function, checkState, returnValue)
bool leaveMulticastGroup(const QHostAddress &groupAddress, const QNetworkInterface &iface)

◆ listen()

bool QNativeSocketEngine::listen ( )
virtual

Prepares a TCP server for accepting incoming connections.

This function must be called after bind(), and only by TCP sockets.

After this function has been called, pending client connections are detected by checking if the socket is ready for reading. This can be done by either creating a QSocketNotifier, passing the socket descriptor returned by socketDescriptor(), or by calling the blocking function waitForRead().

Example:

QNativeSocketEngine socketLayer;
socketLayer.bind(QHostAddress::Any, 4000);
socketLayer.listen();
if (socketLayer.waitForRead()) {
int clientSocket = socketLayer.accept();
// a client is connected
}
See also
bind(), accept()

Implements QAbstractSocketEngine.

Definition at line 633 of file qnativesocketengine.cpp.

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 }
double d
Definition: qnumeric_p.h:62
bool listen()
Prepares a TCP server for accepting incoming connections.
#define Q_D(Class)
Definition: qglobal.h:2482
The QNativeSocketEngine class provides low level access to a socket.
#define Q_CHECK_TYPE(function, type, returnValue)
#define Q_CHECK_VALID_SOCKETLAYER(function, returnValue)
#define Q_CHECK_STATE(function, checkState, returnValue)

◆ multicastInterface()

QNetworkInterface QNativeSocketEngine::multicastInterface ( ) const
virtual
Since
4.8

Implements QAbstractSocketEngine.

Definition at line 693 of file qnativesocketengine.cpp.

694 {
695  Q_D(const QNativeSocketEngine);
698  return d->nativeMulticastInterface();
699 }
double d
Definition: qnumeric_p.h:62
#define Q_D(Class)
Definition: qglobal.h:2482
The QNativeSocketEngine class provides low level access to a socket.
#define Q_CHECK_TYPE(function, type, returnValue)
#define Q_CHECK_VALID_SOCKETLAYER(function, returnValue)
QNetworkInterface multicastInterface() const
The QNetworkInterface class provides a listing of the host&#39;s IP addresses and network interfaces...

◆ option()

int QNativeSocketEngine::option ( SocketOption  option) const
virtual

Returns the value of the option socketOption.

Implements QAbstractSocketEngine.

Definition at line 1130 of file qnativesocketengine.cpp.

Referenced by receiveBufferSize(), and sendBufferSize().

1131 {
1132  Q_D(const QNativeSocketEngine);
1133  return d->option(socketOption);
1134 }
double d
Definition: qnumeric_p.h:62
#define Q_D(Class)
Definition: qglobal.h:2482
The QNativeSocketEngine class provides low level access to a socket.

◆ pendingDatagramSize()

qint64 QNativeSocketEngine::pendingDatagramSize ( ) const
virtual

Returns the size of the pending datagram, or -1 if no datagram is pending.

A datagram size of 0 is perfectly valid. This function is called by UDP sockets before receiveMessage(). For TCP sockets, call bytesAvailable().

Implements QAbstractSocketEngine.

Definition at line 750 of file qnativesocketengine.cpp.

751 {
752  Q_D(const QNativeSocketEngine);
755 
756  return d->nativePendingDatagramSize();
757 }
double d
Definition: qnumeric_p.h:62
#define Q_D(Class)
Definition: qglobal.h:2482
The QNativeSocketEngine class provides low level access to a socket.
#define Q_CHECK_TYPE(function, type, returnValue)
#define Q_CHECK_VALID_SOCKETLAYER(function, returnValue)
qint64 pendingDatagramSize() const
Returns the size of the pending datagram, or -1 if no datagram is pending.

◆ read()

qint64 QNativeSocketEngine::read ( char *  data,
qint64  maxSize 
)
virtual

Reads up to maxSize bytes into data from the socket.

Returns the number of bytes read, or -1 if an error occurred.

Implements QAbstractSocketEngine.

Definition at line 833 of file qnativesocketengine.cpp.

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 }
double d
Definition: qnumeric_p.h:62
QString qt_error_string(int errorCode)
Definition: qglobal.cpp:2600
qint64 read(char *data, qint64 maxlen)
Reads up to maxSize bytes into data from the socket.
#define Q_D(Class)
Definition: qglobal.h:2482
The QNativeSocketEngine class provides low level access to a socket.
static const char * data(const QByteArray &arr)
__int64 qint64
Definition: qglobal.h:942
#define Q_CHECK_VALID_SOCKETLAYER(function, returnValue)
void close()
Closes the socket.
#define Q_CHECK_STATES(function, state1, state2, returnValue)

◆ readDatagram()

qint64 QNativeSocketEngine::readDatagram ( char *  data,
qint64  maxSize,
QHostAddress address = 0,
quint16 port = 0 
)
virtual

Reads up to maxSize bytes of a datagram from the socket, stores it in data and returns the number of bytes read.

The address and port of the sender are stored in address and port. If either of these pointers is 0, the corresponding value is discarded.

To avoid unnecessarily loss of data, call pendingDatagramSize() to determine the size of the pending message before reading it. If maxSize is too small, the rest of the datagram will be lost.

Returns -1 if an error occurred.

See also
hasPendingDatagrams()

Implements QAbstractSocketEngine.

Definition at line 774 of file qnativesocketengine.cpp.

776 {
780 
781  return d->nativeReceiveDatagram(data, maxSize, address, port);
782 }
double d
Definition: qnumeric_p.h:62
#define Q_D(Class)
Definition: qglobal.h:2482
The QNativeSocketEngine class provides low level access to a socket.
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 ...
#define Q_CHECK_TYPE(function, type, returnValue)
static const char * data(const QByteArray &arr)
#define Q_CHECK_VALID_SOCKETLAYER(function, returnValue)

◆ receiveBufferSize()

qint64 QNativeSocketEngine::receiveBufferSize ( ) const

Returns the size of the operating system's socket receive buffer.

Depending on the operating system, this size may be different from what has been set earlier with setReceiveBufferSize().

Definition at line 1064 of file qnativesocketengine.cpp.

1065 {
1068 }
qint64 receiveBufferSize() const
Returns the size of the operating system&#39;s socket receive buffer.
int option(SocketOption option) const
Returns the value of the option socketOption.
#define Q_CHECK_VALID_SOCKETLAYER(function, returnValue)

◆ sendBufferSize()

qint64 QNativeSocketEngine::sendBufferSize ( ) const

Returns the size of the operating system send buffer.

Depending on the operating system, this size may be different from what has been set earlier with setSendBufferSize().

Definition at line 1096 of file qnativesocketengine.cpp.

1097 {
1100 }
void setSendBufferSize(qint64 bufferSize)
Sets the size of the operating system send buffer to size.
int option(SocketOption option) const
Returns the value of the option socketOption.
#define Q_CHECK_VALID_SOCKETLAYER(function, returnValue)

◆ setExceptionNotificationEnabled()

void QNativeSocketEngine::setExceptionNotificationEnabled ( bool  enable)
virtual

Implements QAbstractSocketEngine.

Definition at line 1262 of file qnativesocketengine.cpp.

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 }
double d
Definition: qnumeric_p.h:62
#define Q_D(Class)
Definition: qglobal.h:2482
The QNativeSocketEngine class provides low level access to a socket.

◆ setMulticastInterface()

bool QNativeSocketEngine::setMulticastInterface ( const QNetworkInterface iface)
virtual
Since
4.8

Implements QAbstractSocketEngine.

Definition at line 702 of file qnativesocketengine.cpp.

703 {
707  return d->nativeSetMulticastInterface(iface);
708 }
double d
Definition: qnumeric_p.h:62
bool setMulticastInterface(const QNetworkInterface &iface)
#define Q_D(Class)
Definition: qglobal.h:2482
The QNativeSocketEngine class provides low level access to a socket.
#define Q_CHECK_TYPE(function, type, returnValue)
#define Q_CHECK_VALID_SOCKETLAYER(function, returnValue)

◆ setOption()

bool QNativeSocketEngine::setOption ( SocketOption  option,
int  value 
)
virtual

Sets the option option to the value value.

Implements QAbstractSocketEngine.

Definition at line 1121 of file qnativesocketengine.cpp.

Referenced by initialize(), setReceiveBufferSize(), and setSendBufferSize().

1122 {
1124  return d->setOption(option, value);
1125 }
double d
Definition: qnumeric_p.h:62
#define Q_D(Class)
Definition: qglobal.h:2482
The QNativeSocketEngine class provides low level access to a socket.
int option(SocketOption option) const
Returns the value of the option socketOption.

◆ setReadNotificationEnabled()

void QNativeSocketEngine::setReadNotificationEnabled ( bool  enable)
virtual

Implements QAbstractSocketEngine.

Definition at line 1228 of file qnativesocketengine.cpp.

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 }
double d
Definition: qnumeric_p.h:62
#define Q_D(Class)
Definition: qglobal.h:2482
The QNativeSocketEngine class provides low level access to a socket.

◆ setReceiveBufferSize()

void QNativeSocketEngine::setReceiveBufferSize ( qint64  size)

Sets the size of the operating system receive buffer to size.

For clients, this should be set before connectToHost() is called; otherwise it will have no effect. For servers, it should be called before listen().

The operating system receive buffer size effectively limits two things: how much data can be in transit at any one moment, and how much data can be received in one iteration of the main event loop. Setting the size of the receive buffer may have an impact on the socket's performance.

The default value is operating system-dependent.

Definition at line 1085 of file qnativesocketengine.cpp.

1086 {
1089 }
bool setOption(SocketOption option, int value)
Sets the option option to the value value.
void setReceiveBufferSize(qint64 bufferSize)
Sets the size of the operating system receive buffer to size.
#define Q_CHECK_VALID_SOCKETLAYER(function, returnValue)
#define Q_VOID

◆ setSendBufferSize()

void QNativeSocketEngine::setSendBufferSize ( qint64  size)

Sets the size of the operating system send buffer to size.

The operating system send buffer size effectively limits how much data can be in transit at any one moment. Setting the size of the send buffer may have an impact on the socket's performance.

The default value is operating system-dependent.

Definition at line 1111 of file qnativesocketengine.cpp.

Referenced by sendBufferSize().

1112 {
1115 }
bool setOption(SocketOption option, int value)
Sets the option option to the value value.
void setSendBufferSize(qint64 bufferSize)
Sets the size of the operating system send buffer to size.
#define Q_CHECK_VALID_SOCKETLAYER(function, returnValue)
#define Q_VOID

◆ setWriteNotificationEnabled()

void QNativeSocketEngine::setWriteNotificationEnabled ( bool  enable)
virtual

Implements QAbstractSocketEngine.

Definition at line 1245 of file qnativesocketengine.cpp.

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 }
double d
Definition: qnumeric_p.h:62
#define Q_D(Class)
Definition: qglobal.h:2482
The QNativeSocketEngine class provides low level access to a socket.

◆ socketDescriptor()

int QNativeSocketEngine::socketDescriptor ( ) const
virtual

Returns the native socket descriptor.

Any use of this descriptor stands the risk of being non-portable.

Implements QAbstractSocketEngine.

Definition at line 496 of file qnativesocketengine.cpp.

Referenced by initialize().

497 {
498  Q_D(const QNativeSocketEngine);
499  return d->socketDescriptor;
500 }
double d
Definition: qnumeric_p.h:62
#define Q_D(Class)
Definition: qglobal.h:2482
The QNativeSocketEngine class provides low level access to a socket.

◆ waitForRead()

bool QNativeSocketEngine::waitForRead ( int  msecs = 30000,
bool *  timedOut = 0 
)
virtual

Waits for msecs milliseconds or until the socket is ready for reading.

If timedOut is not 0 and msecs milliseconds have passed, the value of timedOut is set to true.

Returns true if data is available for reading; otherwise returns false.

This is a blocking function call; its use is disadvised in a single threaded application, as the whole thread will stop responding until the function returns. waitForRead() is most useful when there is no event loop available. The general approach is to create a QSocketNotifier, passing the socket descriptor returned by socketDescriptor() to its constructor.

Implements QAbstractSocketEngine.

Definition at line 912 of file qnativesocketengine.cpp.

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 }
double d
Definition: qnumeric_p.h:62
#define Q_D(Class)
Definition: qglobal.h:2482
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 connectToHost(const QHostAddress &address, quint16 port)
Connects to the IP address and port specified by address and port.
#define Q_CHECK_NOT_STATE(function, checkState, returnValue)
#define Q_CHECK_VALID_SOCKETLAYER(function, returnValue)
QAbstractSocket::SocketState state() const

◆ waitForReadOrWrite()

bool QNativeSocketEngine::waitForReadOrWrite ( bool *  readyToRead,
bool *  readyToWrite,
bool  checkRead,
bool  checkWrite,
int  msecs = 30000,
bool *  timedOut = 0 
)
virtual

Implements QAbstractSocketEngine.

Definition at line 1006 of file qnativesocketengine.cpp.

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 }
double d
Definition: qnumeric_p.h:62
bool waitForReadOrWrite(bool *readyToRead, bool *readyToWrite, bool checkRead, bool checkWrite, int msecs=30000, bool *timedOut=0)
void setState(QAbstractSocket::SocketState state)
#define Q_D(Class)
Definition: qglobal.h:2482
The QNativeSocketEngine class provides low level access to a socket.
bool connectToHost(const QHostAddress &address, quint16 port)
Connects to the IP address and port specified by address and port.
#define Q_CHECK_NOT_STATE(function, checkState, returnValue)
#define Q_CHECK_VALID_SOCKETLAYER(function, returnValue)
bool waitForWrite(int msecs=30000, bool *timedOut=0)
Waits for msecs milliseconds or until the socket is ready for writing.
QAbstractSocket::SocketState state() const

◆ waitForWrite()

bool QNativeSocketEngine::waitForWrite ( int  msecs = 30000,
bool *  timedOut = 0 
)
virtual

Waits for msecs milliseconds or until the socket is ready for writing.

If timedOut is not 0 and msecs milliseconds have passed, the value of timedOut is set to true.

Returns true if data is available for writing; otherwise returns false.

This is a blocking function call; its use is disadvised in a single threaded application, as the whole thread will stop responding until the function returns. waitForWrite() is most useful when there is no event loop available. The general approach is to create a QSocketNotifier, passing the socket descriptor returned by socketDescriptor() to its constructor.

Implements QAbstractSocketEngine.

Definition at line 952 of file qnativesocketengine.cpp.

Referenced by waitForReadOrWrite().

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 }
double d
Definition: qnumeric_p.h:62
void setState(QAbstractSocket::SocketState state)
#define Q_D(Class)
Definition: qglobal.h:2482
The QNativeSocketEngine class provides low level access to a socket.
bool connectToHost(const QHostAddress &address, quint16 port)
Connects to the IP address and port specified by address and port.
#define Q_CHECK_NOT_STATE(function, checkState, returnValue)
#define Q_CHECK_VALID_SOCKETLAYER(function, returnValue)
bool waitForWrite(int msecs=30000, bool *timedOut=0)
Waits for msecs milliseconds or until the socket is ready for writing.
QAbstractSocket::SocketState state() const

◆ write()

qint64 QNativeSocketEngine::write ( const char *  data,
qint64  size 
)
virtual

Writes a block of size bytes from data to the socket.

Returns the number of bytes written, or -1 if an error occurred.

Implements QAbstractSocketEngine.

Definition at line 815 of file qnativesocketengine.cpp.

816 {
820  return d->nativeWrite(data, size);
821 }
double d
Definition: qnumeric_p.h:62
#define Q_D(Class)
Definition: qglobal.h:2482
The QNativeSocketEngine class provides low level access to a socket.
static const char * data(const QByteArray &arr)
#define Q_CHECK_VALID_SOCKETLAYER(function, returnValue)
#define Q_CHECK_STATE(function, checkState, returnValue)
qint64 write(const char *data, qint64 len)
Writes a block of size bytes from data to the socket.

◆ writeDatagram()

qint64 QNativeSocketEngine::writeDatagram ( const char *  data,
qint64  size,
const QHostAddress host,
quint16  port 
)
virtual

Writes a UDP datagram of size size bytes to the socket from data to the address host on port port, and returns the number of bytes written, or -1 if an error occurred.

Only one datagram is sent, and if there is too much data to fit into a single datagram, the operation will fail and error() will return QAbstractSocket::DatagramTooLargeError. Operating systems impose an upper limit to the size of a datagram, but this size is different on almost all platforms. Sending large datagrams is in general disadvised, as even if they are sent successfully, they are likely to be fragmented before arriving at their destination.

Experience has shown that it is in general safe to send datagrams no larger than 512 bytes.

See also
readDatagram()

Implements QAbstractSocketEngine.

Definition at line 802 of file qnativesocketengine.cpp.

804 {
808  return d->nativeSendDatagram(data, size, host, port);
809 }
double d
Definition: qnumeric_p.h:62
#define Q_D(Class)
Definition: qglobal.h:2482
The QNativeSocketEngine class provides low level access to a socket.
#define Q_CHECK_TYPE(function, type, returnValue)
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...
static const char * data(const QByteArray &arr)
#define Q_CHECK_VALID_SOCKETLAYER(function, returnValue)

The documentation for this class was generated from the following files: