0

我正在为一件(可能)非常愚蠢的事情而发疯......即使在服务器发送数据之后,QNetworkReply 完成的信号也会更新。

我有一个非常简单的 vb 服务器

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

当我使用 qt 进行调用时,返回一个空字符串。并引发错误“远程服务器在接收和处理整个回复之前过早地关闭了连接”

在客户端请求代码下方

    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)));
}

有人可以帮我吗?

4

1 回答 1

1

已解决.. 在发送数据之前,我们还需要发送标头以提高完成信号

 ' 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)
于 2014-08-25T08:43:19.907 回答