R
R
Robotex2011-02-08 15:09:23
PHP
Robotex, 2011-02-08 15:09:23

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>


And a function to receive a message on QT4
<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>


But here's the problem - the data comes (as evidenced by the output of the data transfer from client line), but is not displayed. The PHP script itself freezes after sending (waits for a response, but does not receive it). What have I done wrong?

Answer the question

In order to leave comments, you need to log in

6 answer(s)
S
Sergey, 2011-02-08
@butteff

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.

N
Naim, 2011-02-08
@naim

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.

X
xdenser, 2011-02-08
@xdenser

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.

X
xdenser, 2011-02-09
@xdenser

Certainly similar. Because for this
QString message; in >> message;
you must first write the correct length of the string to the socket.

R
Robotex, 2011-02-09
@Robotex

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>";
?>

On the server:
INFORMATION: tcp-server is listen on address 0x9afe312 and port 6000
INFORMATION: incoming of new connection 8
INFORMATION: client was connected
connection encrypted
data transfer from client
hello should be displayed here.

R
Robotex, 2011-02-10
@Robotex

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 question

Ask a Question

731 491 924 answers to any question