Trying to get the port number of my server via lua, I've tried [I]GetConVarNumber( [/I][I]"port" [/I][I]) [/I]but that doesn't work, just returns 0, [I]GetConVarString( [/I][I]"ip" [/I][I])[/I] works just fine. Any Ideas?
You need to use this.
[code]GetConVarString("hostport")[/code]
Oh wow, you can get the ip then... Where was this like a week ago???
[QUOTE=Exho;47057338]Oh wow, you can get the ip then... Where was this like a week ago???[/QUOTE]
Not sure what you mean, but I'm fairly certain GetConVarString("ip") returns the internal IP on the local network, not the address users would connect with.
AFAIK, you have to make an external connection that replies with your external address.
[QUOTE=RealDope;47057350]Not sure what you mean, but I'm fairly certain GetConVarString("ip") returns the internal IP on the local network, not the address users would connect with.
AFAIK, you have to make an external connection that replies with your external address.[/QUOTE]
Not necessarily, you can always access this API endpoint: [url]http://bot.whatismyipaddress.com/[/url] which returns the external IP address of the requesting network.
[QUOTE=Svenskunganka;47058136]Not necessarily, you can always access this API endpoint: [url]http://bot.whatismyipaddress.com/[/url] which returns the external IP address of the requesting network.[/QUOTE]
That looks like "an external connection that replies with your external address" to me?
hostport never worked if the first port chosen was in use or there was some other issue with reading the ip that someone came up with an alternative solution. I can't remember who originally posted this but I added the hook at the bottom which sets the value when the first player joins because http takes a little bit of time to load.
[code]
function game.GetIP()
if ( !game.ServerIP ) then
http.Fetch( "http://whatismyip.akamai.com/", function(strIP, iLen)
game.ServerIP = strIP .. ":" .. GetConVarString( "hostport" )
end )
end
end
function game.ProcessIP( hostip )
local ip = {}
ip[ 1 ] = bit.rshift( bit.band( hostip, 0xFF000000 ), 24 )
ip[ 2 ] = bit.rshift( bit.band( hostip, 0x00FF0000 ), 16 )
ip[ 3 ] = bit.rshift( bit.band( hostip, 0x0000FF00 ), 8 )
ip[ 4 ] = bit.band( hostip, 0x000000FF )
return table.concat( ip, "." ) .. ":" .. GetConVarString( "hostport" )
end
function game.GetLocalIP()
local hostip = GetConVarString( "hostip" ) -- GetConVarNumber is inaccurate
hostip = tonumber( hostip )
return game.ProcessIP( hostip )
end
hook.Add( "PlayerInitialSpawn", "SetServerIP", function( )
if ( !game.ServerIP ) then
game.GetIP( );
end
end )[/code]
Sorry, you need to Log In to post a reply to this thread.