Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.0k views
in Technique[技术] by (71.8m points)

vb.net - Visual Basic + PHP Server

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.

Some of the features I need in the Visual Basic Application:

  1. Start/Stop Server
  2. Server Configuration
  3. Access files on the server directory.

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Well, what you want is broad and yet, there is not enough information about your PHP part.

But I can help you with VB.NET. Here is a Class (And a Sub and an Event) that can really help.

Some Examples first

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

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 !"})

Use Handling:

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

See? very easy.


Now to Start

Follow these two steps

First: Add System.Web Reference

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'

Second: Copy this block before the '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

I believe this works properly.

Hope It Helps.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...