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

1

u/pimanac Application Specialist May 12 '15

You should check out a server wrapper called McMyAdmin. It does pretty much everything you described and is free for small servers (no sense in reinventing the wheel).

https://www.mcmyadmin.com/

3

u/chrwei May 12 '15

if you want to do it in vb, start with an asp.net tutorial. the hard part is likely going to be getting the data out of minecraft, but I assume it has some sort of API? and if it's not tcp socket based the next challenge will be permissions as IIS runs in a fairly tight security context by default, and opening to too much makes you VERY susceptible to hacking.

2

u/JakDrako May 12 '15

If you don't want to deal with IIS, you can also "self-host" the web server. Look into OWIN/Katana for examples of how to do this.

You can also use WCF (usually used for web services, but nothing prevents you from returning a valid HTML page) and host it in a console window or a Windows service (or, of course, IIS, but then we're back with dealing with IIS for a very simple use).

1

u/Bonejob VB Guru May 12 '15

why do you have to make it so hard on me man... See above code :P

1

u/JakDrako May 13 '15

Very, very nice. I'm adding that to my list of quick tools and utilities.

How do you parse the request when you get a POST or a GET with many arguments? I'm guessing you can extract that from the context? I generally use a bare-bone WCF class library, so that I can define my ServiceContract and my WebInvokes and get all the parsing done by WCF.

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.