6.0.0-rc4Source

QLocalSocket Class

The QLocalSocket class provides a local stream socket for talking to another process on the same machine. More…

Header#include <QLocalSocket>
CMaketarget_link_libraries(app PRIVATE Nt::Network)
ModuleNt Network C++ Classes
InheritsQIODevice

List of all members, including inherited members

Public Types

Public Functions

QLocalSocket(QObject *parent = nullptr)
~QLocalSocket()
voidconnectToServer(OpenMode openMode = ReadWrite)
voidconnectToServer(const QString &name, OpenMode openMode = ReadWrite)
voiddisconnectFromServer()
voidsetServerName(const QString &name)
QStringserverName() const
QStringfullServerName() const
voidabort()
boolflush()
boolisValid() const
LocalSocketErrorerror() const
LocalSocketStatestate() const
qint64readBufferSize() const
voidsetReadBufferSize(qint64 size)
boolsetSocketDescriptor(qintptr socketDescriptor, LocalSocketState socketState = ConnectedState, OpenMode openMode = ReadWrite)
qintptrsocketDescriptor() const
boolwaitForConnected(int msecs = 30000)
boolwaitForDisconnected(int msecs = 30000)

Reimplemented Public Functions

boolisSequential() const
qint64bytesAvailable() const
qint64bytesToWrite() const
boolcanReadLine() const
voidclose()
boolwaitForReadyRead(int msecs = 30000)
boolwaitForBytesWritten(int msecs = 30000)

Signals

Reimplemented Protected Functions

qint64readData(char *data, qint64 maxSize)
qint64writeData(const char *data, qint64 maxSize)

Detailed Description

Connect to a QLocalServer by name, then read and write as with any QIODevice:

QLocalSocket socket;
socket.connectToServer("my-service");
socket.write("hello");

A relative name is placed in QDir::tempPath(); a name starting with / is used as the socket path.

See also QLocalServer.

Differences from Qt

Connecting either succeeds or fails before connectToServer() returns, so the socket never stays in ConnectingState. Linux and other POSIX systems only, using Unix domain sockets.

The class is not built for Zephyr, where QT_NO_LOCALSOCKET is defined instead. See Nt on Zephyr.

Member Type Documentation

enum QLocalSocket::LocalSocketError

Describes an error on a local socket.

ConstantDescription
QLocalSocket::ConnectionRefusedErrorThe server refused the connection.
QLocalSocket::PeerClosedErrorThe other end closed the connection.
QLocalSocket::ServerNotFoundErrorNo server has that name.
QLocalSocket::SocketAccessErrorThe process may not use that socket.
QLocalSocket::SocketResourceErrorThe system ran out of resources.
QLocalSocket::SocketTimeoutErrorThe operation timed out.
QLocalSocket::DatagramTooLargeErrorThe data was too large to send.
QLocalSocket::ConnectionErrorA connection error occurred.
QLocalSocket::UnsupportedSocketOperationErrorThe operation is not supported.
QLocalSocket::OperationErrorThe operation is not valid in the current state.
QLocalSocket::UnknownSocketErrorAn unidentified error occurred.

enum QLocalSocket::LocalSocketState

Describes the state of a local socket.

ConstantDescription
QLocalSocket::UnconnectedStateThe socket is not connected.
QLocalSocket::ConnectingStateThe socket is connecting.
QLocalSocket::ConnectedStateThe socket is connected.
QLocalSocket::ClosingStateThe socket is closing and flushing pending writes.

Member Function Documentation

[explicit] QLocalSocket::QLocalSocket(QObject *parent = nullptr)

Constructs a local socket with the given parent.

[override] QLocalSocket::~QLocalSocket()

Destroys the socket, closing the connection if necessary.

void QLocalSocket::abort()

Closes the connection at once, discarding pending writes.

See also disconnectFromServer().

[override] qint64 QLocalSocket::bytesAvailable() const

Reimplements QIODevice::bytesAvailable().

[override] qint64 QLocalSocket::bytesToWrite() const

Reimplements QIODevice::bytesToWrite().

[override] bool QLocalSocket::canReadLine() const

Reimplements QIODevice::canReadLine().

[override] void QLocalSocket::close()

Reimplements QIODevice::close().

[signal] void QLocalSocket::connected()

This signal is emitted once the connection is established.

void QLocalSocket::connectToServer(OpenMode openMode = ReadWrite)

Connects to the server set with setServerName(), opening the socket with openMode.

See also setServerName() and connected().

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

This is an overloaded function.

Sets the server name to name and connects, opening the socket with openMode.

[signal] void QLocalSocket::disconnected()

This signal is emitted when the socket has disconnected.

void QLocalSocket::disconnectFromServer()

Disconnects after pending data has been written.

See also abort() and disconnected().

LocalSocketError QLocalSocket::error() const

Returns the last error.

See also errorOccurred().

[signal] void QLocalSocket::errorOccurred(QLocalSocket::LocalSocketError socketError)

This signal is emitted when the error socketError occurs.

bool QLocalSocket::flush()

Writes as much pending data as possible without blocking. Returns true if any data was written.

See also waitForBytesWritten().

QString QLocalSocket::fullServerName() const

Returns the path of the socket connected to.

See also serverName().

[override] bool QLocalSocket::isSequential() const

Reimplements QIODevice::isSequential().

bool QLocalSocket::isValid() const

Returns true if the socket is connected.

See also state().

qint64 QLocalSocket::readBufferSize() const

Returns the read buffer limit in bytes; 0 means unlimited.

See also setReadBufferSize().

[override, protected] qint64 QLocalSocket::readData(char *data, qint64 maxSize)

Reimplements QIODevice::readData().

QString QLocalSocket::serverName() const

Returns the server name given to connectToServer().

See also fullServerName().

void QLocalSocket::setReadBufferSize(qint64 size)

Limits the read buffer to size bytes; 0 means unlimited. When it is full, the sender is held back until data is read.

See also readBufferSize().

void QLocalSocket::setServerName(const QString &name)

Sets the name of the server to connect to, to name.

See also serverName().

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

Adopts the connected descriptor socketDescriptor, with the state socketState and open mode openMode. Returns false if the descriptor is invalid.

See also socketDescriptor().

qintptr QLocalSocket::socketDescriptor() const

Returns the native descriptor, or -1 if there is none.

See also setSocketDescriptor().

LocalSocketState QLocalSocket::state() const

Returns the socket's state.

See also stateChanged().

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

This signal is emitted when the state changes to socketState.

[override] bool QLocalSocket::waitForBytesWritten(int msecs = 30000)

Reimplements QIODevice::waitForBytesWritten().

Waits up to msecs milliseconds until pending data has been written. A negative value waits forever. Returns true if all of it was written.

See also flush().

bool QLocalSocket::waitForConnected(int msecs = 30000)

Returns true if the socket is connected. Connecting finishes inside connectToServer(), so this never waits; msecs is ignored.

See also connectToServer().

bool QLocalSocket::waitForDisconnected(int msecs = 30000)

Waits up to msecs milliseconds for the socket to disconnect. A negative value waits forever. Returns true if it disconnected.

See also disconnectFromServer().

[override] bool QLocalSocket::waitForReadyRead(int msecs = 30000)

Reimplements QIODevice::waitForReadyRead().

Waits up to msecs milliseconds for data to read, handling it as the event loop would. A negative value waits forever. Returns true if data arrived.

See also readyRead().

[override, protected] qint64 QLocalSocket::writeData(const char *data, qint64 maxSize)

Reimplements QIODevice::writeData().