Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
QLocalSocket Class Reference

The QLocalSocket class provides a local socket. More...

#include <qlocalsocket.h>

+ Inheritance diagram for QLocalSocket:
+ Collaboration diagram for QLocalSocket:

Public Types

enum  LocalSocketError {
  ConnectionRefusedError = QAbstractSocket::ConnectionRefusedError , PeerClosedError = QAbstractSocket::RemoteHostClosedError , ServerNotFoundError = QAbstractSocket::HostNotFoundError , SocketAccessError = QAbstractSocket::SocketAccessError ,
  SocketResourceError = QAbstractSocket::SocketResourceError , SocketTimeoutError = QAbstractSocket::SocketTimeoutError , DatagramTooLargeError = QAbstractSocket::DatagramTooLargeError , ConnectionError = QAbstractSocket::NetworkError ,
  UnsupportedSocketOperationError = QAbstractSocket::UnsupportedSocketOperationError , UnknownSocketError = QAbstractSocket::UnknownSocketError , OperationError = QAbstractSocket::OperationError
}
 The LocalServerError enumeration represents the errors that can occur. More...
 
enum  LocalSocketState { UnconnectedState = QAbstractSocket::UnconnectedState , ConnectingState = QAbstractSocket::ConnectingState , ConnectedState = QAbstractSocket::ConnectedState , ClosingState = QAbstractSocket::ClosingState }
 This enum describes the different states in which a socket can be. More...
 
enum  SocketOption { NoOptions = 0x00 , AbstractNamespaceOption = 0x01 }
 
- Public Types inherited from QIODeviceBase
enum  OpenModeFlag {
  NotOpen = 0x0000 , ReadOnly = 0x0001 , WriteOnly = 0x0002 , ReadWrite = ReadOnly | WriteOnly ,
  Append = 0x0004 , Truncate = 0x0008 , Text = 0x0010 , Unbuffered = 0x0020 ,
  NewOnly = 0x0040 , ExistingOnly = 0x0080
}
 This enum is used with QIODevice::open() to describe the mode in which a device is opened. More...
 

Signals

void connected ()
 This signal is emitted after connectToServer() has been called and a connection has been successfully established.
 
void disconnected ()
 This signal is emitted when the socket has been disconnected.
 
void errorOccurred (QLocalSocket::LocalSocketError socketError)
 
void stateChanged (QLocalSocket::LocalSocketState socketState)
 This signal is emitted whenever QLocalSocket's state changes.
 
- Signals inherited from QIODevice
void readyRead ()
 This signal is emitted once every time new data is available for reading from the device's current read channel.
 
void channelReadyRead (int channel)
 
void bytesWritten (qint64 bytes)
 This signal is emitted every time a payload of data has been written to the device's current write channel.
 
void channelBytesWritten (int channel, qint64 bytes)
 
void aboutToClose ()
 This signal is emitted when the device is about to close.
 
void readChannelFinished ()
 
- Signals inherited from QObject
void destroyed (QObject *=nullptr)
 This signal is emitted immediately before the object obj is destroyed, after any instances of QPointer have been notified, and cannot be blocked.
 
void objectNameChanged (const QString &objectName, QPrivateSignal)
 This signal is emitted after the object's name has been changed.
 

Public Member Functions

 QLocalSocket (QObject *parent=nullptr)
 Creates a new local socket.
 
 ~QLocalSocket ()
 Destroys the socket, closing the connection if necessary.
 
void connectToServer (OpenMode openMode=ReadWrite)
 
void connectToServer (const QString &name, OpenMode openMode=ReadWrite)
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.Set the server name and attempts to make a connection to it.
 
void disconnectFromServer ()
 Attempts to close the socket.
 
void setServerName (const QString &name)
 
QString serverName () const
 Returns the name of the peer as specified by setServerName(), or an empty QString if setServerName() has not been called or connectToServer() failed.
 
QString fullServerName () const
 Returns the server path that the socket is connected to.
 
void abort ()
 Aborts the current connection and resets the socket.
 
virtual bool isSequential () const override
 \reimp
 
virtual qint64 bytesAvailable () const override
 \reimp
 
virtual qint64 bytesToWrite () const override
 \reimp
 
virtual bool canReadLine () const override
 \reimp
 
virtual bool open (OpenMode openMode=ReadWrite) override
 Equivalent to connectToServer(OpenMode mode).
 
virtual void close () override
 Closes the I/O device for the socket and calls disconnectFromServer() to close the socket's connection.
 
LocalSocketError error () const
 Returns the type of error that last occurred.
 
bool flush ()
 This function writes as much as possible from the internal write buffer to the socket, without blocking.
 
bool isValid () const
 Returns true if the socket is valid and ready for use; otherwise returns false.
 
qint64 readBufferSize () const
 Returns the size of the internal read buffer.
 
void setReadBufferSize (qint64 size)
 Sets the size of QLocalSocket's internal read buffer to be size bytes.
 
bool setSocketDescriptor (qintptr socketDescriptor, LocalSocketState socketState=ConnectedState, OpenMode openMode=ReadWrite)
 Initializes QLocalSocket with the native socket descriptor socketDescriptor.
 
qintptr socketDescriptor () const
 Returns the native socket descriptor of the QLocalSocket object if this is available; otherwise returns -1.
 
void setSocketOptions (SocketOptions option)
 
SocketOptions socketOptions () const
 
QBindable< SocketOptions > bindableSocketOptions ()
 
LocalSocketState state () const
 Returns the state of the socket.
 
bool waitForBytesWritten (int msecs=30000) override
 \reimp
 
bool waitForConnected (int msecs=30000)
 Waits until the socket is connected, up to msecs milliseconds.
 
bool waitForDisconnected (int msecs=30000)
 Waits until the socket has disconnected, up to msecs milliseconds.
 
bool waitForReadyRead (int msecs=30000) override
 This function blocks until data is available for reading and the \l{QIODevice::}{readyRead()} signal has been emitted.
 
- Public Member Functions inherited from QIODevice
 QIODevice ()
 Constructs a QIODevice object.
 
 QIODevice (QObject *parent)
 Constructs a QIODevice object with the given parent.
 
virtual ~QIODevice ()
 The destructor is virtual, and QIODevice is an abstract base class.
 
QIODeviceBase::OpenMode openMode () const
 Returns the mode in which the device has been opened; i.e.
 
void setTextModeEnabled (bool enabled)
 If enabled is true, this function sets the \l Text flag on the device; otherwise the \l Text flag is removed.
 
bool isTextModeEnabled () const
 Returns true if the \l Text flag is enabled; otherwise returns false.
 
bool isOpen () const
 Returns true if the device is open; otherwise returns false.
 
bool isReadable () const
 Returns true if data can be read from the device; otherwise returns false.
 
bool isWritable () const
 Returns true if data can be written to the device; otherwise returns false.
 
int readChannelCount () const
 
int writeChannelCount () const
 
int currentReadChannel () const
 
void setCurrentReadChannel (int channel)
 
int currentWriteChannel () const
 
void setCurrentWriteChannel (int channel)
 
virtual bool open (QIODeviceBase::OpenMode mode)
 Opens the device and sets its OpenMode to mode.
 
virtual qint64 pos () const
 For random-access devices, this function returns the position that data is written to or read from.
 
virtual qint64 size () const
 For open random-access devices, this function returns the size of the device.
 
virtual bool seek (qint64 pos)
 For random-access devices, this function sets the current position to pos, returning true on success, or false if an error occurred.
 
virtual bool atEnd () const
 Returns true if the current read and write position is at the end of the device (i.e.
 
virtual bool reset ()
 Seeks to the start of input for random-access devices.
 
qint64 read (char *data, qint64 maxlen)
 Reads at most maxSize bytes from the device into data, and returns the number of bytes read.
 
QByteArray read (qint64 maxlen)
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.Reads at most maxSize bytes from the device, and returns the data read as a QByteArray.
 
QByteArray readAll ()
 Reads all remaining data from the device, and returns it as a byte array.
 
qint64 readLine (char *data, qint64 maxlen)
 This function reads a line of ASCII characters from the device, up to a maximum of maxSize - 1 bytes, stores the characters in data, and returns the number of bytes read.
 
QByteArray readLine (qint64 maxlen=0)
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.Reads a line from the device, but no more than maxSize characters, and returns the result as a byte array.
 
void startTransaction ()
 
void commitTransaction ()
 
void rollbackTransaction ()
 
bool isTransactionStarted () const
 
qint64 write (const char *data, qint64 len)
 Writes at most maxSize bytes of data from data to the device.
 
qint64 write (const char *data)
 
qint64 write (const QByteArray &data)
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.Writes the content of data to the device.
 
qint64 peek (char *data, qint64 maxlen)
 
QByteArray peek (qint64 maxlen)
 
qint64 skip (qint64 maxSize)
 
void ungetChar (char c)
 Puts the character c back into the device, and decrements the current position unless the position is 0.
 
bool putChar (char c)
 Writes the character c to the device.
 
bool getChar (char *c)
 Reads one character from the device and stores it in c.
 
QString errorString () const
 Returns a human-readable description of the last device error that occurred.
 
- Public Member Functions inherited from QObject
Q_INVOKABLE QObject (QObject *parent=nullptr)
 Constructs an object with parent object parent.
 
virtual ~QObject ()
 Destroys the object, deleting all its child objects.
 
virtual bool event (QEvent *event)
 This virtual function receives events to an object and should return true if the event e was recognized and processed.
 
virtual bool eventFilter (QObject *watched, QEvent *event)
 Filters events if this object has been installed as an event filter for the watched object.
 
QString objectName () const
 
Q_WEAK_OVERLOAD void setObjectName (const QString &name)
 Sets the object's name to name.
 
void setObjectName (QAnyStringView name)
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
 
QBindable< QStringbindableObjectName ()
 
bool isWidgetType () const
 Returns true if the object is a widget; otherwise returns false.
 
bool isWindowType () const
 Returns true if the object is a window; otherwise returns false.
 
bool isQuickItemType () const
 Returns true if the object is a QQuickItem; otherwise returns false.
 
bool signalsBlocked () const noexcept
 Returns true if signals are blocked; otherwise returns false.
 
bool blockSignals (bool b) noexcept
 If block is true, signals emitted by this object are blocked (i.e., emitting a signal will not invoke anything connected to it).
 
QThreadthread () const
 Returns the thread in which the object lives.
 
bool moveToThread (QThread *thread QT6_DECL_NEW_OVERLOAD_TAIL)
 Changes the thread affinity for this object and its children and returns true on success.
 
int startTimer (int interval, Qt::TimerType timerType=Qt::CoarseTimer)
 This is an overloaded function that will start a timer of type timerType and a timeout of interval milliseconds.
 
int startTimer (std::chrono::nanoseconds time, Qt::TimerType timerType=Qt::CoarseTimer)
 
void killTimer (int id)
 Kills the timer with timer identifier, id.
 
void killTimer (Qt::TimerId id)
 
template<typename T >
findChild (QAnyStringView aName, Qt::FindChildOptions options=Qt::FindChildrenRecursively) const
 Returns the child of this object that can be cast into type T and that is called name, or \nullptr if there is no such object.
 
template<typename T >
QList< T > findChildren (QAnyStringView aName, Qt::FindChildOptions options=Qt::FindChildrenRecursively) 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.
 
template<typename T >
findChild (Qt::FindChildOptions options=Qt::FindChildrenRecursively) const
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
 
template<typename T >
QList< T > findChildren (Qt::FindChildOptions options=Qt::FindChildrenRecursively) const
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
 
const QObjectListchildren () const
 Returns a list of child objects.
 
void setParent (QObject *parent)
 Makes the object a child of parent.
 
void installEventFilter (QObject *filterObj)
 Installs an event filter filterObj on this object.
 
void removeEventFilter (QObject *obj)
 Removes an event filter object obj from this object.
 
QMetaObject::Connection connect (const QObject *sender, const char *signal, const char *member, Qt::ConnectionType type=Qt::AutoConnection) const
 
bool disconnect (const char *signal=nullptr, const QObject *receiver=nullptr, const char *member=nullptr) const
 
bool disconnect (const QObject *receiver, const char *member=nullptr) const
 
void dumpObjectTree () const
 Dumps a tree of children to the debug output.
 
void dumpObjectInfo () const
 Dumps information about signal connections, etc.
 
bool setProperty (const char *name, const QVariant &value)
 Sets the value of the object's name property to value.
 
bool setProperty (const char *name, QVariant &&value)
 
QVariant property (const char *name) const
 Returns the value of the object's name property.
 
QList< QByteArraydynamicPropertyNames () const
 
QBindingStoragebindingStorage ()
 
const QBindingStoragebindingStorage () const
 
QObjectparent () const
 Returns a pointer to the parent object.
 
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.
 

Protected Member Functions

virtual qint64 readData (char *, qint64) override
 \reimp
 
qint64 readLineData (char *data, qint64 maxSize) override
 \reimp
 
qint64 skipData (qint64 maxSize) override
 \reimp
 
virtual qint64 writeData (const char *, qint64) override
 \reimp
 
- Protected Member Functions inherited from QIODevice
 QIODevice (QIODevicePrivate &dd, QObject *parent=nullptr)
 
void setOpenMode (QIODeviceBase::OpenMode openMode)
 Sets the OpenMode of the device to openMode.
 
void setErrorString (const QString &errorString)
 Sets the human readable description of the last device error that occurred to str.
 
- Protected Member Functions inherited from QObject
QObjectsender () const
 Returns a pointer to the object that sent the signal, if called in a slot activated by a signal; otherwise it returns \nullptr.
 
int senderSignalIndex () const
 
int receivers (const char *signal) const
 Returns the number of receivers connected to the signal.
 
bool isSignalConnected (const QMetaMethod &signal) const
 
virtual void timerEvent (QTimerEvent *event)
 This event handler can be reimplemented in a subclass to receive timer events for the object.
 
virtual void childEvent (QChildEvent *event)
 This event handler can be reimplemented in a subclass to receive child events.
 
virtual void customEvent (QEvent *event)
 This event handler can be reimplemented in a subclass to receive custom events.
 
virtual void connectNotify (const QMetaMethod &signal)
 
virtual void disconnectNotify (const QMetaMethod &signal)
 
 QObject (QObjectPrivate &dd, QObject *parent=nullptr)
 
- Protected Member Functions inherited from QIODeviceBase
 ~QIODeviceBase ()=default
 

Properties

SocketOptions socketOptions
 the socket options.
 
- Properties inherited from QObject
QString objectName
 the name of this object
 

Additional Inherited Members

- Public Slots inherited from QObject
void deleteLater ()
 \threadsafe
 
- Static Public Member Functions inherited from QObject
static QMetaObject::Connection connect (const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
 \threadsafe
 
static QMetaObject::Connection connect (const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &method, Qt::ConnectionType type=Qt::AutoConnection)
 
template<typename Func1 , typename Func2 >
static QMetaObject::Connection connect (const typename QtPrivate::FunctionPointer< Func1 >::Object *sender, Func1 signal, const typename QtPrivate::ContextTypeForFunctor< Func2 >::ContextType *context, Func2 &&slot, Qt::ConnectionType type=Qt::AutoConnection)
 
template<typename Func1 , typename Func2 >
static QMetaObject::Connection connect (const typename QtPrivate::FunctionPointer< Func1 >::Object *sender, Func1 signal, Func2 &&slot)
 
static bool disconnect (const QObject *sender, const char *signal, const QObject *receiver, const char *member)
 \threadsafe
 
static bool disconnect (const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &member)
 
static bool disconnect (const QMetaObject::Connection &)
 Disconnect a connection.
 
template<typename Func1 , typename Func2 >
static bool disconnect (const typename QtPrivate::FunctionPointer< Func1 >::Object *sender, Func1 signal, const typename QtPrivate::FunctionPointer< Func2 >::Object *receiver, Func2 slot)
 
template<typename Func1 >
static bool disconnect (const typename QtPrivate::FunctionPointer< Func1 >::Object *sender, Func1 signal, const QObject *receiver, void **zero)
 
- Protected Attributes inherited from QObject
QScopedPointer< QObjectDatad_ptr
 

Detailed Description

The QLocalSocket class provides a local socket.

Since
4.4 \inmodule QtNetwork

On Windows this is a named pipe and on Unix this is a local domain socket.

If an error occurs, error() returns the type of error, and errorString() can be called to get a human readable description of what happened.

Although QLocalSocket is designed for use with an event loop, it's possible to use it without one. In that case, you must use waitForConnected(), waitForReadyRead(), waitForBytesWritten(), and waitForDisconnected() which blocks until the operation is complete or the timeout expires.

See also
QLocalServer

Definition at line 21 of file qlocalsocket.h.

Member Enumeration Documentation

◆ LocalSocketError

The LocalServerError enumeration represents the errors that can occur.

The most recent error can be retrieved through a call to \l QLocalSocket::error().

\value ConnectionRefusedError The connection was refused by the peer (or timed out). \value PeerClosedError The remote socket closed the connection. Note that the client socket (i.e., this socket) will be closed after the remote close notification has been sent. \value ServerNotFoundError The local socket name was not found. \value SocketAccessError The socket operation failed because the application lacked the required privileges. \value SocketResourceError The local system ran out of resources (e.g., too many sockets). \value SocketTimeoutError The socket operation timed out. \value DatagramTooLargeError The datagram was larger than the operating system's limit (which can be as low as 8192 bytes). \value ConnectionError An error occurred with the connection. \value UnsupportedSocketOperationError The requested socket operation is not supported by the local operating system. \value OperationError An operation was attempted while the socket was in a state that did not permit it. \value UnknownSocketError An unidentified error occurred.

Enumerator
ConnectionRefusedError 
PeerClosedError 
ServerNotFoundError 
SocketAccessError 
SocketResourceError 
SocketTimeoutError 
DatagramTooLargeError 
ConnectionError 
UnsupportedSocketOperationError 
UnknownSocketError 
OperationError 

Definition at line 29 of file qlocalsocket.h.

◆ LocalSocketState

This enum describes the different states in which a socket can be.

See also
QLocalSocket::state()

\value UnconnectedState The socket is not connected. \value ConnectingState The socket has started establishing a connection. \value ConnectedState A connection is established. \value ClosingState The socket is about to close (data may still be waiting to be written).

Enumerator
UnconnectedState 
ConnectingState 
ConnectedState 
ClosingState 

Definition at line 44 of file qlocalsocket.h.

◆ SocketOption

Since
6.2 This enum describes the possible options that can be used to connect to a server. Currently, on Linux and Android it is used for specifying connection to a server listening to a socket bound to an abstract address.

\value NoOptions No options have been set. \value AbstractNamespaceOption The socket will try to connect to an abstract address. This flag is specific to Linux and Android. On other platforms is ignored.

See also
socketOptions
Enumerator
NoOptions 
AbstractNamespaceOption 

Definition at line 52 of file qlocalsocket.h.

Constructor & Destructor Documentation

◆ QLocalSocket()

QLocalSocket::QLocalSocket ( QObject * parent = nullptr)

Creates a new local socket.

The parent argument is passed to QObject's constructor.

Definition at line 358 of file qlocalsocket.cpp.

References d.

◆ ~QLocalSocket()

QLocalSocket::~QLocalSocket ( )

Destroys the socket, closing the connection if necessary.

Definition at line 370 of file qlocalsocket.cpp.

References abort(), and d.

+ Here is the call graph for this function:

Member Function Documentation

◆ abort()

void QLocalSocket::abort ( )

Aborts the current connection and resets the socket.

Unlike disconnectFromServer(), this function immediately closes the socket, clearing any pending data in the write buffer.

See also
disconnectFromServer(), close()

Definition at line 288 of file qlocalsocket_tcp.cpp.

References close(), and d.

Referenced by ~QLocalSocket().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ bindableSocketOptions()

QBindable< QLocalSocket::SocketOptions > QLocalSocket::bindableSocketOptions ( )

Definition at line 458 of file qlocalsocket.cpp.

References d.

◆ bytesAvailable()

qint64 QLocalSocket::bytesAvailable ( ) const
overridevirtual

\reimp

Reimplemented from QIODevice.

Definition at line 295 of file qlocalsocket_tcp.cpp.

References QIODevice::bytesAvailable(), and d.

Referenced by QBluetoothSocketPrivateBluezDBus::bytesAvailable().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ bytesToWrite()

qint64 QLocalSocket::bytesToWrite ( ) const
overridevirtual

\reimp

Reimplemented from QIODevice.

Definition at line 301 of file qlocalsocket_tcp.cpp.

References d.

Referenced by QBluetoothSocketPrivateBluezDBus::bytesToWrite(), and QLocalClientConnection::disconnect().

+ Here is the caller graph for this function:

◆ canReadLine()

bool QLocalSocket::canReadLine ( ) const
overridevirtual

\reimp

Reimplemented from QIODevice.

Definition at line 307 of file qlocalsocket_tcp.cpp.

References QIODevice::canReadLine(), and d.

Referenced by QBluetoothSocketPrivateBluezDBus::canReadLine().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ close()

void QLocalSocket::close ( )
overridevirtual

Closes the I/O device for the socket and calls disconnectFromServer() to close the socket's connection.

See QIODevice::close() for a description of the actions that occur when an I/O device is closed.

See also
abort()

Reimplemented from QIODevice.

Definition at line 313 of file qlocalsocket_tcp.cpp.

References QIODevice::close(), and d.

Referenced by abort(), and QBluetoothSocketPrivateBluezDBus::abort().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ connected

void QLocalSocket::connected ( )
signal

This signal is emitted after connectToServer() has been called and a connection has been successfully established.

See also
connectToServer(), disconnected()

◆ connectToServer() [1/2]

void QLocalSocket::connectToServer ( const QString & name,
OpenMode openMode = ReadWrite )

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.Set the server name and attempts to make a connection to it.

The socket is opened in the given openMode and first enters ConnectingState. If a connection is established, QLocalSocket enters ConnectedState and emits connected().

After calling this function, the socket can emit errorOccurred() to signal that an error occurred.

See also
state(), serverName(), waitForConnected()

Definition at line 396 of file qlocalsocket.cpp.

References connectToServer(), QIODevice::openMode(), and setServerName().

+ Here is the call graph for this function:

◆ connectToServer() [2/2]

void QLocalSocket::connectToServer ( OpenMode openMode = ReadWrite)
Since
5.1

Attempts to make a connection to serverName(). setServerName() must be called before you open the connection. Alternatively you can use connectToServer(const QString &name, OpenMode openMode);

The socket is opened in the given openMode and first enters ConnectingState. If a connection is established, QLocalSocket enters ConnectedState and emits connected().

After calling this function, the socket can emit errorOccurred() to signal that an error occurred.

See also
state(), serverName(), waitForConnected()

Definition at line 180 of file qlocalsocket_tcp.cpp.

References ConnectedState, ConnectingState, d, emit, errorOccurred(), QHostAddress::LocalHost, ok, QIODevice::open(), QIODevice::openMode(), OperationError, ServerNotFoundError, QIODevice::setErrorString(), settings, state(), stateChanged(), QVariant::toUInt(), tr, and QSettings::value().

Referenced by connectToServer(), and open().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ disconnected

void QLocalSocket::disconnected ( )
signal

This signal is emitted when the socket has been disconnected.

See also
connectToServer(), disconnectFromServer(), abort(), connected()

◆ disconnectFromServer()

void QLocalSocket::disconnectFromServer ( )

Attempts to close the socket.

If there is pending data waiting to be written, QLocalSocket will enter ClosingState and wait until all data has been written. Eventually, it will enter UnconnectedState and emit the disconnected() signal.

See also
connectToServer()

Definition at line 335 of file qlocalsocket_tcp.cpp.

References d.

◆ error()

◆ errorOccurred

void QLocalSocket::errorOccurred ( QLocalSocket::LocalSocketError socketError)
signal
Since
5.15

This signal is emitted after an error occurred. The socketError parameter describes the type of error that occurred.

QLocalSocket::LocalSocketError is not a registered metatype, so for queued connections, you will have to register it with Q_DECLARE_METATYPE() and qRegisterMetaType().

See also
error(), errorString(), {Creating Custom Qt Types}

Referenced by LocalSocketSignalTranslator::LocalSocketSignalTranslator(), and connectToServer().

+ Here is the caller graph for this function:

◆ flush()

bool QLocalSocket::flush ( )

This function writes as much as possible from the internal write buffer to the socket, without blocking.

If any data was written, this function returns true; otherwise false is returned.

Call this function if you need QLocalSocket to start sending buffered data immediately. The number of bytes successfully written depends on the operating system. In most cases, you do not need to call this function, because QLocalSocket will start sending data automatically once control goes back to the event loop. In the absence of an event loop, call waitForBytesWritten() instead.

See also
write(), waitForBytesWritten()

Definition at line 329 of file qlocalsocket_tcp.cpp.

References d.

Referenced by QLocalClientConnection::flush().

+ Here is the caller graph for this function:

◆ fullServerName()

QString QLocalSocket::fullServerName ( ) const

Returns the server path that the socket is connected to.

Note
The return value of this function is platform specific.
See also
connectToServer(), serverName()

Definition at line 471 of file qlocalsocket.cpp.

References d.

◆ isSequential()

bool QLocalSocket::isSequential ( ) const
overridevirtual

\reimp

Reimplemented from QIODevice.

Definition at line 490 of file qlocalsocket.cpp.

◆ isValid()

bool QLocalSocket::isValid ( ) const

Returns true if the socket is valid and ready for use; otherwise returns false.

Note
The socket's state must be ConnectedState before reading and writing can occur.
See also
state(), connectToServer()

Definition at line 374 of file qlocalsocket_tcp.cpp.

References d.

◆ open()

bool QLocalSocket::open ( OpenMode openMode = ReadWrite)
overridevirtual

Equivalent to connectToServer(OpenMode mode).

The socket is opened in the given openMode to the server defined by setServerName().

Note that unlike in most other QIODevice subclasses, open() may not open the device directly. The function return false if the socket was already connected or if the server to connect to was not defined and true in any other case. The connected() or errorOccurred() signals will be emitted once the device is actually open (or the connection failed).

See connectToServer() for more details.

Definition at line 379 of file qlocalsocket.cpp.

References connectToServer(), QIODevice::isOpen(), and QIODevice::openMode().

+ Here is the call graph for this function:

◆ readBufferSize()

qint64 QLocalSocket::readBufferSize ( ) const

Returns the size of the internal read buffer.

This limits the amount of data that the client can receive before you call read() or readAll(). A read buffer size of 0 (the default) means that the buffer has no size limit, ensuring that no data is lost.

See also
setReadBufferSize(), read()

Definition at line 380 of file qlocalsocket_tcp.cpp.

References d.

◆ readData()

qint64 QLocalSocket::readData ( char * data,
qint64 c )
overrideprotectedvirtual

\reimp

Implements QIODevice.

Definition at line 261 of file qlocalsocket_tcp.cpp.

References d.

◆ readLineData()

qint64 QLocalSocket::readLineData ( char * data,
qint64 maxSize )
overrideprotectedvirtual

\reimp

Reimplemented from QIODevice.

Definition at line 267 of file qlocalsocket_tcp.cpp.

◆ serverName()

QString QLocalSocket::serverName ( ) const

Returns the name of the peer as specified by setServerName(), or an empty QString if setServerName() has not been called or connectToServer() failed.

See also
connectToServer(), fullServerName()

Definition at line 427 of file qlocalsocket.cpp.

References d.

◆ setReadBufferSize()

void QLocalSocket::setReadBufferSize ( qint64 size)

Sets the size of QLocalSocket's internal read buffer to be size bytes.

If the buffer size is limited to a certain size, QLocalSocket won't buffer more than this size of data. Exceptionally, a buffer size of 0 means that the read buffer is unlimited and all incoming data is buffered. This is the default.

This option is useful if you only read the data at certain points in time (e.g., in a real-time streaming application) or if you want to protect your socket against receiving too much data, which may eventually cause your application to run out of memory.

See also
readBufferSize(), read()

Definition at line 386 of file qlocalsocket_tcp.cpp.

References d.

◆ setServerName()

void QLocalSocket::setServerName ( const QString & name)
Since
5.1

Set the name of the peer to connect to. On Windows name is the name of a named pipe; on Unix name is the name of a local domain socket.

This function must be called when the socket is not connected.

Definition at line 410 of file qlocalsocket.cpp.

References d, qWarning, and UnconnectedState.

Referenced by connectToServer().

+ Here is the caller graph for this function:

◆ setSocketDescriptor()

bool QLocalSocket::setSocketDescriptor ( qintptr socketDescriptor,
LocalSocketState socketState = ConnectedState,
OpenMode openMode = ReadWrite )

Initializes QLocalSocket with the native socket descriptor socketDescriptor.

Returns true if socketDescriptor is accepted as a valid socket descriptor; otherwise returns false. The socket is opened in the mode specified by openMode, and enters the socket state specified by socketState.

Note
It is not possible to initialize two local sockets with the same native socket descriptor.
See also
socketDescriptor(), state(), openMode()

Definition at line 215 of file qlocalsocket_tcp.cpp.

References child, QAbstractSocket::ClosingState, ClosingState, QAbstractSocket::ConnectedState, ConnectedState, QAbstractSocket::ConnectingState, ConnectingState, d, QIODevice::open(), QIODevice::openMode(), QObject::parent(), socketDescriptor(), QAbstractSocket::UnconnectedState, and UnconnectedState.

+ Here is the call graph for this function:

◆ setSocketOptions()

void QLocalSocket::setSocketOptions ( SocketOptions option)

Definition at line 448 of file qlocalsocket.cpp.

References d, qWarning, and UnconnectedState.

◆ skipData()

qint64 QLocalSocket::skipData ( qint64 maxSize)
overrideprotectedvirtual

\reimp

Reimplemented from QIODevice.

Definition at line 277 of file qlocalsocket_tcp.cpp.

◆ socketDescriptor()

qintptr QLocalSocket::socketDescriptor ( ) const

Returns the native socket descriptor of the QLocalSocket object if this is available; otherwise returns -1.

The socket descriptor is not available when QLocalSocket is in UnconnectedState. The type of the descriptor depends on the platform:

\list

  • On Windows, the returned value is a \l{Winsock 2 Socket Handle}.
  • On all other UNIX-like operating systems, the type is a file descriptor representing a socket. \endlist
See also
setSocketDescriptor()

Definition at line 255 of file qlocalsocket_tcp.cpp.

References d.

Referenced by QBluetoothSocketPrivateBluezDBus::localPort(), QBluetoothSocketPrivateBluezDBus::peerPort(), and setSocketDescriptor().

+ Here is the caller graph for this function:

◆ socketOptions()

QLocalSocket::SocketOptions QLocalSocket::socketOptions ( ) const

Definition at line 442 of file qlocalsocket.cpp.

References d.

◆ state()

QLocalSocket::LocalSocketState QLocalSocket::state ( ) const

Returns the state of the socket.

See also
error()

Definition at line 482 of file qlocalsocket.cpp.

References d.

Referenced by connectToServer(), QLocalClientConnection::isConnected(), waitForConnected(), waitForDisconnected(), and waitForReadyRead().

+ Here is the caller graph for this function:

◆ stateChanged

void QLocalSocket::stateChanged ( QLocalSocket::LocalSocketState socketState)
signal

This signal is emitted whenever QLocalSocket's state changes.

The socketState parameter is the new state.

QLocalSocket::SocketState is not a registered metatype, so for queued connections, you will have to register it with Q_DECLARE_METATYPE() and qRegisterMetaType().

See also
state(), {Creating Custom Qt Types}

Referenced by LocalSocketSignalTranslator::LocalSocketSignalTranslator(), and connectToServer().

+ Here is the caller graph for this function:

◆ waitForBytesWritten()

bool QLocalSocket::waitForBytesWritten ( int msecs = 30000)
overridevirtual

\reimp

Reimplemented from QIODevice.

Definition at line 323 of file qlocalsocket_tcp.cpp.

References d.

Referenced by QLocalClientConnection::disconnect().

+ Here is the caller graph for this function:

◆ waitForConnected()

bool QLocalSocket::waitForConnected ( int msecs = 30000)

Waits until the socket is connected, up to msecs milliseconds.

If the connection has been established, this function returns true; otherwise it returns false. In the case where it returns false, you can call error() to determine the cause of the error.

The following example waits up to one second for a connection to be established:

socket->connectToServer("market");
qDebug("Connected!");

If msecs is -1, this function will not time out.

See also
connectToServer(), connected()

Definition at line 392 of file qlocalsocket_tcp.cpp.

References ConnectedState, ConnectingState, d, and state().

Referenced by QLocalClientConnection::waitForConnection().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ waitForDisconnected()

bool QLocalSocket::waitForDisconnected ( int msecs = 30000)

Waits until the socket has disconnected, up to msecs milliseconds.

If the connection was successfully disconnected, this function returns true; otherwise it returns false (if the operation timed out, if an error occurred, or if this QLocalSocket is already disconnected). In the case where it returns false, you can call error() to determine the cause of the error.

The following example waits up to one second for a connection to be closed:

socket->disconnectFromServer();
qDebug("Disconnected!");
}

If msecs is -1, this function will not time out.

See also
disconnectFromServer(), close()

Definition at line 401 of file qlocalsocket_tcp.cpp.

References d, qWarning, state(), and UnconnectedState.

+ Here is the call graph for this function:

◆ waitForReadyRead()

bool QLocalSocket::waitForReadyRead ( int msecs = 30000)
overridevirtual

This function blocks until data is available for reading and the \l{QIODevice::}{readyRead()} signal has been emitted.

The function will timeout after msecs milliseconds; the default timeout is 30000 milliseconds.

The function returns true if data is available for reading; otherwise it returns false (if an error occurred or the operation timed out).

See also
waitForBytesWritten()

Reimplemented from QIODevice.

Definition at line 411 of file qlocalsocket_tcp.cpp.

References d, state(), and UnconnectedState.

+ Here is the call graph for this function:

◆ writeData()

qint64 QLocalSocket::writeData ( const char * data,
qint64 c )
overrideprotectedvirtual

\reimp

Implements QIODevice.

Definition at line 282 of file qlocalsocket_tcp.cpp.

References d.

Property Documentation

◆ socketOptions

QLocalSocket::socketOptions
readwrite

the socket options.

Since
6.2

Options must be set while the socket is in \l{UnconnectedState} state.

See also
connectToServer()

Definition at line 25 of file qlocalsocket.h.


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