QLocalSocket

Inheritance diagram of QLocalSocket

Synopsis

Functions

Signals

Detailed Description

The PySide.QtNetwork.QLocalSocket class provides a local socket.

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 PySide.QtCore.QIODevice.errorString() can be called to get a human readable description of what happened.

Although PySide.QtNetwork.QLocalSocket is designed for use with an event loop, it’s possible to use it without one. In that case, you must use PySide.QtNetwork.QLocalSocket.waitForConnected() , PySide.QtNetwork.QLocalSocket.waitForReadyRead() , PySide.QtNetwork.QLocalSocket.waitForBytesWritten() , and PySide.QtNetwork.QLocalSocket.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.

class PySide.QtNetwork.QLocalSocket([parent=None])
Parameters:parentPySide.QtCore.QObject

Creates a new local socket. The parent argument is passed to PySide.QtCore.QObject ‘s constructor.

PySide.QtNetwork.QLocalSocket.LocalSocketState

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

Constant Description
QLocalSocket.UnconnectedState The socket is not connected.
QLocalSocket.ConnectingState The socket has started establishing a connection.
QLocalSocket.ConnectedState A connection is established.
QLocalSocket.ClosingState The socket is about to close (data may still be waiting to be written).
PySide.QtNetwork.QLocalSocket.LocalSocketError

The LocalServerError enumeration represents the errors that can occur. The most recent error can be retrieved through a call to QLocalSocket.error() .

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

Aborts the current connection and resets the socket. Unlike PySide.QtNetwork.QLocalSocket.disconnectFromServer() , this function immediately closes the socket, clearing any pending data in the write buffer.

See also

PySide.QtNetwork.QLocalSocket.disconnectFromServer() PySide.QtNetwork.QLocalSocket.close()

PySide.QtNetwork.QLocalSocket.connectToServer(name[, openMode=QIODevice.ReadWrite])
Parameters:
  • name – unicode
  • openModePySide.QtCore.QIODevice.OpenMode
PySide.QtNetwork.QLocalSocket.connected()
PySide.QtNetwork.QLocalSocket.disconnectFromServer()

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

PySide.QtNetwork.QLocalSocket.disconnected()
PySide.QtNetwork.QLocalSocket.error(socketError)
Parameters:socketErrorPySide.QtNetwork.QLocalSocket.LocalSocketError
PySide.QtNetwork.QLocalSocket.error()
Return type:PySide.QtNetwork.QLocalSocket.LocalSocketError

Returns the type of error that last occurred.

See also

PySide.QtNetwork.QLocalSocket.state() PySide.QtCore.QIODevice.errorString()

PySide.QtNetwork.QLocalSocket.flush()
Return type:PySide.QtCore.bool

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 PySide.QtNetwork.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 PySide.QtNetwork.QLocalSocket will start sending data automatically once control goes back to the event loop. In the absence of an event loop, call PySide.QtNetwork.QLocalSocket.waitForBytesWritten() instead.

See also

PySide.QtCore.QIODevice.write() PySide.QtNetwork.QLocalSocket.waitForBytesWritten()

PySide.QtNetwork.QLocalSocket.fullServerName()
Return type:unicode

Returns the server path that the socket is connected to.

Note

The return value of this function is platform specific.

PySide.QtNetwork.QLocalSocket.isValid()
Return type:PySide.QtCore.bool

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.

PySide.QtNetwork.QLocalSocket.readBufferSize()
Return type:PySide.QtCore.qint64

Returns the size of the internal read buffer. This limits the amount of data that the client can receive before you call PySide.QtCore.QIODevice.read() or PySide.QtCore.QIODevice.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

PySide.QtNetwork.QLocalSocket.setReadBufferSize() PySide.QtCore.QIODevice.read()

PySide.QtNetwork.QLocalSocket.serverName()
Return type:unicode

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

PySide.QtNetwork.QLocalSocket.setReadBufferSize(size)
Parameters:sizePySide.QtCore.qint64

Sets the size of PySide.QtNetwork.QLocalSocket ‘s internal read buffer to be size bytes.

If the buffer size is limited to a certain size, PySide.QtNetwork.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

PySide.QtNetwork.QLocalSocket.readBufferSize() PySide.QtCore.QIODevice.read()

PySide.QtNetwork.QLocalSocket.state()
Return type:PySide.QtNetwork.QLocalSocket.LocalSocketState

Returns the state of the socket.

PySide.QtNetwork.QLocalSocket.stateChanged(socketState)
Parameters:socketStatePySide.QtNetwork.QLocalSocket.LocalSocketState
PySide.QtNetwork.QLocalSocket.waitForConnected([msecs=30000])
Parameters:msecsPySide.QtCore.int
Return type:PySide.QtCore.bool

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 PySide.QtNetwork.QLocalSocket.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))
    print("Connected!")

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

PySide.QtNetwork.QLocalSocket.waitForDisconnected([msecs=30000])
Parameters:msecsPySide.QtCore.int
Return type:PySide.QtCore.bool

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 PySide.QtNetwork.QLocalSocket.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))
    print("Disconnected!")

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

See also

PySide.QtNetwork.QLocalSocket.disconnectFromServer() PySide.QtNetwork.QLocalSocket.close()