S.H.
S.H.

Reputation: 35

QTcpSocket readyRead not emitted

The target is to receive some messages constantly from another tcp server. Instead of python built-in tcp socket, I switch to QTcpsocket. The server side confirms a client connection. Well, the readyRead seems that has never been emitted and do not know what is wrong.

I have tried the python built-in socket and it works fine.

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_())

The QTcpSocket client should constantly receive the input.

Upvotes: 1

Views: 1021

Answers (1)

eyllanesc
eyllanesc

Reputation: 244132

You have to remove:

self.tcpSocket.waitForConnected (1000)

In Qt the waitForXXX(...) method is blocking.

What is blocking in Qt? It is a task that does not allow the Qt eventloop to be executed, thus preventing asynchronous tasks from executing.

This interferes with the data reception tasks that is triggered by readyRead a signal that is an asynchronous element.

In conclusion, do not use the waitForXXX(...) functions if you are going to use the event loop.

Upvotes: 2

Related Questions