Visual Basic + PHP服务器 [英] Visual Basic + PHP Server
问题描述
我需要一个PHP Server与我的无线传感器进行交互.但是我还需要该服务器由Visual Basic应用程序控制.
I need a PHP Server to interact with my wireless sensors. But I also need that server to be controlled by a Visual Basic Application.
我在Visual Basic应用程序中需要的一些功能:
Some of the features I need in the Visual Basic Application:
- 启动/停止服务器
- 服务器配置
- 访问服务器目录上的文件.
PHP文件(服务器应用程序)仅用于接收来自无线传感器模块的数据并将其存储在平面数据库文件(CSV,XML)中.写入此数据后,Visual Basic必须访问平面数据库文件以执行分析.
The PHP file (server application) is simply to accept data from the wireless sensor module and store in a flat database file (CSV, XML). After this data has been written Visual Basic must access the flat database file to perform analysis.
关于使用哪种服务器以及哪种特定方法可能提供最简单解决方案的任何建议?
Any suggestions on what server to use and what particular methods might provide the easiest solution?
推荐答案
好吧,您想要的内容很广泛,但是关于PHP部分的信息却不够.
Well, what you want is broad and yet, there is not enough information about your PHP part.
但是我可以帮助您使用VB.NET.这是一个可以真正提供帮助的类(以及子类和事件).
But I can help you with VB.NET. Here is a Class (And a Sub and an Event) that can really help.
只需加载HTML代码:
Simply loads a HTML code:
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行):
Sends POST to destination (just the Dim line):
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" 然后按确定.还要在" 导入的名称空间 "
Go to Project > [Project name] Properties > Reference After that press the "Add..." button, and check 'System.Web' Then press Ok. also check it in 'Imported namespaces'
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
我相信此 正常工作 .
I believe this works properly.
希望有帮助.
这篇关于Visual Basic + PHP服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!