QLocalSocket Class Reference

[ QtNetwork module]

QLocalSocket 类提供本地套接字。 更多...

继承 QIODevice .

类型

方法

Qt Signals


详细描述

QLocalSocket 类提供本地套接字。

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.


类型文档编制

QLocalSocket.LocalSocketError

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

常量 描述
QLocalSocket.ConnectionRefusedError QAbstractSocket.ConnectionRefusedError The connection was refused by the peer (or timed out).
QLocalSocket.PeerClosedError QAbstractSocket.RemoteHostClosedError 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 QAbstractSocket.HostNotFoundError 本地套接字名未找到。
QLocalSocket.SocketAccessError QAbstractSocket.SocketAccessError The socket operation failed because the application lacked the required privileges.
QLocalSocket.SocketResourceError QAbstractSocket.SocketResourceError The local system ran out of resources (e.g., too many sockets).
QLocalSocket.SocketTimeoutError QAbstractSocket.SocketTimeoutError 套接字操作超时。
QLocalSocket.DatagramTooLargeError QAbstractSocket.DatagramTooLargeError The datagram was larger than the operating system's limit (which can be as low as 8192 bytes).
QLocalSocket.ConnectionError QAbstractSocket.NetworkError 连接发生错误。
QLocalSocket.UnsupportedSocketOperationError QAbstractSocket.UnsupportedSocketOperationError The requested socket operation is not supported by the local operating system.
QLocalSocket.UnknownSocketError QAbstractSocket.UnknownSocketError 发生无法识别的错误。

QLocalSocket.LocalSocketState

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

常量 描述
QLocalSocket.UnconnectedState QAbstractSocket.UnconnectedState 套接字未连接。
QLocalSocket.ConnectingState QAbstractSocket.ConnectingState The socket has started establishing a 连接。
QLocalSocket.ConnectedState QAbstractSocket.ConnectedState 已建立连接。
QLocalSocket.ClosingState QAbstractSocket.ClosingState The socket is about to close (data may still be waiting to be written).

另请参阅 QLocalSocket.state ().


方法文档编制

QLocalSocket.__init__ ( self , QObject   parent  = None)

parent argument, if not None, causes self to be owned by Qt instead of PyQt.

创建新的本地套接字。 parent argument is passed to QObject 的构造函数。

QLocalSocket.abort ( self )

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

另请参阅 disconnectFromServer () and close ().

int QLocalSocket.bytesAvailable ( self )

重实现自 QIODevice.bytesAvailable ().

int QLocalSocket.bytesToWrite ( self )

重实现自 QIODevice.bytesToWrite ().

bool QLocalSocket.canReadLine ( self )

重实现自 QIODevice.canReadLine ().

QLocalSocket.close ( self )

重实现自 QIODevice.close ().

QLocalSocket.connectToServer ( self , QString  name , QIODevice.OpenMode   mode  = QIODevice.ReadWrite)

试图连接到 name .

套接字被打开采用给定 openMode and first 进入 ConnectingState . It then attempts to connect to the address or addresses returned by the lookup. Finally, if a connection is established, QLocalSocket 进入 ConnectedState and 发射 connected ().

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

另请参阅 state (), serverName (),和 waitForConnected ().

QLocalSocket.disconnectFromServer ( self )

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.

另请参阅 connectToServer ().

LocalSocketError QLocalSocket.error ( self )

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

另请参阅 state () and errorString ().

bool QLocalSocket.flush ( self )

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.

调用此函数若需要 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 () 代替。

另请参阅 write () 和 waitForBytesWritten ().

QString QLocalSocket.fullServerName ( self )

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

注意: The return value of this function is platform specific.

另请参阅 connectToServer () 和 serverName ().

bool QLocalSocket.isSequential ( self )

重实现自 QIODevice.isSequential ().

bool QLocalSocket.isValid ( self )

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

注意: 套接字的状态必须为 ConnectedState before reading and writing can occur.

另请参阅 state () and connectToServer ().

int QLocalSocket.readBufferSize ( self )

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

str QLocalSocket.readData ( self , int  maxlen )

重实现自 QIODevice.readData ().

QString QLocalSocket.serverName ( self )

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

另请参阅 connectToServer () 和 fullServerName ().

QLocalSocket.setReadBufferSize ( self , int  size )

设置尺寸为 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.

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.

另请参阅 readBufferSize () 和 read ().

bool QLocalSocket.setSocketDescriptor ( self , sip.voidptr  socketDescriptor , LocalSocketState   state  = QLocalSocket.ConnectedState, QIODevice.OpenMode   mode  = QIODevice.ReadWrite)

初始化 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 ,并进入指定套接字状态通过 socketState .

注意: It is not possible to initialize two local sockets with the same native socket descriptor.

另请参阅 socketDescriptor (), state (),和 openMode ().

sip.voidptr QLocalSocket.socketDescriptor ( self )

返回本地套接字描述符为 QLocalSocket object if this is available; otherwise returns -1.

套接字描述符不可用当 QLocalSocket 是在 UnconnectedState .

另请参阅 setSocketDescriptor ().

LocalSocketState QLocalSocket.state ( self )

返回套接字的状态。

另请参阅 error ().

bool QLocalSocket.waitForBytesWritten ( self , int  msecs  = 30000)

重实现自 QIODevice.waitForBytesWritten ().

bool QLocalSocket.waitForConnected ( self , int  msecs  = 30000)

等待直到套接字被连接,最长 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 错误。

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

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

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

另请参阅 connectToServer () 和 connected ().

bool QLocalSocket.waitForDisconnected ( self , int  msecs  = 30000)

等待直到套接字已断开连接,最长 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 错误。

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

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

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

另请参阅 disconnectFromServer () and close ().

bool QLocalSocket.waitForReadyRead ( self , int  msecs  = 30000)

重实现自 QIODevice.waitForReadyRead ().

此函数阻塞直到数据可用于读取且 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).

另请参阅 waitForBytesWritten ().

int QLocalSocket.writeData ( self , str)

重实现自 QIODevice.writeData ().


Qt Signal Documentation

void connected ()

This is the default overload of this signal.

此信号被发射在 connectToServer () has been called and a connection has been successfully established.

另请参阅 connectToServer () 和 disconnected ().

void disconnected ()

This is the default overload of this signal.

This signal is emitted when the socket has been disconnected.

另请参阅 connectToServer (), disconnectFromServer (), abort (),和 connected ().

void error (QLocalSocket::LocalSocketError)

This is the default overload of this signal.

此信号被发射在发生错误之后。 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 () 和 qRegisterMetaType ().

另请参阅 error (), errorString (),和 创建自定义 Qt 类型 .

void stateChanged (QLocalSocket::LocalSocketState)

This is the default overload of this signal.

此信号被发射每当 QLocalSocket 的状态改变。 socketState 参数是新状态。

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

另请参阅 state () and 创建自定义 Qt 类型 .