Data exchange between PHP and the server application in C++ with Qt4?

You need PHP on the server to transfer application data via SSL, wait for response data and display them on the screen.


Wrote this code in PHP:
<?php
error_reporting(E_ALL);

$fp = fsockopen("ssl://localhost", 6000);

$send = "hello";

fputs($fp, $send);
$html = fread($fp, 1000000);
fclose($fp);

echo "<pre-->".$html."
"; ?>


And the function of receiving messages in QT4
void CConnThread::slotReadyRead()
{
 //Create the data flow.
 QDataStream in(sslSocket);
in.the setversion(QDataStream::Qt_4_4);

 //If came the first part of the sent client information.
 if(blockSize == 0)
{
 cout << "data transfer from client" << endl;
 //If the first part is smaller than the count of the information that determines the size of the entire message...
 if(sslSocket->bytesAvailable() < (int)sizeof(qint64)) return;

 //Get the size sent by the client message.
 in >> blockSize;
}

 //If the next part together less than a certain client number...
 if(sslSocket->bytesAvailable() < blockSize) return;

 //Reset the size parameter sent by the client message.
 blockSize = 0;

 //Get the string network message.
 QString message; in >> message;

 //Next, we create...;)

 cout << message.toAscii().data() << endl;

this->sendMessage("received!");
}

void CConnThread::sendMessage(QString message)
{
 QByteArray block;
 QDataStream out(&block, QIODevice::WriteOnly);
out.the setversion(QDataStream::Qt_4_4);
 out << (qint64)0;
 out << message;
out.device()->seek(0);
 out << (qint64)(block.size() - sizeof(qint64));
sslSocket->write(block);
}



But here's the problem — the data arrive (as evidenced by the output line of the data transfer from the client), but not displayed. The very same PHP script hangs after sending (waiting for a response, but not receiving it). What did I do wrong?
October 10th 19 at 14:56
6 answers
October 10th 19 at 14:58
Not the fact that the data arrives, because if(blockSize == 0) says that if the variable is zero, then it will take place, so uterati that the data came from for these reasons is wrong. You need to use debugging, so I can't tell you. In the variable also can store "garbage", you need to use malloc to zero out memory. It is concerned with C++

But in the PHP code where it checks whether the opened socket is there continuity?
Maybe the port to which You connect is closed. and You connect on the 6000 port, and the like 3665 for TCP.

I do in programming is not strong, but maybe it somehow helps.
C++ client works fine, so the problem is only in PHP - theresa97 commented on October 10th 19 at 15:01
October 10th 19 at 15:00
If all runs under Linux you can use strace to run the script and qt app and see what system calls are.
October 10th 19 at 15:02
I suspect that it is necessary to write to the socket data in the same format in which QT serializes strings.
There should at least be present or a sign of the end of the string or its length.

Then no reason to read the message size from the socket, if there is not written?
Anyway, fputs does not write to handle anything except what he was given.
Thus the program stops at the line

in >> blockSize;

I certainly don't see declaring a variable blockSize, but I suspect that this is a qint64, and that at least 8 bytes.
You've got it in the socket only 5 byte string "hello".
Here she is waiting for the other 3 bytes :-) - theresa97 commented on October 10th 19 at 15:05
And then she will try to get a 32-bit string length and string itself in UTF-16. ;) - ewell_Lindgren commented on October 10th 19 at 15:08
True, it is qint64.

If I send the same hello from the client, the server displays it. Or is there another service data is? - krista.Hills82 commented on October 10th 19 at 15:11
I tried to send more than 8, the result is the same. There may be a problem in SSL? - theresa97 commented on October 10th 19 at 15:14
And QString is UTF-16? - theresa97 commented on October 10th 19 at 15:17
October 10th 19 at 15:04
Of course the same. Because of this

QString message; in >> message;

it is necessary first to write to socket the correct length of the string.
See below. - theresa97 commented on October 10th 19 at 15:07
October 10th 19 at 15:06
Same thing:


<?php
include_once('doc.write.php');

error_reporting(E_ALL);

$fp = fsockopen("ssl://localhost", 6000);

$send = doc_write_create();
doc_write_ui8($send, 6);
doc_write_string($send, "hello");

fputs($fp, doc_write_content($send));
$html = fread($fp, 1000000);
fclose($fp);

echo "<pre-->".$html."
"; ?>

On the server:
INFORMATION: tcp server is listen on address and port 6000 0x9afe312
INFORMATION: incoming of new connection 8
INFORMATION: the client was connected
connection encrypted
data transfer from client
here it should output hello.
doc_write_ui8, doc.write.php
not Google
what is this?

if the server code has not changed and assume that
doc_write_ui64 writes to the buffer UInt64 in the right byte order
doc_write_ui32 writes to the buffer UInt32 in the right byte order
it is necessary to write so

$send = doc_write_create();
doc_write_ui64($send, 9);
doc_write_ui32($send, 5);
doc_write_string($send, "hello"); - theresa97 commented on October 10th 19 at 15:09
this is all under the assumption that doc_write_string writes to the buffer is nothing but the string. - ewell_Lindgren commented on October 10th 19 at 15:12
This is a homemade function. The number is the number of bytes. They feign sisney function fwrite(); to Write in the correct order (I'm using them working with binary formats) - krista.Hills82 commented on October 10th 19 at 15:15
and doc_write_string writes? - theresa97 commented on October 10th 19 at 15:18
Just line in C style. - theresa97 commented on October 10th 19 at 15:21
well, then you have to write something like
doc_write_ui8($send, 9);
doc_write_ui4($send, 6);
doc_write_string($send, "hello"); - theresa97 commented on October 10th 19 at 15:24
October 10th 19 at 15:08
Turns out I mixed up little endian and big endian. Now the output looks like this:

INFORMATION: tcp server is listen on address and port 6000 0x8e15312
INFORMATION: incoming of new connection 8
INFORMATION: the client was connected
connection encrypted
data transfer from client

INFORMATION: the client was disconnected
INFORMATION: thread client was destroyed

But somehow, the string on the server is empty (although the size is read correct). And in PHP script not output the answer.
The answer comes in PHP, but for some reason the first 12 bytes (not 8) I understand the size. Then the line in the form r 0x00 0x00 0x00 0x00 e c e i v 0x00 0x00 0x00 0x00 0x00 e d!

0000 0000 0000 0000 1600 1200 7200 6500
6300 6500 6900 7600 6500 6400 2100 - theresa97 commented on October 10th 19 at 15:11
Yeah, understood. Did not expect that QString is a string in the Pascal style. The first 8 bytes is the size of the entire packet, the next 4 bytes is the length of the string and then the string itself in UTF-16. - ewell_Lindgren commented on October 10th 19 at 15:14

Find more questions by tags PHPC++SocketsDigital certificatesQt