r/visualbasic • • May 12 '15

Web Server

I have a dedicated Minecraft server. I'd like to build a basic web server that shows detailed info about my server and let's me know if the Minecraft server process is running.

That way I don't have to keep an RDC window open while I play and so I can view stats when I'm away from home.

I've never built a web server app, so any tutorials or advice would be excellent.

5 Upvotes

10 comments sorted by

View all comments

1

u/Bonejob VB Guru May 12 '15 edited May 12 '15

here I did some work with the HttpListener class to build a web server for a project. Here is a multithreaded console app with the basics.

Here it is as a code paste. http://codepaste.net/codeonly/f8kwqv

Sorry about the crazy ass lambda I was to lazy to break it out as a full function. :)

Imports System.Net
Imports System.Threading
Module Module1

Public Sub Main(args As String())
    Dim ws = New SimpleWebServer("http://localhost:666/Simple/")
    AddHandler ws.RequestReceived, Function(sender, context)
                                       Dim buffer = System.Text.Encoding.[Default].GetBytes("Simple Web Server from HttpListener class. Time: " + DateTime.Now)
                                       context.Response.OutputStream.Write(buffer, 0, buffer.Length)
                                       context.Response.StatusCode = CInt(HttpStatusCode.OK)
                                       context.Response.OutputStream.Close()
                                       Return 1
                                   End Function
    ws.ServerStart()
    Console.WriteLine("Listening to http://localhost:666/Simple/")
    Console.WriteLine("Press any key to stop...")
    Console.ReadLine()
    ws.ServerStop()
End Sub
Class SimpleWebServer
    Public Delegate Sub RequestReceivedHandler(sender As Object, context As HttpListenerContext)
    Public Event RequestReceived As RequestReceivedHandler

    Private ReadOnly listener As HttpListener
    Private running As Boolean
    Private ReadOnly connthread As Thread

    Public Sub New(prefix As String)
        listener = New HttpListener()
        listener.Prefixes.Add(prefix)
        connthread = New Thread(AddressOf ConnectionThread)
    End Sub

    Public Sub ServerStart()
        connthread.Start()
    End Sub

    Public Sub ServerStop()
        running = False
        listener.Stop()
    End Sub

    Private Sub ConnectionThread()
        Try
            running = True
            listener.Start()
            While running
                ProcessRequest()
            End While
        Catch generatedExceptionName As HttpListenerException
        Catch generatedExceptionName As ThreadAbortException
        Catch generatedExceptionName As Exception
        End Try
    End Sub

    Private Sub ProcessRequest()
        Dim result As IAsyncResult = listener.BeginGetContext(AddressOf ListenerCallback, listener)
        result.AsyncWaitHandle.WaitOne()
    End Sub

    Protected Sub ListenerCallback(result As IAsyncResult)
        If listener Is Nothing OrElse Not listener.IsListening Then
            Return
        End If
        Dim context = listener.EndGetContext(result)
        OnRequestReceived(context)
    End Sub

    Private Sub OnRequestReceived(context As HttpListenerContext)
        RaiseEvent RequestReceived(Me, context)
    End Sub
End Class
End Module

Edit: Damn code did not paste right.