QIODevice::read (QTcpSocket): device not open
时间: 2024-06-13 12:05:46 浏览: 396
根据提供的引用内容,这个问题可能是由于在使用QIODevice::read函数时,QTcpSocket没有正确打开导致的。可以尝试在使用read函数之前,先使用open函数打开QTcpSocket。具体的代码实现可以参考以下范例:
```python
import socket
# 创建一个TCP/IP套接字
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# 连接到远程套接字
server_address = ('localhost', 10000)
print('connecting to {} port {}'.format(*server_address))
sock.connect(server_address)
# 打开套接字
sock.open(QIODevice.ReadWrite)
# 发送数据
message = 'This is the message. It will be repeated.'
print('sending {!r}'.format(message))
sock.sendall(message.encode('utf-8'))
# 接收数据
amount_received = 0
amount_expected = len(message)
while amount_received < amount_expected:
data = sock.read(16)
amount_received += len(data)
print('received {!r}'.format(data))
# 关闭套接字
print('closing socket')
sock.close()
```
阅读全文