内容表

上一话题

QLocalServer

下一话题

QNetworkAccessManager

QLocalSocket

QLocalSocket class provides a local socket. 更多

Inheritance diagram of PySide2.QtNetwork.QLocalSocket

概要

函数

信号

详细描述

在 Windows 这是命名管道,在 Unix 这是本地域套接字。

若发生错误, error() returns the type of error, and errorString() can be called to get a human readable description of what happened.

尽管 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() ,和 waitForDisconnected() which blocks until the operation is complete or the timeout expires.

另请参阅

QLocalServer

class QLocalSocket ( [ parent=None ] )
param parent

QObject

创建新的本地套接字。 parent 自变量会被传递给 QObject ‘s constructor.

PySide2.QtNetwork.QLocalSocket. LocalSocketError

LocalServerError 枚举表示可能发生的错误。最近错误可以被检索透过调用 error() .

常量

描述

QLocalSocket.ConnectionRefusedError

连接被对等方拒绝 (或超时)。

QLocalSocket.PeerClosedError

远程套接字关闭连接。注意:客户端套接字 (即:此套接字) 将在远程关闭通知发送后被关闭。

QLocalSocket.ServerNotFoundError

本地套接字名未找到。

QLocalSocket.SocketAccessError

套接字操作失败,因为应用程序缺乏所需特权。

QLocalSocket.SocketResourceError

本地系统资源不足 (如:太多套接字)。

QLocalSocket.SocketTimeoutError

套接字操作超时。

QLocalSocket.DatagramTooLargeError

The datagram was larger than the operating system’s limit (which can be as low as 8192 bytes).

QLocalSocket.ConnectionError

连接发生错误。

QLocalSocket.UnsupportedSocketOperationError

本地操作系统不支持所请求的套接字操作。

QLocalSocket.OperationError

试图操作当套接字处于不准许状态时。

QLocalSocket.UnknownSocketError

发生无法识别的错误。

PySide2.QtNetwork.QLocalSocket. LocalSocketState

此枚举描述套接字可以处于的不同状态。

常量

描述

QLocalSocket.UnconnectedState

套接字未连接。

QLocalSocket.ConnectingState

套接字开始建立连接。

QLocalSocket.ConnectedState

已建立连接。

QLocalSocket.ClosingState

套接字即将关闭 (数据可能仍在等待被写入)。

另请参阅

state()

PySide2.QtNetwork.QLocalSocket. abort ( )

中止当前连接并重置套接字。不像 disconnectFromServer() , this function immediately closes the socket, clearing any pending data in the write buffer.

另请参阅

disconnectFromServer() close()

PySide2.QtNetwork.QLocalSocket. connectToServer ( [ openMode=QIODevice.ReadWrite ] )
参数

openMode OpenMode

试图连接到 serverName() . setServerName() must be called before you open the connection. Alternatively you can use (const QString &name, OpenMode openMode );

套接字被打开采用给定 openMode 并首先进入 ConnectingState 。若建立连接, QLocalSocket 进入 ConnectedState 并发射 connected() .

在调用此函数后,套接字可以发射 errorOccurred() to signal that an error occurred.

PySide2.QtNetwork.QLocalSocket. connectToServer ( name [ , openMode=QIODevice.ReadWrite ] )
参数
  • name – unicode

  • openMode OpenMode

这是重载函数。

设置服务器 name 并试图与它建立连接。

套接字被打开采用给定 openMode 并首先进入 ConnectingState 。若建立连接, QLocalSocket 进入 ConnectedState 并发射 connected() .

在调用此函数后,套接字可以发射 errorOccurred() to signal that an error occurred.

PySide2.QtNetwork.QLocalSocket. connected ( )
PySide2.QtNetwork.QLocalSocket. disconnectFromServer ( )

试图关闭套接字。若有等待写入的待决数据, QLocalSocket 将进入 ClosingState 并等待直到所有数据被写入。最终,它将进入 UnconnectedState 并发射 disconnectedFromServer() 信号。

另请参阅

connectToServer()

PySide2.QtNetwork.QLocalSocket. disconnected ( )
PySide2.QtNetwork.QLocalSocket. error ( socketError )
参数

socketError LocalSocketError

注意

此函数被弃用。

PySide2.QtNetwork.QLocalSocket. error ( )
返回类型

LocalSocketError

返回最后发生的错误类型。

另请参阅

state() errorString()

PySide2.QtNetwork.QLocalSocket. errorOccurred ( socketError )
参数

socketError LocalSocketError

PySide2.QtNetwork.QLocalSocket. flush ( )
返回类型

bool

此函数尽可能将内部写入缓冲不阻塞地写入套接字。若有写入任何数据,此函数返回 true ;否则 false 被返回。

调用此函数若需要 QLocalSocket 立即开始发送缓冲数据。成功写入的字节数从属操作系统。在大多数情况下,不需调用此函数,因为 QLocalSocket 将开始自动发送数据,一旦控制回到事件循环。若缺乏事件循环,调用 waitForBytesWritten() 代替。

另请参阅

write() waitForBytesWritten()

PySide2.QtNetwork.QLocalSocket. fullServerName ( )
返回类型

unicode

返回套接字连接到的服务器路径。

注意

此函数的返回值是特定于平台的。

PySide2.QtNetwork.QLocalSocket. isValid ( )
返回类型

bool

返回 true 若套接字是有效的且准备使用;否则返回 false .

注意

The socket’s state must be ConnectedState 在读写可以发生之前。

PySide2.QtNetwork.QLocalSocket. readBufferSize ( )
返回类型

qint64

返回内部读取缓冲尺寸。这限制客户端可以接收的数据量先于调用 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.

另请参阅

setReadBufferSize() read()

PySide2.QtNetwork.QLocalSocket. serverName ( )
返回类型

unicode

返回对等方名称,指定通过 setServerName() , or an empty QString if setServerName() has not been called or connectToServer() failed.

PySide2.QtNetwork.QLocalSocket. setReadBufferSize ( size )
参数

size qint64

设置尺寸为 QLocalSocket ‘s internal read buffer to be 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.

此选项是有用的若仅在某时间点读取数据 (如:在实时流应用程序中),或者,若希望保护套接字以免接收太多数据 (可能最终导致应用程序内存不足)。

另请参阅

readBufferSize() read()

PySide2.QtNetwork.QLocalSocket. setServerName ( name )
参数

name – unicode

设置 name 为要连接的对等方。在 Windows 名称是命名管道的名称;在 Unix 名称是本地域套接字的名称。

此函数必须被调用,当套接字未被连接时。

另请参阅

serverName()

PySide2.QtNetwork.QLocalSocket. setSocketDescriptor ( socketDescriptor [ , socketState=ConnectedState [ , openMode=QIODevice.ReadWrite ] ] )
参数
返回类型

bool

初始化 QLocalSocket 采用本机套接字描述符 socketDescriptor 。返回 true if socketDescriptor 被接受作为有效套接字描述符;否则返回 false 。以指定模式打开套接字通过 openMode ,并进入指定套接字状态通过 socketState .

注意

采用相同本机套接字描述符初始化 2 个本地套接字是不可能的。

另请参阅

socketDescriptor() state() openMode()

PySide2.QtNetwork.QLocalSocket. socketDescriptor ( )
返回类型

qintptr

返回本地套接字描述符为 QLocalSocket 对象若这是可用的;否则返回 -1。

套接字描述符不可用当 QLocalSocket 是在 UnconnectedState 。描述符的类型取决于平台:

  • 在 Windows,返回值是 Winsock 2 套接字句柄 .

  • 采用 WinRT 并启用 INTEGRITY 时,返回值为 QTcpSocket 套接字描述符且类型定义由 socketDescriptor .

  • 在所有其它像 UNIX 操作系统,类型是表示套接字的文件描述符。

PySide2.QtNetwork.QLocalSocket. state ( )
返回类型

LocalSocketState

返回套接字的状态。

另请参阅

error()

PySide2.QtNetwork.QLocalSocket. stateChanged ( socketState )
参数

socketState LocalSocketState

PySide2.QtNetwork.QLocalSocket. waitForConnected ( [ msecs=30000 ] )
参数

msecs int

返回类型

bool

等待直到套接字被连接,最长 msecs 毫秒。若连接已建立,此函数返回 true ;否则它返回 false 。若它返回 false ,可以调用 error() to determine the cause of the error.

以下范例最多等待 1 秒为建立连接:

socket.connectToServer("market")
if (socket.waitForConnected(1000))
    print("Connected!")
											

msecs 是 -1,此函数不会超时。

PySide2.QtNetwork.QLocalSocket. waitForDisconnected ( [ msecs=30000 ] )
参数

msecs int

返回类型

bool

等待直到套接字已断开连接,最长 msecs 毫秒。若连接被成功断开连接,此函数返回 true ;否则它返回 false (若操作超时,若发生错误,或者若此 QLocalSocket 已经断开连接)。若它返回 false ,可以调用 error() to determine the cause of the error.

以下范例最多等待 1 秒为关闭连接:

socket.disconnectFromServer()
if (socket.waitForDisconnected(1000))
    print("Disconnected!")
											

msecs 是 -1,此函数不会超时。

另请参阅

disconnectFromServer() close()