ServerIP Clientside?

I just realised that I couldn’t really figure a way to get the server IP on client? Please correct me if I’m wrong. Haven’t tested GetGlobalString(“serverip”) or anything. But I couldn’t think of a way of doing it?

You need to format the return of the serverip convar. I think either raBBish or Chrisaster posted code to format it a year or so ago.

[lua] local function band( x, y )
local z, i, j = 0, 1
for j = 0,31 do
if ( x%2 == 1 and y%2 == 1 ) then
z = z + i
end
x = math.floor( x/2 )
y = math.floor( y/2 )
i = i * 2
end
return z
end

local hostip = tonumber( string.format("%u", GetConVarString( "hostip" ) ) )

local serverip = string.format( "%u.%u.%u.%u",
								band( hostip, 0xFF000000 ) / 2^24,
								band( hostip, 0xFF0000 ) / 2^16,
								band( hostip, 0xFF00 ) / 2^8,
								band( hostip, 0xFF ) )[/lua]

Thanks bro. I owe you one ;).

I found a better way, I once wrote a integer -> color conversation function, and it worked perfectly fine for the ip.

Well, nearly :v:



21:46:17 > PrintTable(IntToColor(xxxxxxx84))...
         a<TAB>=<TAB>xx
         b<TAB>=<TAB>xxx
         g<TAB>=<TAB>47
         r<TAB>=<TAB>216


So I just took my code and changed it a bit…

[lua]
function IntToIP(int)
return tostring((int >> 24) & 255)…"."…tostring((int >> 16) & 255)…"."…tostring((int >> 8) & 255)…"."…tostring((int >> 0) & 255);
end
[/lua]

[editline]14th March 2011[/editline]

To speed things up…

[lua]
function GetServerIP()
local int = GetConVarNumber(“hostip”);
return tostring((int >> 24) & 255)…"."…tostring((int >> 16) & 255)…"."…tostring((int >> 8) & 255)…"."…tostring((int >> 0) & 255);
end
[/lua]

The bitwise operators don’t work properly, which is why “/” is used in place of >> and band for &.

They work perfectly fine for me.

They have been fixed a while ago as far as I know.

I tested the code before replying to make sure - it didn’t work.

[editline]15th March 2011[/editline]

[lua]local function band( x, y )
local z, i, j = 0, 1
for j = 0,31 do
if ( x%2 == 1 and y%2 == 1 ) then
z = z + i
end
x = math.floor( x/2 )
y = math.floor( y/2 )
i = i * 2
end
return z
end

function GetServerIP1()
local hostip = tonumber( string.format("%u", GetConVarString( “hostip” ) ) )

return string.format( "%u.%u.%u.%u",
					   band( hostip, 0xFF000000 ) / 2^24,
					   band( hostip, 0xFF0000 ) / 2^16,
					   band( hostip, 0xFF00 ) / 2^8,
					   band( hostip, 0xFF ) )

end

function GetServerIP2()
local int = GetConVarNumber(“hostip”);
return tostring((int >> 24) & 255)…"."…tostring((int >> 16) & 255)…"."…tostring((int >> 8) & 255)…"."…tostring((int >> 0) & 255);
end

print( GetServerIP1() )
print( GetServerIP2() )[/lua]

Output:

192.168.1.9 (correct)
192.168.1.0 (incorrect)

[editline]15th March 2011[/editline]

That actually seems to be caused by GetConVarNumber returning a slightly inaccurate value, however I’m still having problems with the bitwise operators.

[lua] print( string.format( “%08X & 0xFF000000 = %08X”, hostip, hostip & 0xFF000000 ) )
print( string.format( “band( %08X, 0xFF000000 ) = %08X”, hostip, band( hostip, 0xFF000000 ) ) )[/lua]

C0A80109 & 0xFF000000 = 80000000 (incorrect)
band( C0A80109, 0xFF000000 ) = C0000000 (correct)

[editline]15th March 2011[/editline]

The inaccuracy looks like it’s coming from an atof call.

[editline]15th March 2011[/editline]

Yup, some precision is being lost in GetConVarNumber. The string is passed to atof, which returns a double that gets casted to a float for Lua to use.

Just to demonstrate:


printf( "%f
", atof( "-1062731511" ) );
printf( "%f
", (float)atof( "-1062731511" ) );

-1062731511.000000 (correct)
-1062731520.000000 (incorrect)

All the binary operators start spitting out wrong values when either one of the operands or the result is >0x7fffffff so maybe it’s a sign issue?



] lua_run print(0x80000000,0x80000000 & 0x80000000)
> print(0x80000000,0x80000000 & 0x80000000)...
2147483648	-2147483648
] lua_run print(0x7fffffff,0x7fffffff & 0x7fffffff)
> print(0x7fffffff,0x7fffffff & 0x7fffffff)...
2147483647	2147483647



Uh. I just noticed, it prints your local IP. Not the servers IP.

I don’t think that’s possible without a module.

It’s possible if the server is properly configured. The hostip convar ( I think ) should point to the correct IP.

[editline]15th March 2011[/editline]

Most aren’t though.

Actually, hostip should always be your local IP.

Shouldn’t it be possible? Like, hostname is serverside. Why wouldn’t hostip be?

A server’s hostname won’t affect the client’s hostname. hostip is set in the function that retrieves your own local address and nowhere else (that I’m aware of).

Well. GetGlobalString(“hostname”) gets the servers.
GetGlobalString(“hostip”) would give profit.

[editline]16th March 2011[/editline]

“servername” that is.

That only works because the hostname is sent in one of the net messages. Creating a function to retrieve the server IP wouldn’t be hard though - try asking Garry.

Will do. Since it doesn’t exist, obviously.

Thanks for the help anyway guys.