Answer the question
In order to leave comments, you need to log in
Data exchange between PHP and C++ Qt4 server application?
It is necessary to transfer data via SSL from PHP to the server application, wait for the response data and display it on the screen.
I wrote the following code in PHP:
<br>
<?php<br>
error_reporting(E_ALL);<br>
<br>
$fp = fsockopen("ssl://localhost", 6000);<br>
<br>
$send = "hello";<br>
<br>
fputs($fp, $send);<br>
$html = fread($fp, 1000000);<br>
fclose($fp);<br>
<br>
echo "<pre>".$html."</pre>";<br>
?><br>
<br>
void CConnThread::slotReadyRead()<br>
{<br>
//Создать поток данных.<br>
QDataStream in(sslSocket);<br>
in.setVersion(QDataStream::Qt_4_4);<br>
<br>
//Если пришла первая часть из посланной клиентом информации.<br>
if(blockSize == 0)<br>
{<br>
cout << "data transfer from client" << endl;<br>
//Если первая часть меньше того кол-ва информации что определяет размер всего сообщения...<br>
if(sslSocket->bytesAvailable() < (int)sizeof(qint64)) return;<br>
<br>
//Получить размер посылаемого клиентом сообщения.<br>
in >> blockSize;<br>
}<br>
<br>
//Если последующие части вместе взятые меньше, чем определенное клиентом кол-во...<br>
if(sslSocket->bytesAvailable() < blockSize) return;<br>
<br>
//Обнулить параметр размера посылаемого клиентом сообщения.<br>
blockSize = 0;<br>
<br>
//Получить строку сетевого сообщения.<br>
QString message; in >> message;<br>
<br>
//Далее... творим ;)<br>
<br>
cout << message.toAscii().data() << endl;<br>
<br>
this->sendMessage("received!");<br>
}<br>
<br>
void CConnThread::sendMessage(QString message)<br>
{<br>
QByteArray block;<br>
QDataStream out(&block, QIODevice::WriteOnly);<br>
out.setVersion(QDataStream::Qt_4_4);<br>
out << (qint64)0;<br>
out << message;<br>
out.device()->seek(0);<br>
out << (qint64)(block.size() - sizeof(qint64));<br>
sslSocket->write(block);<br>
}<br>
Answer the question
In order to leave comments, you need to log in
It's not a fact that the data is coming, because if(blockSize == 0) says that if the variable is zero, then it will display a line, so it's wrong to say that the data came for these reasons. You need to use debugging, so you can’t say right away. The variable can also store "garbage", you need to use malloc to zero out the memory. This is about C ++
But in the php code, where are the checks whether the socket has opened, is there a connection?
Maybe the port you are connecting to is closed. and you connect to port 6000, and there are only 3665 of them for TCP.
I'm not really good at programming, but maybe this will help somehow.
If everything starts under Linux, then you can use strace to run both the script and the qt application, and see how the system calls go there.
I suspect that you need to write data to the socket in the same format in which QT serializes strings.
There, at least, there must be either a sign of the end of the line or its length.
Then it is not clear why to read the size of the message from the socket, if it was not written there?
In any case, fputs does not write anything to the handle other than what was given to it.
Certainly similar. Because for this
QString message; in >> message;
you must first write the correct length of the string to the socket.
So the same:
<?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."</pre>";
?>
It turned out that I mixed up little endian and big endian. Now the output looks like this:
INFORMATION: tcp-server is listen on address 0x8e15312 and port 6000
INFORMATION: incoming of new connection 8
INFORMATION: client was connected
connection encrypted
data transfer from client
INFORMATION: client was disconnected
INFORMATION: thread client was destroyed
But why- then the string on the server is empty (although the size is read correctly). And in the PHP script, the answer is not displayed.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question