目标是不断从另一个tcp服务器接收一些消息。我切换到QTcpsocket而不是python内置的tcp套接字。服务器端确认客户端连接。好吧,readyRead似乎从未发出过,也不知道出了什么问题。
我尝试了python内置套接字,并且工作正常。
import socket
import sys
import struct
import json
import os
#crate a TCP/IP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
#Connect the socket to the port where the server is listening
server_address = ('localhost', 3336)
print('connectiong to {} port {}'.format(*server_address))
sock.connect(server_address)
file_count = 0
try:
amount_received = 0
amount_expected = 6000
while amount_received < amount_expected:
data = sock.recv(5000)
data_string = data.decode("utf-8")
#print('received {!r}'.format(data))
if data_string.find("Header") != -1:
dataChunkSize = int(data_string[data_string.find("Header")+6:data_string.find("|")])
print(dataChunkSize)
try:
out = json.loads(data_string[data_string.find("JSONSTART|")+10 : data_string.find("JSONEND")])
print(out)
file_count += 1
json_data_folder = "XXXX"
if os.path.exists(json_data_folder):
json_data_folder = os.path.join(json_data_folder, str(file_count)+'.json')
with open(json_data_folder, 'w') as outfile:
json.dump(out, outfile, sort_keys=True)
print("save")
except ValueError as e:
print(e)
data_false = data
amount_expected += len(data)
finally:
print('socket closed')
sock.close()
from PyQt5.QtCore import QDataStream, QIODevice
from PyQt5.QtWidgets import QApplication, QDialog
from PyQt5.QtNetwork import QTcpSocket, QAbstractSocket
class Client(QDialog):
def __init__(self):
super().__init__()
self.tcpSocket = QTcpSocket(self)
self.blockSize = 0
self.makeRequest()
self.tcpSocket.waitForConnected(1000)
# send any message you like it could come from a widget text.
#self.tcpSocket.write(b'hello')
self.tcpSocket.readyRead.connect(self.dealCommunication)
self.tcpSocket.error.connect(self.displayError)
def makeRequest(self):
HOST = 'localhost'
PORT = 3336
self.tcpSocket.connectToHost(HOST, PORT, QIODevice.ReadWrite)
def dealCommunication(self):
print('Message Comming')
#data = self.tcpSocket.readAll()
instr = QDataStream(self.tcpSocket)
instr.setVersion(QDataStream.Qt_5_0)
if self.blockSize == 0:
if self.tcpSocket.bytesAvailable() < 2:
return
self.blockSize = instr.readUInt16()
if self.tcpSocket.bytesAvailable() < self.blockSize:
return
# Print response to terminal, we could use it anywhere else we wanted.
print(str(instr.readString(), encoding='utf-8'))
def displayError(self, socketError):
if socketError == QAbstractSocket.RemoteHostClosedError:
pass
else:
print(self, "The following error occurred: %s." % self.tcpSocket.errorString())
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
client = Client()
sys.exit(client.exec_())
QTcpSocket客户端应不断接收输入。
答案 0 :(得分:1)
您必须删除:
self.tcpSocket.waitForConnected (1000)
在Qt中,waitForXXX(...)
方法正在阻塞。
什么阻止了Qt? 该任务不允许执行Qt eventloop,从而阻止了异步任务的执行。
这会干扰由readyRead触发的异步元素信号触发的数据接收任务。
最后,如果要使用事件循环,请不要使用waitForXXX(...)
函数。