GNet for Blitzmax

Tweet blitzmax networking code-archives network-gamenet
(Posted 3 years ago) RonTek

GNet master server query for BlitzMax by boomboommax and example by markcwm. Reposted from BlitzBasic Code Archives

Important Note: To display your games to the GNet section and use the blitzcoder gnet master server, use host:String=""

Type networkgnet
    Const host:String = "localhost"   'enter to connect to BlitzCoder GNet MasterServer
    Const hostget:String = ""

    Const port:Int = 80

    Global socket:tsocket
    Global stream:tsocketstream

    Function gnet_esc:String(t:String)

        t = Replace(t,"&","")
        t = Replace(t,"%","")
        t = Replace(t,"'","")
        t = Replace(t,Chr(34),"")
        t = Replace(t," ","_")

        Return t

    End Function

    Function gnet_open:tsocketstream(opt:String)

        networkgnet.socket = CreateTCPSocket()

        ConnectSocket(networkgnet.socket,HostIp(,networkgnet.port) = CreateSocketStream(networkgnet.socket,True)

        WriteLine,"GET "+networkgnet.hostget+"?opt="+opt+" HTTP/1.0"
        WriteLine,"HOST: "


        While ReadLine( <> ""


    End Function

    Function gnet_exec(opt:String,game:String,server:String)

        opt = opt+"&game="+networkgnet.gnet_esc(game)

        If server <> "" opt = opt+"&server="+networkgnet.gnet_esc(server)

        Local t:tsocketstream = networkgnet.gnet_open(opt)
        If Not t Then Return False

        Local ok = False
        If ReadLine(t) = "OK" Then ok = True

        Return ok

    End Function

    Function gnet_ping:String()

        Local t:tsocketstream = networkgnet.gnet_open("ping")
        If Not t Then Return False

        Local ip:String = ReadLine(t)

        Return ip

    End Function

    Function gnet_addserver(game:String,server:String="")

        Return networkgnet.gnet_exec("add",game,server)

    End Function

    Function gnet_refreshserver(game:String,server:String="")

        Return networkgnet.gnet_exec("ref",game,server)

    End Function

    Function gnet_removeserver(game:String)

        Return networkgnet.gnet_exec("rem",game,"")

    End Function

    Function gnet_listservers:Int(game:String="")
        For Local n:networkgnet_server=EachIn networkserverlist
            n = Null

        Local t:tsocketstream = networkgnet.gnet_open("list")
        If Not t Then Return False

        Local t_game:String
        Local t_server:String
        Local t_ip:String

            t_game = ReadLine(t)
            If Left(t_game,2)<>"<b" And t_game<>""
                t_server = ReadLine(t)
                t_ip = ReadLine(t)

                If game = "" Or gnet_esc(game) = t_game
                    Local p:networkgnet_server=New networkgnet_server

           = t_game
                    p.server = t_server
                    p.ip = t_ip

        Until Eof(t)

        Return 1
    End Function    

End Type

Global networkserverlist:TList = New TList

Type networkgnet_server
    Field game:String
    Field server:String
    Field ip:String
End Type

Demo / Example

'   gnet conversion test


Import "gnet_inc.bmx"

Global gnet:networkgnet = New networkgnet
Global gnet_server:networkgnet_server = Null

Local my_game$="my_gnet_game",my_server$="my_gnet_server"

Local p$=gnet.gnet_ping()
Print "~nPing:"+p


Print "~nAdding server..."
gnet.gnet_addserver( my_game$ )


Input "~nHit return to refresh server..."

gnet.gnet_refreshserver( my_game$,my_server$ )

gnet.gnet_listservers( my_game$ ) 'list just my game

Input "~nHit return to remove server and exit..."

gnet.gnet_removeserver( my_game$ )


Function print_servers()
    Print ""
    For gnet_server:networkgnet_server = EachIn networkserverlist
        If gnet_server<>Null
            Print "Game:"" Server:"+gnet_server.server+" IP:"+gnet_server.ip
(Posted 3 years ago) markcwm commented:

Thanks Ron, but you shouldn't be crediting me, all I did was write the example code, the gnet conversion functions were by boomboommax (formerly cyanide) except for gnet_listservers which was fixed by Regular K but is identical to ListGNetServers in GNetServerList by Oddball. Oddball's code also works fine.

One function I did write is GetLocalIP but I can't remember if gnet_ping does this.

(Posted 3 years ago) RonTek commented:

Hey Mark, thanks! Ok just updated it GetLocalIP would be a good addition..

(Posted 3 years ago) hardcoal commented:

Excuse me for asking.. but what exactly is GNet? I mean whats the difference betwin this and the regular blitzmax network commands?
Or Am I confusing stuff up?

(Posted 3 years ago) RonTek commented:

Yes, it is a bit confusing having both this masterserver and the networking commands with the same name.

(Posted 11 months ago) hardcoal commented:

Hi ron.. Im struggeling with networking.. Im tying to understand which path should i go..
there is UDP And TCP.. And GNet..

[Topic Moved Here]

Reply To Topic (minimum 10 characters)

Please log in to reply