Visual Basic 服务器和 Qt 客户端(QNetworkReply 完成较新的发布)

Posted

技术标签:

【中文标题】Visual Basic 服务器和 Qt 客户端(QNetworkReply 完成较新的发布)【英文标题】:Visual Basic server and Qt client (QNetworkReply finished newer emitted) 【发布时间】:2014-08-24 12:37:39 【问题描述】:

我正在为一件(可能)非常愚蠢的事情而发疯...... 即使在服务器发送数据之后,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)));

有人可以帮帮我吗?

【问题讨论】:

【参考方案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)

【讨论】:

以上是关于Visual Basic 服务器和 Qt 客户端(QNetworkReply 完成较新的发布)的主要内容,如果未能解决你的问题,请参考以下文章

怎样用Visual Basic6.0编写木马程序

Visual Basic 6:Winsock,使用已在使用的端口

使用 Visual Basic 脚本和批处理的 Java 作为 Windows 服务

在Visual Basic 6中,Environ()函数为CLIENTNAME返回null值

辉煌不再!Microsoft计划结束Visual Basic编程语言

将电子表格 (OpenOffice) 导入 Visual Basic .net 组合框