Search code examples
vb.netqtqnetworkaccessmanagerqnetworkrequestqnetworkreply

Visual Basic server and Qt client (QNetworkReply finished newer emitted)


I'm going crazy on a (probably) very stupid thing... The QNetworkReply finished signal is newer emitted even after the server send data.

I have a very simple vb server

Public Class WebServer
    Public Shared Sub Main()
        Try
            Dim strIP As String = "serverip"
            Dim hostName As String = Dns.GetHostName()
            Dim serverIP As IPAddress = Nothing
            Dim checkIP As IPAddress = Nothing

            Dim strPort As String = "9071"

            For Each checkIP In Dns.GetHostEntry(hostName).AddressList()
                DebugLog("Checking IP: " & checkIP.ToString)
                If checkIP.ToString = strIP Then
                    serverIP = checkIP
                End If
            Next

            Dim tcpListener As New TcpListener(serverIP, Int32.Parse(strPort))

            tcpListener.Start()

            Dim clientSocket As System.Net.Sockets.Socket
            clientSocket = tcpListener.AcceptSocket()

            Dim respByte() As Byte = Encoding.ASCII.GetBytes("replystring")

            clientSocket.Send(respByte, 0, respByte.Length, SocketFlags.None)

            clientSocket.Shutdown(SocketShutdown.Both)
            clientSocket.Close()

        Catch ex As Exception
            MsgBox("Exception")
        End Try
    End Sub
End Class

when I make a call with qt an empty string return.. and an error is raised "the remote server closed the connection prematurely, before the entire reply was received and processed"

below the client request code

    void TESTJson::makeTheCallBtnPressed()
    {

    ui.label->clear();
    ui.label_2->clear();

        QUrl url("serveraddressandport");
        QString method = "methodname";
        url.setPath(QString("%1%2").arg(url.path()).arg(method));

        QNetworkRequest request;
        request.setUrl(url);

        request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");

        QByteArray postData; // test
        postData.append("param1=hi");

        networkManager= new QNetworkAccessManager();

        connect(networkManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(_NETAM_finished(QNetworkReply*)));

        reply = networkManager->post(request, postData);

        connect(reply, SIGNAL(downloadProgress(qint64, qint64)), this, SLOT(_downloadProgress(qint64, qint64)));
        connect(reply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(_error(QNetworkReply::NetworkError)));
        connect(reply, SIGNAL(finished()), this, SLOT(_finished()));
        connect(reply, SIGNAL(metaDataChanged()), this, SLOT(_metaDataChanged()));
        connect(reply, SIGNAL(sslErrors(const QList<QSslError> &)), this, SLOT(_sslErrors(const QList<QSslError> &)));
        connect(reply, SIGNAL(uploadProgress(qint64, qint64)), this, SLOT(_uploadProgress(qint64, qint64)));
}

could someone help me please?


Solution

  • Solved.. Before send data we need to send also headers in order to raise finished signal

     ' Set HTML Header
      Dim htmlHeader As String = _
              "HTTP/1.0 200 OK" & ControlChars.CrLf & _
              "Content-Length: " & respByte.Length & ControlChars.CrLf & _
              "Content-Type: " & getContentType(strBuff) & _
              ControlChars.CrLf & ControlChars.CrLf
    
      ' The content Length of HTML Header
      Dim headerByte() As Byte = Encoding.ASCII.GetBytes(htmlHeader)
    
      ' Send HTML Header back to Web Browser
      clientSocket.Send(headerByte, 0, headerByte.Length, SocketFlags.None)