-1

I have a web client (php) which connects to a C++ server (using Qt 4.8) by an IP socket. The weird thing is that if I send anything from the web (I checked data is correct), I always receive "0100000001000000" at the server side.

Weirder, If I do "sleep(1)" between connecting socket and sending data at web side, most of the time data is correct at server side. Below I attach the pertinent code:

PHP side (client):

    ....
    $socket = socket_create(AF_INET, SOCK_STREAM, /*SOL_TCP*/0);

    if(!$socket) 
        return false;

    //EscribirTrama writes $data
    $this->EscribirTrama($data, $peticion);
    $exito = true;

    if(!socket_connect($socket , gethostbyname($this->direccion) , $this->puerto))
    {
        $error = socket_strerror(socket_last_error());
        return false;
    }

    //FIXME this sleep resolves the problem most of the time, why?!
    sleep(1);

    if(!socket_send ($socket, $data, strlen($data), 0))
    {
        $error = socket_strerror(socket_last_error());
        $exito = false;
    }

    socket_close($socket);

C++ side (server, using QTcpSocket)

    ....
    if(!p_socket->waitForReadyRead())
        return false;
    received_bytes = 0;

    if(p_socket->bytesAvailable() < START_SIZE)
    {
        Log.AVISO("Insuficientes datos en el socket");
        return false;
    }

    QByteArray start_bytes = p_socket->read(START_SIZE);

    received_bytes += TAM_COMIENZO;

    quint8 head = start_bytes.at(0);

    if(head != START_CHAR) //this happens when not using sleep at client side!
    {
        //bad data
        QByteArray bad_data;

        bad_data.append(head);

        while(p_socket->bytesAvailable() ||
          p_socket->waitForReadyRead(MS_PARTIAL_READ))
        {
            QByteArray bad = p_socket->readAll();

            received_bytes += bad.size();
            bad_data.append(bad);
        }

        //At this point bad_data is always "0100000001000000"!
        Log.ERROR("Se ha recibido una trama sin cabacera: " +
            CFunciones::Cadena(bad_data));

        return false;
    }
    ......

Why is this happening? How can I fix this? Thanks for help!!

Javi Carnero
  • 409
  • 4
  • 9
  • Don't know why my question has a -1 without explanation :-p – Javi Carnero Jun 23 '14 at 10:36
  • Try adding a bit of logging, such as printing what is sent at client side, and what is recevied at server side (length and hex dump of the data). – hyde Jun 30 '14 at 20:41
  • Also, might help if you converted your Qt code to use signals and slots, and connected all the signals (like errors), even if just to a debug slots. Then there'd be no need to use `bytesAvailable` and `waitForReadyRead` (you'll probably want a buffer as member variable, in case of partial reads). – hyde Jun 30 '14 at 20:44

1 Answers1

0

Finally the error was on the C++ server side. The reason of this weird error is because p_socket comes from a QTcpServer class, but p_socket and QTcpServer are not in the same thread producing this unexpected behavior.

In fact, apart from using sleep, changing the code also changes what the server reads from p_socket (changes from 010000... to 0200000 for example)

Solution: Reimplement "incommingConnection" method in QTcpServer to be able to use the socket in other threads. (http://qt-project.org/doc/qt-4.8/qtcpserver.html#incomingConnection)

Javi Carnero
  • 409
  • 4
  • 9