GLBasic forum

Main forum => GLBasic - en => Topic started by: CptnRoughnight on 2013-Nov-19

Title: Returning User's IP
Post by: CptnRoughnight on 2013-Nov-19
Hi folks,

need some help  :)

I have a raspberry pi. This is my server. I need a function that returns me the IP address of the user.
User connects to server and the server sends the IP of the user back to the user. How does it work?

I tried it with NetGetIP $, but that just gives me the IP address on my network (192 172 .....)
:doubt:


Title: Re: Returning User's IP
Post by: Moru on 2013-Nov-19
You are talking about the external IP. Your computer has no clue what this is, you need to ask an external server for the originating IP. See services like "whatismyip.com" and similar. Also easy to do with a PHP script. Should be a post here on the boards how to do it. Let us know if you need more help with it :-)
Title: Re: Returning User's IP
Post by: CptnRoughnight on 2013-Nov-19
You mean this thread: http://www.glbasic.com/forum/index.php?topic=5400.0 ?

This one I tried, but the service is not available. How do I call another service in this way, I do not know, I would not actually.

Is there a way with the NET commands, as a server to determine the IP address of the user? That would solve my problem as I had imagined.
Title: Re: Returning User's IP
Post by: kanonet on 2013-Nov-19
Your Server can easily know the IP of the clients that connected to it, SOCK_TCPACCEPT returns the IP of an client every time a client joins (watch 2nd parameter, its byref/a pointer), so you can simply store it somewhere and return it later. Im not sure if you can do the same with the NET_... commands, havent used them in ages, SOCK_...s are way better and more powerful.

Maybe you want to have a look at my libNET (http://www.kanonet.de/downloads/libnet) for some inspiration how to do socket work.
Title: Re: Returning User's IP
Post by: CptnRoughnight on 2013-Nov-19
Thank you!


I'll look at it! The sockets have not tried, but it's apparently a good solution!

I will report on the success or failure! ;)
Title: Re: Returning User's IP
Post by: Moru on 2013-Nov-19
Oh, kanonet, that was just what I was about to look for tonight, thanks! :-)
Can it do UDP broadcast too?
Some usage examples could be useful too, I'm very rusty with glbasic types...
Title: Re: Returning User's IP
Post by: kanonet on 2013-Nov-19
No its only designed for TCP. And important features like automatic syncing variables etc got never finished/added (will come... sometimes.. in the future...). But it is working and can give you a start at Sock-commands.
No real test project, but should be easy to use. Here is an very basic example how to connect (and also how to send/receive messages), sorry its a bit confusing, since its only one of my test projects, no intended tutorial:
Code (glbasic) Select
// --------------------------------- //
// Project: Server
// Start: Saturday, October 01, 2011
// IDE Version: 10.118
// Autor: Kanonet

ALLOWESCAPE FALSE
AUTOPAUSE FALSE

STDOUT "  (1) Server oder (2) Client?\n"
GLOBAL sel%
sel = STDIN$()

IF sel=1
GLOBAL server AS TNetServer
server.Start(34111)
ELSEIF sel=2
GLOBAL client AS TNetClient
client.Join("",34111)
client.SendLogIn("CDK","a", TRUE)
LOCAL l%
REPEAT
l=client.LogInReply()

IF l=-4
client.SendLogIn("CDK","a")
ELSEIF l<0
END
ENDIF

SLEEP 40
UNTIL l=1
ELSE
STDOUT "Eingabe ungültig."
END
ENDIF

LOCAL key$
REPEAT
IF sel=1
server.Update()
// FOREACH p IN server.player[] // for each connected player = logged in client
// FOREACH m$ IN p.channel[chan_num%].msg$[]
// // now you can prozess each message in channel chan_num%
// NEXT
// p.ChannelSend(chan_num%, "test message from server") // send something to each player on channel 0
// NEXT
ELSE
client.Update()
// FOREACH m$ IN client.channel[chan_num%].msg$[]
// // now you can prozess all messages in channel chan_num%
// NEXT
// client.ChannelSend(chan_num%, "test message from client") // send something to the server
ENDIF
SLEEP 40

//
//  // watch ESC key to exit program
// key$=INKEY$()
// IF key$=""
// BREAK
// ENDIF
UNTIL FALSE

END

FUNCTION log%: code%, msg$, ip$=""
IF ip$ THEN msg$ = msg$+" "+ip$
STDOUT PLATFORMINFO$("TIME")+"  "+code+"   "+msg$+"\n"
ENDFUNCTION

FUNCTION PlayerJoined%: num%
server.player[num%].ChannelOpen( 0 ) // automatically open channel 0 for each player that joined
ENDFUNCTION

SUB GLB_ON_QUIT:
IF sel=1
server.Quit()
ELSE
client.Quit()
ENDIF
ENDSUB

FUNCTION ServerTerminated:
SLEEP 10000
ENDFUNCTION
Title: Re: Returning User's IP
Post by: Moru on 2013-Nov-19
Perfekt, thanks! I just need UDP to find the server so can easily hack something together. It's just a small project.
Title: Re: Returning User's IP
Post by: spicypixel on 2013-Nov-20
I put this online ages ago for people to query to get the real IP
Code (glbasic) Select
http://ip.spicypixel.net/
Title: Re: Returning User's IP
Post by: kanonet on 2013-Nov-20
Quote from: Ocean on 2013-Nov-20
Quote from: kanonet on 2013-Nov-19
No its only designed for TCP. ....

directly from the online GLB-manual:     sock% = SOCK_UDPOPEN(port%)
Yes I know?
But I think Morus question was if my libNET does support UDP and so I answered it does not. Or did I understand something wrong?
Title: Re: Returning User's IP
Post by: Moru on 2013-Nov-20
No you are correct, kanonet.
Title: Re: Returning User's IP
Post by: matchy on 2013-Nov-22
Quote from: Dr.Damn Fist on 2013-Nov-19
I have a raspberry pi. This is my server.

Cool and what does the server do, I wonder?
Title: Re: Returning User's IP
Post by: CptnRoughnight on 2013-Nov-22
The Rapsberry Pi is used as a server for my games.

This idea I wanted to implement for competition (Crap Game Competition 2013), but I've not enough time i guess.

I have many other ideas for which the Pi is suitable as a server.

Maybe i will learn html/css/php to write a homepage by myself! ;)
Title: Re: Returning User's IP
Post by: matchy on 2013-Nov-23
What sort of games? I have tried many server and client games. I've also had GLB as a web server!
Title: AW: Returning User's IP
Post by: CptnRoughnight on 2013-Nov-23
First simple games with high score management. But I have ideas for a kind of online game. I'll play around a bit with the possibilities once I have fully understood the network functionality ;)