Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Client-server app, "send image" having problems
Forum Update on Monday, May 27th 2025

Client-server app, "send image" having problems

Scheduled Pinned Locked Moved General and Desktop
1 Posts 1 Posters 1.4k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    deleted571
    wrote on 9 May 2014, 12:39 last edited by
    #1

    Hi,
    I am trying to send an image (OpenCV Mat) from client to server over a QDataStream. The first item is an int, the size of the buffer. It works for 10 to 15 images, then the server reads the first int a random number (usual ~2^30) which causes the app to crash. Please help. Thanks in advance!
    This is the client side, in a loop (the images come from webcam with period 500ms):
    @
    //1) encode to jpg
    cv::vector<uchar> buf;
    cv::imencode(".jpg", *mat, buf);

    //2) write to buffer
    QByteArray buffer;
    QDataStream out(&buffer, QIODevice::WriteOnly);
    out << int(0); // save place for an int which represents the buffer size

    for(cv::vector<uchar>::iterator it = buf.begin(); it != buf.end(); ++it)
    {
    out << *it; //write each byte
    }

    out.device()->seek(0); //write the buffer size
    out << buffer.size();
    qDebug() << "Sent " << buffer.size() << "bytes";

    qint64 bytesSent = socket->write(buffer);
    if(bytesSent < buffer.size())
    {
    if(bytesSent != -1)
    {
    qDebug() << "Transmit Error! Sent " << bytesSent << " out of " << buffer.size();
    }
    else
    {
    qDebug() << socket->errorString();
    }
    }
    @

    Server side:

    @QDataStream in(socket);
    int msgSize = -1;

    //first read the size as an int
    if(socket->bytesAvailable())
    {
    in >> msgSize;
    }

    qDebug() << "Read image size: " << msgSize << "bytes";
    //wait until all bytes all available
    while(socket->bytesAvailable() < ( qint64 ) ( ( qint64 )msgSize - sizeof(int) ) )
    {
    if(!socket->waitForReadyRead())
    {
    qDebug() << "Disconnected: " << socket->errorString();
    socket->disconnectFromHost();
    break;
    }
    }
    qDebug() << "Bytes recieved: " << msgSize;

    QByteArray ba;
    quint8 byte;
    for(int i = 0; i < msgSize - 1; ++i)
    {
    in >> byte;
    ba.append(byte);
    }
    cv::Mat imgbuf = cv::Mat(FRAME_WIDTH, FRAME_HEIGHT, CV_8UC3, ba.data());
    cv::Mat matImg = cv::imdecode(imgbuf, CV_LOAD_IMAGE_COLOR);
    @

    Both sockets are QTcpSocket.

    Output example:

    The server side:

    @Read image size: 67551 bytes
    Bytes recieved: 67551
    Read image size: 56924 bytes
    Bytes recieved: 56924
    Read image size: 70027 bytes
    Bytes recieved: 70027
    Read image size: -2046830337 bytes
    Bytes recieved: -2046830337
    Read image size: -536866742 bytes
    Bytes recieved: -536866742
    Read image size: 1179207168 bytes@

    At this point it tries to read 1179207168 bytes.

    The client side:
    @Sent 67551 bytes
    Sent 56924 bytes
    Sent 70027 bytes
    Sent 70277 bytes
    Sent 85633 bytes
    Sent 65155 bytes

    etc ...
    Only the first three are successful.
    @

    1 Reply Last reply
    0

    1/1

    9 May 2014, 12:39

    • Login

    • Login or register to search.
    1 out of 1
    • First post
      1/1
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Get Qt Extensions
    • Unsolved