D
D
Denis2016-10-15 00:01:47
Python
Denis, 2016-10-15 00:01:47

Receiving data with QTcpSocket from server in python?

There was a problem getting data:
Server in python. Receives and processes data and sends the result:

def server(net, host, port):
    sock = socket.socket()
    sock.bind((host, port))
    sock.listen(1)

    print('Server start...')
    while True:
        c, addr = sock.accept()
        print('Got connection from ', addr)
        print('Receiving...')
        f = open('pic', mode='wb')

        rcvData = c.recv(BLOCK_SIZE)
        f.write(rcvData)
        while (rcvData):
            print('Receiving...')
            rcvData = c.recv(BLOCK_SIZE)
            f.write(rcvData)
        f.close()
        print('Done receiving!')

        img = cv2.imread('pic')
        objLoc = getCaption(net, img)
        print('processing is done')
        writeObjLocToCsv(objLoc, 'objs')
        z = open('objs', 'rb')
        l = z.read(BLOCK_SIZE)
        print(l)
        print('send...')
        print c.send(l)

        c.close()


The client part is implemented in Qt.
The problem is that I can not get data from the server.
void Client::getCaption(QString &img)
{
    pmSock = new QTcpSocket(this);
    pmSock->connectToHost("127.0.0.1",12343);
    sendQImage(img);
    pmSock->waitForBytesWritten();
    reciveData();
}

void Client::reciveData()
{
    QString recvData;
    //qDebug() << pmSock->waitForReadyRead(0);
    qDebug() << "Reading: " << pmSock->bytesAvailable();//всегда 0


    qDebug() << pmSock->readAll();
    pmSock->close();
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Pavel K, 2016-10-15
@D3Nd3R

Because reciveData needs to be called when the data actually arrives.
those. bind to readyRead signal:
well, or put a check into an endless loop, but this is not according to Feng Shui.
And forget about

waitForBytesWritten();
waitForReadyRead()

They hang up the interface (only useful in a separate thread), use signal slots.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question