【问题标题】:Visual Basic + PHP ServerVisual Basic + PHP 服务器
【发布时间】:2013-05-20 18:34:38
【问题描述】:

我需要一个 PHP 服务器来与我的无线传感器交互。但我还需要该服务器由 Visual Basic 应用程序控制。

我在 Visual Basic 应用程序中需要的一些功能:

  1. 启动/停止服务器
  2. 服务器配置
  3. 访问服务器目录中的文件。

PHP 文件(服务器应用程序)只是从无线传感器模块接收数据并存储在平面数据库文件(CSV、XML)中。写入此数据后,Visual Basic 必须访问平面数据库文件以执行分析。

关于使用什么服务器以及哪些特定方法可以提供最简单的解决方案有什么建议吗?

【问题讨论】:

  • PHP 不打算作为服务器运行,因此使用它是一种不好的方式。您可以做的只是简单地传递带有参数的 URL 并以这种方式发送/接收数据
  • 这有点过于宽泛了。但是,在高层次上,您可以做的是让 PHP 应用程序通过 HTTP 公开功能以控制它抽象的所有内容,而 VB 应用程序可以向 PHP 功能发出 Web 请求以执行这些控制。因此,Web 应用程序可能有一个名为 ReadSensors 的资源,它接受它需要的任何参数(在 POST 中或作为查询字符串)并返回传感器数据的 JSON 对象。然后 VB 应用程序可以使用类似WebClient 对象的东西向这样的资源发送请求,从而有效地向服务器发出命令。

标签: php vb.net visual-studio


【解决方案1】:

好吧,您想要的内容很广泛,但是关于您的 PHP 部分的信息还不够。

但我可以帮助您使用 VB.NET。这是一个真正有用的类(以及一个子和一个事件)。

先举几个例子

只需加载 HTML 代码:

Dim Page As New WEBhtml("http://www.example.com/index.php?get=something")
While Page.IsReady = False
End While
If IsNothing(Page.Exception) Then 
    MsgBox(Page.GetHtml)
Else
    MsgBox(Page.Exception.Message)
End If

将 POST 发送到目的地(只是 Dim 行):

Dim Page As New WEBhtml("http://www.example.com/index.php?get=something", {"a=alpha", "b=beta", "c=I Don't Know :D !"})

使用处理:

Private Sub form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim Page As New WEBhtml("http://www.e.com/i.php")
End Sub
Private Sub html_done(ByRef sender As WEBhtml) Handles Me.WebHtml_Done
    MsgBox("Timetook: " & sender.TimeTook / 1000 & "s")
    MsgBox("Url: " & sender.Url)
    If IsNothing(sender.Exception) Then
        MsgBox("Bandwidth: " & sender.Bytes / 1024 & "kb")
        MsgBox("HTML: " & sender.GetHtml)
    Else
        MsgBox("Error: " & sender.Exception.Message)
    End If
End Sub

看到了吗?很容易。


现在开始

按照这两个步骤进行

首先:添加 System.Web 引用

转到项目 > [项目名称] 属性 > 参考 之后按“添加...”按钮,然后检查“System.Web” 然后按确定。还要在 'Imported namespaces'

中检查它

第二个:把这个块复制到'End Class'之前

Public Shared Event WebHtml_Done(ByRef sender As WEBhtml)
Friend Shared Sub RaiseDone(ByRef wh As WEBhtml)
    RaiseEvent WebHtml_Done(wh)
End Sub
Public Class WEBhtml
    Private thrd As Threading.Thread
    Private Err As Exception
    Private BytesUsed As ULong = 0
    Private Time As UInteger = 0
    Private Html As String = ""
    Private _Url As String
    Private tmr As New Timer

    Private Sub initialize()
        tmr.Interval = 50
        AddHandler tmr.Tick, AddressOf Tick
        tmr.Enabled = True
        tmr.Start()
    End Sub

    Public Sub New(ByVal Url As String)
        thrd = New Threading.Thread(Sub() WEB_POST(Url))
        initialize()
        thrd.Start()
    End Sub
    Public Sub New(ByVal Url As String, ByVal PostData As String())
        thrd = New Threading.Thread(Sub() WEB_POST(Url, PostData))
        initialize()
        thrd.Start()
    End Sub

    Private Sub Tick(sender As Object, e As EventArgs)
        If thrd.IsAlive = False Then
            tmr.Enabled = False
            RaiseDone(Me)
        End If
    End Sub
    Private Sub WEB_POST(ByVal url As String, Optional ByVal values() As String = Nothing)
        _Url = url
        Dim data As String = ""
        Dim a, b As Integer
        b = My.Computer.Clock.TickCount
        Try
            For i = 0 To values.GetLength(0) - 1
                a = values(i).IndexOf("=")
                If a >= 0 Then
                    data += System.Web.HttpUtility.UrlEncode(Mid(values(i), 1, a)) & "=" & System.Web.HttpUtility.UrlEncode(Mid(values(i), a + 2))
                    If i < values.GetLength(0) - 1 Then data += "&"
                End If
            Next
        Catch
            data = ""
        End Try

        Try
            Dim request As System.Net.WebRequest = System.Net.WebRequest.Create(url)
            request.Method = "POST"
            Dim postdata As String = data
            Dim byteArray As Byte() = System.Text.Encoding.UTF8.GetBytes(postdata)
            request.ContentType = "application/x-www-form-urlencoded"
            request.ContentLength = byteArray.Length
            request.Timeout = 100000
            Dim dataStream As IO.Stream = request.GetRequestStream()
            dataStream.Write(byteArray, 0, byteArray.Length)
            dataStream.Close()

            Dim response As Net.WebResponse = request.GetResponse()
            dataStream = response.GetResponseStream()
            Dim reader As New IO.StreamReader(dataStream)
            Dim responseFromServer As String = reader.ReadToEnd()
            reader.Close()
            dataStream.Close()
            response.Close()
            BytesUsed += responseFromServer.Length + byteArray.Length
            Time = My.Computer.Clock.TickCount - b
            Html = (responseFromServer)
        Catch ex As Exception
            Err = ex
            Time = My.Computer.Clock.TickCount - b
            Html = ""
        End Try
    End Sub

    Public ReadOnly Property Exception() As Exception
        Get
            Return Err
        End Get
    End Property
    Public ReadOnly Property TimeTook() As UInteger
        Get
            Return Time
        End Get
    End Property
    Public ReadOnly Property Bytes() As ULong
        Get
            Return BytesUsed
        End Get
    End Property
    Public ReadOnly Property GetHtml() As String
        Get
            Return Html
        End Get
    End Property
    Public ReadOnly Property IsReady() As Boolean
        Get
            Return Not thrd.IsAlive
        End Get
    End Property
    Public ReadOnly Property Url() As String
        Get
            Return _Url
        End Get
    End Property
End Class

我相信这工作正常

希望对您有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-11
    • 2020-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多