Qt 4.8
Public Types | Signals | Public Functions | Protected Functions | List of all members
QLocalSocket Class Reference

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

#include <qlocalsocket.h>

Inheritance diagram for QLocalSocket:
QIODevice QObject

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
}
 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...
 
- Public Types inherited from QIODevice
enum  OpenModeFlag {
  NotOpen = 0x0000, ReadOnly = 0x0001, WriteOnly = 0x0002, ReadWrite = ReadOnly | WriteOnly,
  Append = 0x0004, Truncate = 0x0008, Text = 0x0010, Unbuffered = 0x0020
}
 This enum is used with 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. More...
 
void disconnected ()
 This signal is emitted when the socket has been disconnected. More...
 
void error (QLocalSocket::LocalSocketError socketError)
 This signal is emitted after an error occurred. More...
 
void stateChanged (QLocalSocket::LocalSocketState socketState)
 This signal is emitted whenever QLocalSocket's state changes. More...
 
- Signals inherited from QIODevice
void aboutToClose ()
 This signal is emitted when the device is about to close. More...
 
void bytesWritten (qint64 bytes)
 This signal is emitted every time a payload of data has been written to the device. More...
 
void readChannelFinished ()
 This signal is emitted when the input (reading) stream is closed in this device. More...
 
void readyRead ()
 This signal is emitted once every time new data is available for reading from the device. More...
 
- 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...
 

Public Functions

void abort ()
 Aborts the current connection and resets the socket. More...
 
virtual qint64 bytesAvailable () const
 Reimplemented Function More...
 
virtual qint64 bytesToWrite () const
 Reimplemented Function More...
 
virtual bool canReadLine () const
 Reimplemented Function More...
 
virtual void close ()
 Reimplemented Function More...
 
void connectToServer (const QString &name, OpenMode openMode=ReadWrite)
 Attempts to make a connection to name. More...
 
void disconnectFromServer ()
 Attempts to close the socket. More...
 
LocalSocketError error () const
 Returns the type of error that last occurred. More...
 
bool flush ()
 This function writes as much as possible from the internal write buffer to the socket, without blocking. More...
 
QString fullServerName () const
 Returns the server path that the socket is connected to. More...
 
virtual bool isSequential () const
 Reimplemented Function More...
 
bool isValid () const
 Returns true if the socket is valid and ready for use; otherwise returns false. More...
 
 QLocalSocket (QObject *parent=0)
 Creates a new local socket. More...
 
qint64 readBufferSize () const
 Returns the size of the internal read buffer. More...
 
QString serverName () const
 Returns the name of the peer as specified by connectToServer(), or an empty QString if connectToServer() has not been called or it failed. More...
 
void setReadBufferSize (qint64 size)
 Sets the size of QLocalSocket's internal read buffer to be size bytes. More...
 
bool setSocketDescriptor (quintptr socketDescriptor, LocalSocketState socketState=ConnectedState, OpenMode openMode=ReadWrite)
 
quintptr socketDescriptor () const
 Returns the native socket descriptor of the QLocalSocket object if this is available; otherwise returns -1. More...
 
LocalSocketState state () const
 Returns the state of the socket. More...
 
bool waitForBytesWritten (int msecs=30000)
 Reimplemented Function More...
 
bool waitForConnected (int msecs=30000)
 Waits until the socket is connected, up to msecs milliseconds. More...
 
bool waitForDisconnected (int msecs=30000)
 Waits until the socket has disconnected, up to msecs milliseconds. More...
 
bool waitForReadyRead (int msecs=30000)
 This function blocks until data is available for reading and the QIODevice::readyRead() signal has been emitted. More...
 
 ~QLocalSocket ()
 Destroys the socket, closing the connection if necessary. More...
 
- Public Functions inherited from QIODevice
virtual bool atEnd () const
 Returns true if the current read and write position is at the end of the device (i.e. More...
 
QString errorString () const
 Returns a human-readable description of the last device error that occurred. More...
 
bool getChar (char *c)
 Reads one character from the device and stores it in c. More...
 
bool isOpen () const
 Returns true if the device is open; otherwise returns false. More...
 
bool isReadable () const
 Returns true if data can be read from the device; otherwise returns false. More...
 
bool isTextModeEnabled () const
 Returns true if the Text flag is enabled; otherwise returns false. More...
 
bool isWritable () const
 Returns true if data can be written to the device; otherwise returns false. More...
 
virtual bool open (OpenMode mode)
 Opens the device and sets its OpenMode to mode. More...
 
OpenMode openMode () const
 Returns the mode in which the device has been opened; i.e. More...
 
qint64 peek (char *data, qint64 maxlen)
 Reads at most maxSize bytes from the device into data, without side effects (i. More...
 
QByteArray peek (qint64 maxlen)
 Peeks at most maxSize bytes from the device, returning the data peeked as a QByteArray. More...
 
virtual qint64 pos () const
 For random-access devices, this function returns the position that data is written to or read from. More...
 
bool putChar (char c)
 Writes the character c to the device. More...
 
 QIODevice ()
 Constructs a QIODevice object. More...
 
 QIODevice (QObject *parent)
 Constructs a QIODevice object with the given parent. More...
 
qint64 read (char *data, qint64 maxlen)
 Reads at most maxSize bytes from the device into data, and returns the number of bytes read. More...
 
QByteArray read (qint64 maxlen)
 Reads at most maxSize bytes from the device, and returns the data read as a QByteArray. More...
 
QByteArray readAll ()
 Reads all available data from the device, and returns it as a QByteArray. More...
 
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. More...
 
QByteArray readLine (qint64 maxlen=0)
 Reads a line from the device, but no more than maxSize characters, and returns the result as a QByteArray. More...
 
virtual bool reset ()
 Seeks to the start of input for random-access devices. More...
 
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. More...
 
void setTextModeEnabled (bool enabled)
 If enabled is true, this function sets the Text flag on the device; otherwise the Text flag is removed. More...
 
virtual qint64 size () const
 For open random-access devices, this function returns the size of the device. More...
 
void ungetChar (char c)
 Puts the character c back into the device, and decrements the current position unless the position is 0. More...
 
qint64 write (const char *data, qint64 len)
 Writes at most maxSize bytes of data from data to the device. More...
 
qint64 write (const char *data)
 Writes data from a zero-terminated string of 8-bit characters to the device. More...
 
qint64 write (const QByteArray &data)
 Writes the content of byteArray to the device. More...
 
virtual ~QIODevice ()
 The destructor is virtual, and QIODevice is an abstract base class. More...
 
- 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...
 

Protected Functions

virtual qint64 readData (char *, qint64)
 Reimplemented Function More...
 
virtual qint64 writeData (const char *, qint64)
 Reimplemented Function More...
 
- Protected Functions inherited from QIODevice
 QIODevice (QIODevicePrivate &dd, QObject *parent=0)
 
virtual qint64 readLineData (char *data, qint64 maxlen)
 Reads up to maxSize characters into data and returns the number of characters read. More...
 
void setErrorString (const QString &errorString)
 Sets the human readable description of the last device error that occurred to str. More...
 
void setOpenMode (OpenMode openMode)
 Sets the OpenMode of the device to openMode. More...
 
- 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...
 

Additional Inherited Members

- Public Slots inherited from QObject
void deleteLater ()
 Schedules this object for deletion. More...
 
- 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 Variables inherited from QObject
QScopedPointer< QObjectDatad_ptr
 
- Static Protected Variables inherited from QObject
static const QMetaObject staticQtMetaObject
 

Detailed Description

The QLocalSocket class provides a local socket.

Since
4.4

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

If an error occurs, socketError() 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.

Note that this feature is not supported on versions of Windows earlier than Windows XP.

See also
QLocalServer

Definition at line 58 of file qlocalsocket.h.

Enumerations

◆ LocalSocketError

The LocalServerError enumeration represents the errors that can occur.

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

  • ConnectionRefusedError The connection was refused by the peer (or timed out).
  • 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.
  • ServerNotFoundError The local socket name was not found.
  • SocketAccessError The socket operation failed because the application lacked the required privileges.
  • SocketResourceError The local system ran out of resources (e.g., too many sockets).
  • SocketTimeoutError The socket operation timed out.
  • DatagramTooLargeError The datagram was larger than the operating system's limit (which can be as low as 8192 bytes).
  • ConnectionError An error occurred with the connection.
  • UnsupportedSocketOperationError The requested socket operation is not supported by the local operating system.
  • UnknownSocketError An unidentified error occurred.
Enumerator
ConnectionRefusedError 
PeerClosedError 
ServerNotFoundError 
SocketAccessError 
SocketResourceError 
SocketTimeoutError 
DatagramTooLargeError 
ConnectionError 
UnsupportedSocketOperationError 
UnknownSocketError 

Definition at line 64 of file qlocalsocket.h.

65  {
76  };

◆ LocalSocketState

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

See also
QLocalSocket::state()
  • UnconnectedState The socket is not connected.
  • ConnectingState The socket has started establishing a connection.
  • ConnectedState A connection is established.
  • ClosingState The socket is about to close (data may still be waiting to be written).
Enumerator
UnconnectedState 
ConnectingState 
ConnectedState 
ClosingState 

Definition at line 78 of file qlocalsocket.h.

Constructors and Destructors

◆ QLocalSocket()

QLocalSocket::QLocalSocket ( QObject parent = 0)

Creates a new local socket.

The parent argument is passed to QObject's constructor.

Definition at line 390 of file qlocalsocket.cpp.

391  : QIODevice(*new QLocalSocketPrivate, parent)
392 {
393  Q_D(QLocalSocket);
394  d->init();
395 }
double d
Definition: qnumeric_p.h:62
#define Q_D(Class)
Definition: qglobal.h:2482
The QLocalSocket class provides a local socket.
Definition: qlocalsocket.h:58
QIODevice()
Constructs a QIODevice object.
Definition: qiodevice.cpp:390

◆ ~QLocalSocket()

QLocalSocket::~QLocalSocket ( )

Destroys the socket, closing the connection if necessary.

Definition at line 400 of file qlocalsocket.cpp.

401 {
402  close();
403 #if !defined(Q_OS_WIN) && !defined(QT_LOCALSOCKET_TCP)
404  Q_D(QLocalSocket);
405  d->unixSocket.setParent(0);
406 #endif
407 }
double d
Definition: qnumeric_p.h:62
virtual void close()
Reimplemented Function
#define Q_D(Class)
Definition: qglobal.h:2482
The QLocalSocket class provides a local socket.
Definition: qlocalsocket.h:58

Functions

◆ 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 308 of file qlocalsocket_tcp.cpp.

Referenced by QLocalSocketPrivate::completeAsyncRead().

309 {
310  Q_D(QLocalSocket);
311  d->tcpSocket->abort();
312 }
double d
Definition: qnumeric_p.h:62
#define Q_D(Class)
Definition: qglobal.h:2482
The QLocalSocket class provides a local socket.
Definition: qlocalsocket.h:58

◆ bytesAvailable()

qint64 QLocalSocket::bytesAvailable ( ) const
virtual

Reimplemented Function

Reimplemented from QIODevice.

Definition at line 314 of file qlocalsocket_tcp.cpp.

Referenced by QLocalSocketPrivate::_q_pipeClosed().

315 {
316  Q_D(const QLocalSocket);
317  return QIODevice::bytesAvailable() + d->tcpSocket->bytesAvailable();
318 }
double d
Definition: qnumeric_p.h:62
#define Q_D(Class)
Definition: qglobal.h:2482
The QLocalSocket class provides a local socket.
Definition: qlocalsocket.h:58
virtual qint64 bytesAvailable() const
Returns the number of bytes that are available for reading.
Definition: qiodevice.cpp:752

◆ bytesToWrite()

qint64 QLocalSocket::bytesToWrite ( ) const
virtual

Reimplemented Function

Reimplemented from QIODevice.

Definition at line 320 of file qlocalsocket_tcp.cpp.

Referenced by QLocalSocketPrivate::_q_pipeClosed().

321 {
322  Q_D(const QLocalSocket);
323  return d->tcpSocket->bytesToWrite();
324 }
double d
Definition: qnumeric_p.h:62
#define Q_D(Class)
Definition: qglobal.h:2482
The QLocalSocket class provides a local socket.
Definition: qlocalsocket.h:58

◆ canReadLine()

bool QLocalSocket::canReadLine ( ) const
virtual

Reimplemented Function

Reimplemented from QIODevice.

Definition at line 326 of file qlocalsocket_tcp.cpp.

Referenced by QLocalSocketPrivate::_q_pipeClosed().

327 {
328  Q_D(const QLocalSocket);
329  return QIODevice::canReadLine() || d->tcpSocket->canReadLine();
330 }
double d
Definition: qnumeric_p.h:62
#define Q_D(Class)
Definition: qglobal.h:2482
The QLocalSocket class provides a local socket.
Definition: qlocalsocket.h:58
virtual bool canReadLine() const
Returns true if a complete line of data can be read from the device; otherwise returns false...
Definition: qiodevice.cpp:1330

◆ close()

void QLocalSocket::close ( )
virtual

Reimplemented Function

Reimplemented from QIODevice.

Definition at line 332 of file qlocalsocket_tcp.cpp.

Referenced by QLocalSocketPrivate::_q_pipeClosed(), and ~QLocalSocket().

333 {
334  Q_D(QLocalSocket);
335  d->tcpSocket->close();
336  d->serverName.clear();
337  d->fullServerName.clear();
339 }
double d
Definition: qnumeric_p.h:62
virtual void close()
First emits aboutToClose(), then closes the device and sets its OpenMode to NotOpen.
Definition: qiodevice.cpp:590
#define Q_D(Class)
Definition: qglobal.h:2482
The QLocalSocket class provides a local socket.
Definition: qlocalsocket.h:58

◆ 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()

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

Attempts to make a connection to name.

The socket is opened in the given openMode and first enters ConnectingState. It then attempts to connect to the address or addresses returned by the lookup. Finally, if a connection is established, QLocalSocket enters ConnectedState and emits connected().

At any point, the socket can emit error() to signal that an error occurred.

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

Definition at line 214 of file qlocalsocket_tcp.cpp.

Referenced by QLocalSocketPrivate::destroyPipeHandles().

215 {
216  Q_D(QLocalSocket);
217  if (state() == ConnectedState
218  || state() == ConnectingState)
219  return;
220 
221  d->errorString.clear();
222  d->state = ConnectingState;
223  emit stateChanged(d->state);
224 
225  if (name.isEmpty()) {
226  d->errorOccurred(ServerNotFoundError,
227  QLatin1String("QLocalSocket::connectToServer"));
228  return;
229  }
230 
231  d->serverName = name;
232  const QLatin1String prefix("QLocalServer/");
233  if (name.startsWith(prefix))
234  d->fullServerName = name;
235  else
236  d->fullServerName = prefix + name;
237 
238  QSettings settings(QLatin1String("Trolltech"), QLatin1String("Qt"));
239  bool ok;
240  const quint16 port = settings.value(d->fullServerName).toUInt(&ok);
241  if (!ok) {
242  d->errorOccurred(ServerNotFoundError,
243  QLatin1String("QLocalSocket::connectToServer"));
244  return;
245  }
246  d->tcpSocket->connectToHost(QHostAddress::LocalHost, port, openMode);
248 }
double d
Definition: qnumeric_p.h:62
QVariant value(const QString &key, const QVariant &defaultValue=QVariant()) const
Returns the value for setting key.
Definition: qsettings.cpp:3460
The QSettings class provides persistent platform-independent application settings.
Definition: qsettings.h:73
bool startsWith(const QString &s, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Returns true if the string starts with s; otherwise returns false.
Definition: qstring.cpp:3734
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
LocalSocketState state() const
Returns the state of the socket.
#define Q_D(Class)
Definition: qglobal.h:2482
The QLocalSocket class provides a local socket.
Definition: qlocalsocket.h:58
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition: qstring.h:704
const char * name
#define emit
Definition: qobjectdefs.h:76
unsigned short quint16
Definition: qglobal.h:936
The QLatin1String class provides a thin wrapper around an US-ASCII/Latin-1 encoded string literal...
Definition: qstring.h:654
OpenMode openMode() const
Returns the mode in which the device has been opened; i.e.
Definition: qiodevice.cpp:465
uint toUInt(bool *ok=0) const
Returns the variant as an unsigned int if the variant has type() UInt , Bool , ByteArray ...
Definition: qvariant.cpp:2644
virtual bool open(OpenMode mode)
Opens the device and sets its OpenMode to mode.
Definition: qiodevice.cpp:570
void stateChanged(QLocalSocket::LocalSocketState socketState)
This signal is emitted whenever QLocalSocket&#39;s state changes.

◆ 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 disconnectedFromServer() signal.

See also
connectToServer()

Definition at line 353 of file qlocalsocket_tcp.cpp.

Referenced by QLocalSocketPrivate::_q_pipeClosed().

354 {
355  Q_D(QLocalSocket);
356  d->tcpSocket->disconnectFromHost();
357 }
double d
Definition: qnumeric_p.h:62
#define Q_D(Class)
Definition: qglobal.h:2482
The QLocalSocket class provides a local socket.
Definition: qlocalsocket.h:58

◆ error() [1/2]

QLocalSocket::LocalSocketError QLocalSocket::error ( ) const

Returns the type of error that last occurred.

See also
state(), errorString()

Definition at line 359 of file qlocalsocket_tcp.cpp.

Referenced by QLocalSocketPrivate::_q_pipeClosed().

360 {
361  Q_D(const QLocalSocket);
362  switch (d->tcpSocket->error()) {
383  default:
384 #if defined QLOCALSOCKET_DEBUG
385  qWarning() << "QLocalSocket error not handled:" << d->tcpSocket->error();
386 #endif
387  break;
388  }
389  return UnknownSocketError;
390 }
double d
Definition: qnumeric_p.h:62
#define Q_D(Class)
Definition: qglobal.h:2482
The QLocalSocket class provides a local socket.
Definition: qlocalsocket.h:58
Q_CORE_EXPORT void qWarning(const char *,...)

◆ error [2/2]

void QLocalSocket::error ( QLocalSocket::LocalSocketError  socketError)
signal

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}

◆ 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 347 of file qlocalsocket_tcp.cpp.

Referenced by QLocalSocketPrivate::_q_pipeClosed().

348 {
349  Q_D(QLocalSocket);
350  return d->tcpSocket->flush();
351 }
double d
Definition: qnumeric_p.h:62
#define Q_D(Class)
Definition: qglobal.h:2482
The QLocalSocket class provides a local socket.
Definition: qlocalsocket.h:58

◆ 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 429 of file qlocalsocket.cpp.

430 {
431  Q_D(const QLocalSocket);
432  return d->fullServerName;
433 }
double d
Definition: qnumeric_p.h:62
#define Q_D(Class)
Definition: qglobal.h:2482
The QLocalSocket class provides a local socket.
Definition: qlocalsocket.h:58

◆ isSequential()

bool QLocalSocket::isSequential ( ) const
virtual

Reimplemented Function

Reimplemented from QIODevice.

Definition at line 448 of file qlocalsocket.cpp.

449 {
450  return true;
451 }

◆ 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 392 of file qlocalsocket_tcp.cpp.

Referenced by QLocalSocketPrivate::_q_emitReadyRead().

393 {
394  Q_D(const QLocalSocket);
395  return d->tcpSocket->isValid();
396 }
double d
Definition: qnumeric_p.h:62
#define Q_D(Class)
Definition: qglobal.h:2482
The QLocalSocket class provides a local socket.
Definition: qlocalsocket.h:58

◆ 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 398 of file qlocalsocket_tcp.cpp.

Referenced by QLocalSocketPrivate::_q_emitReadyRead().

399 {
400  Q_D(const QLocalSocket);
401  return d->tcpSocket->readBufferSize();
402 }
double d
Definition: qnumeric_p.h:62
#define Q_D(Class)
Definition: qglobal.h:2482
The QLocalSocket class provides a local socket.
Definition: qlocalsocket.h:58

◆ readData()

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

Reimplemented Function

Implements QIODevice.

Definition at line 296 of file qlocalsocket_tcp.cpp.

Referenced by QLocalSocketPrivate::destroyPipeHandles().

297 {
298  Q_D(QLocalSocket);
299  return d->tcpSocket->readData(data, c);
300 }
double d
Definition: qnumeric_p.h:62
unsigned char c[8]
Definition: qnumeric_p.h:62
#define Q_D(Class)
Definition: qglobal.h:2482
The QLocalSocket class provides a local socket.
Definition: qlocalsocket.h:58
static const char * data(const QByteArray &arr)

◆ serverName()

QString QLocalSocket::serverName ( ) const

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

See also
connectToServer(), fullServerName()

Definition at line 416 of file qlocalsocket.cpp.

417 {
418  Q_D(const QLocalSocket);
419  return d->serverName;
420 }
double d
Definition: qnumeric_p.h:62
#define Q_D(Class)
Definition: qglobal.h:2482
The QLocalSocket class provides a local socket.
Definition: qlocalsocket.h:58

◆ 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 404 of file qlocalsocket_tcp.cpp.

Referenced by QLocalSocketPrivate::_q_emitReadyRead().

405 {
406  Q_D(QLocalSocket);
407  d->tcpSocket->setReadBufferSize(size);
408 }
double d
Definition: qnumeric_p.h:62
virtual qint64 size() const
For open random-access devices, this function returns the size of the device.
Definition: qiodevice.cpp:642
#define Q_D(Class)
Definition: qglobal.h:2482
The QLocalSocket class provides a local socket.
Definition: qlocalsocket.h:58

◆ setSocketDescriptor()

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

Definition at line 250 of file qlocalsocket_tcp.cpp.

Referenced by QLocalSocketPrivate::_q_pipeClosed(), and QLocalServer::incomingConnection().

252 {
253  Q_D(QLocalSocket);
255  switch (socketState) {
256  case ConnectingState:
257  newSocketState = QAbstractSocket::ConnectingState;
258  break;
259  case ConnectedState:
260  newSocketState = QAbstractSocket::ConnectedState;
261  break;
262  case ClosingState:
263  newSocketState = QAbstractSocket::ClosingState;
264  break;
265  case UnconnectedState:
266  newSocketState = QAbstractSocket::UnconnectedState;
267  break;
268  }
270  d->state = socketState;
271 
272  // Is our parent a localServer? Then it wants us to use its remote socket.
273  QLocalServer* localServer = qobject_cast<QLocalServer*>( parent() );
274  if (localServer) {
275  foreach (QObject* child, localServer->children()) {
276  QTcpSocket* childTcpSocket = qobject_cast<QTcpSocket*>(child);
277  if (childTcpSocket && childTcpSocket->socketDescriptor() == socketDescriptor) {
278  d->setSocket( static_cast<QLocalUnixSocket*>(childTcpSocket) );
279  return true;
280  }
281  }
282  }
283 
284  // We couldn't find the socket in the children list of our server.
285  // So it might be that the user wants to set a socket descriptor.
286  return d->tcpSocket->setSocketDescriptor(socketDescriptor,
287  newSocketState, openMode);
288 }
double d
Definition: qnumeric_p.h:62
quintptr socketDescriptor() const
Returns the native socket descriptor of the QLocalSocket object if this is available; otherwise retur...
int socketDescriptor() const
Returns the native socket descriptor of the QAbstractSocket object if this is available; otherwise re...
T * qobject_cast(QObject *object)
Definition: qobject.h:375
The QObject class is the base class of all Qt objects.
Definition: qobject.h:111
#define Q_D(Class)
Definition: qglobal.h:2482
The QLocalSocket class provides a local socket.
Definition: qlocalsocket.h:58
SocketState
This enum describes the different states in which a socket can be.
The QLocalServer class provides a local socket based server.
Definition: qlocalserver.h:58
The QTcpSocket class provides a TCP socket.
Definition: qtcpsocket.h:56
OpenMode openMode() const
Returns the mode in which the device has been opened; i.e.
Definition: qiodevice.cpp:465
QObject * parent() const
Returns a pointer to the parent object.
Definition: qobject.h:273
const QObjectList & children() const
Returns a list of child objects.
Definition: qobject.h:197
virtual bool open(OpenMode mode)
Opens the device and sets its OpenMode to mode.
Definition: qiodevice.cpp:570

◆ socketDescriptor()

quintptr 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.

See also
setSocketDescriptor()

Definition at line 290 of file qlocalsocket_tcp.cpp.

Referenced by QLocalSocketPrivate::_q_emitReadyRead().

291 {
292  Q_D(const QLocalSocket);
293  return d->tcpSocket->socketDescriptor();
294 }
double d
Definition: qnumeric_p.h:62
#define Q_D(Class)
Definition: qglobal.h:2482
The QLocalSocket class provides a local socket.
Definition: qlocalsocket.h:58

◆ state()

QLocalSocket::LocalSocketState QLocalSocket::state ( ) const

Returns the state of the socket.

See also
error()

Definition at line 440 of file qlocalsocket.cpp.

441 {
442  Q_D(const QLocalSocket);
443  return d->state;
444 }
double d
Definition: qnumeric_p.h:62
#define Q_D(Class)
Definition: qglobal.h:2482
The QLocalSocket class provides a local socket.
Definition: qlocalsocket.h:58

◆ 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}

◆ waitForBytesWritten()

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

Reimplemented Function

Reimplemented from QIODevice.

Definition at line 341 of file qlocalsocket_tcp.cpp.

Referenced by QLocalSocketPrivate::_q_emitReadyRead().

342 {
343  Q_D(QLocalSocket);
344  return d->tcpSocket->waitForBytesWritten(msecs);
345 }
double d
Definition: qnumeric_p.h:62
#define Q_D(Class)
Definition: qglobal.h:2482
The QLocalSocket class provides a local socket.
Definition: qlocalsocket.h:58

◆ 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");
if (socket->waitForConnected(1000))
qDebug("Connected!");

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

See also
connectToServer(), connected()

Definition at line 410 of file qlocalsocket_tcp.cpp.

Referenced by QLocalSocketPrivate::_q_emitReadyRead().

411 {
412  Q_D(QLocalSocket);
413  if (state() != ConnectingState)
414  return (state() == ConnectedState);
415 
416  return d->tcpSocket->waitForConnected(msec);
417 }
double d
Definition: qnumeric_p.h:62
LocalSocketState state() const
Returns the state of the socket.
#define Q_D(Class)
Definition: qglobal.h:2482
The QLocalSocket class provides a local socket.
Definition: qlocalsocket.h:58

◆ waitForDisconnected()

bool QLocalSocket::waitForDisconnected ( int  msecs = 30000)

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

If the connection has been disconnected, 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 closed:

socket->disconnectFromServer();
if (socket->waitForDisconnected(1000))
qDebug("Disconnected!");

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

See also
disconnectFromServer(), close()

Definition at line 419 of file qlocalsocket_tcp.cpp.

Referenced by QLocalSocketPrivate::_q_emitReadyRead().

420 {
421  Q_D(QLocalSocket);
422  if (state() == UnconnectedState) {
423  qWarning() << "QLocalSocket::waitForDisconnected() is not allowed in UnconnectedState";
424  return false;
425  }
426  return (d->tcpSocket->waitForDisconnected(msecs));
427 }
double d
Definition: qnumeric_p.h:62
LocalSocketState state() const
Returns the state of the socket.
#define Q_D(Class)
Definition: qglobal.h:2482
The QLocalSocket class provides a local socket.
Definition: qlocalsocket.h:58
Q_CORE_EXPORT void qWarning(const char *,...)

◆ waitForReadyRead()

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

This function blocks until data is available for reading and the 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 429 of file qlocalsocket_tcp.cpp.

Referenced by QLocalSocketPrivate::_q_emitReadyRead().

430 {
431  Q_D(QLocalSocket);
433  return false;
434  return (d->tcpSocket->waitForReadyRead(msecs));
435 }
double d
Definition: qnumeric_p.h:62
LocalSocketState state() const
Returns the state of the socket.
#define Q_D(Class)
Definition: qglobal.h:2482
The QLocalSocket class provides a local socket.
Definition: qlocalsocket.h:58

◆ writeData()

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

Reimplemented Function

Implements QIODevice.

Definition at line 302 of file qlocalsocket_tcp.cpp.

Referenced by QLocalSocketPrivate::completeAsyncRead().

303 {
304  Q_D(QLocalSocket);
305  return d->tcpSocket->writeData(data, c);
306 }
double d
Definition: qnumeric_p.h:62
unsigned char c[8]
Definition: qnumeric_p.h:62
#define Q_D(Class)
Definition: qglobal.h:2482
The QLocalSocket class provides a local socket.
Definition: qlocalsocket.h:58
static const char * data(const QByteArray &arr)

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