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!!