Concept for a server uptime keeper

Hi there everyone, I'm one who generally hosts servers in Garry's Mod, a Source engine mod. Being widely open to many submodifications, it's difficult to keep that program up without it crashing. It gets particularly annoying when it freezes with an error, and needs me to manually reboot it. So anyways, I was kind of hoping that someone may be able to guide me through making a VB.Net program capable of

A)Detecting and keeping track of the specific server's process
B)Pinging it through the port I want
C)Ability to kill it when it freezes or an error pops up

I am pretty new to VB.Net, but I am (sort of) coming along.

Thanks in advance.

If anyone wants any more details, they can of course ask, too.


  • seancampbellseancampbell Pennsylvania, USA
    Wish I could help ya, I have never done anything like that before.

    Sounds like you'd want to write a windows service that resides on the server that keeps hanging up. The program would probably need permissions to reboot the computer, another thing I have never done from an app before.

    If you run into any problems with code you've written, feel free to post up specific errors or lines of code you're having trouble with.

    I tend to be better at solving specific issues, usually am not successful at helping new coders write applications, only help them understand why they are having errors with specific code

    -sean c
  • Thanks for replying.

    I think I may have misspoken when I said server. By server, I actually meant a game server, which is just a simple executable on my otherwise home computer. I think that might make this program more easier, anyways. I seem to have a few problems here and there, but it wouldn't be too much for me to ask if you could join the irc chatroom on I think it just might make faster communication.
  • What sort of communication foundation are you using?

    If your whole server is freezing on a single call then I'm guessing you are not properly threading your server.

    Also - what does your framework look like and what pattern strategies are you using?

  • Also a huge part of debugging any server is having proper logging. Every service call should be writting to a log file or db.

    Also your application or system event logs may have something in them if there was any kind of resulting exception.
  • Well, I think this is more of an unavoidable crash, since it's developer doesn't seem to give a crap about what the end users think. And since the game, (server), has such a large community of modders, anything can really make it crash. Anyhow, I believe most of the crashes occuring to it come from just it running out of resources. It does however create a .MDMP file, which I gather is for debugging, but if it's an error in the code, there's nothing I can do about it. That's why I'm trying to make this program.
  • Actually - there are alot of profilers available that can give you a wealth of info on what's going on in a .net assembly - if you are trying to pinpoint a performance issue check this out:
    [link=]Red-Gate Performance Profiler[/link]
  • It sounds like you are saying that you do not have access to the source code for this thing - do you have at least access to the executing assemblies?

    If not there isn't going to be much you can do.

    If you absolutely can't get ahold of source for this thing but you CAN get ahold of the compiled assembly (as long as it is not signed) one very painfull option for you is to download Reflector from and just dissassemble the thing back into source and patch it back together into a solution.
  • I think we might be getting a little off track here. This server, Garry's mod, has tons of dlls, and I'm not very interested in doing the developers dirty work to fix their own program. I wouldn't even know how, since it's in C++. However, I want to sort of help by making my own program, in VB.Net, that will restart that serving program is it crashes, and if it can't ping/open a socket to it.

    I hope that helps.
  • ok - I'm sorry, I assumed you wanted to actually solve the underlying problem and made all sorts of foolery here.

    So what you want to do is:
    - Work with the existing crappy system without making any alterations.
    - Be able to detect when the server has stopped responding.
    - Upon detecting a dead server, close the process and restart it.

    This is actually fairly simple to do. What you are actually looking at designing is an "application host".

    Now to my understanding the server process itself is not actually crashing, but is "locking" up or just stops responding to requests. So it won't help to detect if the application crashes.

    While "pinging" will tell you that the actual server machine is up and running, it will not actually tell you if the server application is responsive or not. You will need to find a "heartbeat" command that you can tap into. So if there is some already-existing request that you can send to the server and get a response from that doesn't require any significant processing time - this is what you should use.

    So I can't tell by this thread what kind of communication solution the server application is using to communicate with clients. In order to make a heartbeat call you will need to find this out and create appropriate interfaces in your host.

    Here is a very simple implementation of what you will be building:

    static class Program
    private static Process _server = null;

    public static void Main()
    while (true)
    if (_server.HasExited
    || !_server.Responding
    || !HeartBeat())


    public static void RestartServer()
    if (_server != null && !_server.HasExited) _server.Kill();
    _server = Process.Start("TheEvilServer.exe");

    public static bool HeartBeat()
    // Code here to check responsiveness
    return true;


    You are basically taking ownership of the running process of the server here and monitoring it's status every 10 minutes.
    If your server has stoped responding, crashed or exited, or does not respond to a heartbeat - then it will be forcibly restarted.

  • Sorry - forgot what forum this was. Here that is in VB:
    Module Module1
    Dim _server As Process = Nothing

    Sub Main()
    While (True)
    If (_server.HasExited Or Not _server.Responding Or Not HeartBeat()) Then
    End If
    End While
    End Sub

    Sub RestartServer()
    If (Not _server Is Nothing and Not _server.HasExited)
    End If
    _server = Process.Start("TheEvilServer.exe")

    End Sub

    Function HeartBeat() As Boolean
    ' Code here for checking responsiveness
    Return True;
    End Function

    End Module

  • Ahh, that should pretty much complete my program, thank you! There are actually two instances in which I would want to restart the server's process, that's when it either just plainly closes, which should be easy enough to detect, or when an error pops up and it doesn't close right away. At that point, however, the outbound/inbound connections to it close, so if I can some how open a socket connection with VB.Net as well, I think that could add another layer of security in terms of detecting unresponsiveness. If you could possibly help me with the sockets in VB.Net, that would be awesome.

    Thanks so far!
  • So I'm not convinced that opening a socket is going to help you because the sockets will maintain themselves even if your server becomes unresponsive - but in order to just get a socket connection you will want to use the tcpclient

    Dim tcpClient As New System.Net.Sockets.TcpClient()
    tcpClient.Connect("", 1234)
    Dim tcpStream As NetworkStream = tcpClient.GetStream()

    since your shell will be running on the same physical machine as your server you will want to connect to the "loop-back" ip - and whatever port your server is listening on.

    Treat the stream just as you would any other stream and read/write bytes to it or whatever - I think you can even wrap it with at streamreader or streamwriter to make communication easier.
  • It seems it has a problem with the line saying

    [code]If (Not _server Is Nothing And Not _server.HasExited) Then[/code]

    I don't know, it seems to have a lot of double negatives.
  • ya, sorry I don't ever actually use vb myself so I forget its formatting sometimes.

    change that to
    If ((Not _server = Nothing) And (Not _server.HasExited)) Then

    Most other languages just use the != sign for "Not Equal To" but VB purposely attempts to move away from traditional coding schemes and attempts to provide more human readable code.

    Oh - also, you might want to make your host into a service. This way if the host fails then windows can restore it.
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!