Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7891105
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T06:31:58+00:00 2026-06-03T06:31:58+00:00

I had a socket connection with UNIX C++ sockets where, after connecting, I had

  • 0

I had a socket connection with UNIX C++ sockets where, after connecting, I had a loop for reading byte by byte until I had the full msg. I know the first two bytes of the message I’m going to receive, and its length (15 bytes). So the function looked like:

bool mastControl::findPacket(int sockfd, st_messageMastToPc * messageReceived, bool * connected) {

    int n = 0;
    bool messageFound = false;
    char * buffer = (char *) messageReceived;

    unsigned int pos = 0;

    while ( ((n = read(sockfd, &(buffer[pos]), 1)) > 0) and not messageFound) {

         if (n == 1) {
            pos++;

            if ( (pos == 1) && (buffer[0] == 0x02)) { // First byte to receive
                std::cout << "INFO - Rcv1" << std::endl;
            } else if ( (pos == 2) && (buffer[1] == 0x33) ) { // Second byte
                std::cout << "INFO - Rcv2" << std::endl;
            } else if (pos >= uiMessageMastToPcSize) { // Full msg received
                messageFound = true;
                std::cout << "INFO - Complete message received" << std::endl;
            } else if (pos <= 2) { // Wrong values for the first 2 bytes
                std::cout << "WARN - Reseting (byte " << pos << " -> " << int(pos) << ")" << std::endl;
                pos = 0;
            }
        }
    }

    if (n < 0){
        //EROR
        *connected = false;
    }

    return messageFound;
}

Now I’m implementing the same with QTcpSockets. Connection is established, and then I call:

if(socket->waitForReadyRead(Global::tiempoMaximoDeEsperaParaRecibirDatosMastil)){
                /* Read socket to find a valid packet */
                if (findPacket(socket, &messageReceived)) {
                    qDebug()<<"New packet found!";
//...
}
}

So I’m waiting until there is some info ready yo be read, and then call findPacket, which is now almost the same, reading byte by byte:

bool mastControl::findPacket(QTcpSocket *socket, st_messageMastToPc * messageReceived) {
    int n = 0;
    bool messageFound = false;
    char * buffer = (char *) messageReceived;
    unsigned int pos = 0;

    while ( ((n = socket->read(&(buffer[pos]), 1)) >= 0) and not messageFound) {
        if (n == 1) {
            qDebug()<<"Recibido: "<<buffer[pos]<<", en pos: "<<pos;
            pos++;
            if ( (pos == 1) && (buffer[0] == 0x022)) {
                qDebug()<<"0x02 in first position";
//                std::cout << "INFO - Rcv1" << std::endl;
            } else if ( (pos == 2) && (buffer[1] == 0x33) ) {
                qDebug()<<"0x33 in second";
                std::cout << "INFO - Rcv2" << std::endl;
            } else if (pos >= uiMessageMastToPcSize) {
                messageFound = true;
                std::cout << "INFO - Complete message received" << std::endl;
            } else if (pos <= 2) {
                std::cout << "WARN - Reseting (byte " << pos << " -> " << int(pos) << ")" << std::endl;
                pos = 0;
            }
        }
    }

    if (n < 0){
      qDebug()<< "Disconnected. Reason: " << socket->errorString();
    }

    return messageFound;
}

Looks pretty the same, but it doesn’t work. Once I have waited on waitForReadyRead, I enter findPacket’s loop and I am able to read the first 4 bytes received. After that, no more data is received. It remains inside findPacket’s loop checking again, and again, and again, but read function always return 0 bytes read. No new info is received. Which is impossible, because server is sending the same packet once every few ms, so even if I loss some data, eventually I should be reading something.

So, what I am doing wrong? Should I wait in a different way? Should I wait again the first time read function returns 0 bytes read? What’s the difference between this read function and the C++ libraries one?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T06:31:59+00:00Added an answer on June 3, 2026 at 6:31 am

    Finally, the problem was trying to read all the time instead of waiting for new data after the first zero returned by the read function. Doing that works like a charm!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I had my server working with an earlier version of node.js, npm, and socket.io
Folks: Has anyone had success connecting to a Progress-4GL database with Delphi ?  
My fwrite to a socket is not flushed, until the socket closes. How to
I'm having trouble getting a socket connection between Android(client) and a c# app(server) to
I've had a bug in our software that occurs when I receive a connection
I have a general socket implementation consisting of an OutputStream and an InputStream. After
Is it possible to have a server and a client with 1 socket connection
I wanted to know how to flush the socket streams while doing socket programming
Can anyone recommend a Socket.IO client library for Python? I've had a look around,
I made a client & server that establishes a TCP connection through sockets, I'm

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.