Page 1 of 2

get ip address

Posted: Sat Mar 05, 2005 4:05 pm
by bacardirigoman
I tried to write a simple platform independent server. It should run on linux as well on windows.
But there is one function really missing in the network library.
getclientip(client)
It should return the IP-address of the specific client. That would be great. Then I don't have to do my server in Java :-/

Posted: Sat Mar 05, 2005 7:22 pm
by Gansta93
Hi,

On Windows, I know it is possible, there is an example in CodeArchiv. But on Linux, I don't know.

Posted: Sun Mar 06, 2005 7:32 am
by nco2k
having that command natively in pb, would be indeed a nice thing. :wink:

c ya,
nco2k

Posted: Sun Mar 06, 2005 9:59 am
by dracflamloc
i agree

Posted: Sun Mar 06, 2005 10:47 am
by blueznl
the only way around is i guess go native winsock ie. berkely socket io

Posted: Sun Mar 06, 2005 10:57 am
by dracflamloc
good for windows, not for linux

Posted: Sun Mar 06, 2005 11:37 am
by blueznl
linux should have socket support as well! but don't ask me how to do it :-)

Posted: Sun Mar 06, 2005 9:10 pm
by dracflamloc
Of course linux has API sockets, but this is a Basic language. API involvement should be kept to a minimum.

Posted: Sun Mar 06, 2005 10:36 pm
by Gansta93
I think that function is possible to add in Windows and Linux because for example, Apash on Windows and Linux can log IP addresses. And I think it would be good to add this function to PB... Fred, what do you think ? Is my remark stupid like ReceiveNetworkString() ? ;-)

Posted: Mon Mar 07, 2005 9:48 am
by olejr
Until it's added, or someone create a little userlib.
This one work with windows & linux:

Code: Select all

Procedure.s GetClientIP(ClientID)
  Structure IPType
   Reserved.w
   Port.w
   StructureUnion
    IPLong.l
    IP.b[4]
   EndStructureUnion
   Zeros.l[2]
  EndStructure
  
  s = SizeOf(IPType)
  
  res = getpeername_(ClientID, @IP.IPType, @s)
  If res = 0
   remotip$ = StrU(IP\IP[0], #Byte)+"."+StrU(IP\IP[1],#Byte)+"."+StrU(IP\IP[2], #Byte)+"."+StrU(IP\IP[3], #Byte)
  Else
   remotip$ = ""
  EndIf

  ProcedureReturn remotip$

EndProcedure
Use it like this:

Code: Select all

If InitNetwork()
  If CreateNetworkServer(8181) = 0 
    MessageRequester("Error", "Can't Create Server", 0)
    End
  EndIf

  Repeat

   event = NetworkServerEvent()
   
   If event = 1 ; Someone connected
    Debug GetClientIP(NetworkClientID()); Here you go
   EndIf
  ForEver
EndIf

Posted: Mon Mar 07, 2005 1:27 pm
by Fred
Ok, it will be added.

Posted: Mon Mar 07, 2005 2:46 pm
by olejr
And then Fred came and saved the day.. :wink:

Just as I had the libs ready..
So if someone want's them until Fred adds it..

Windows->getclientip.zip
Linux->getclientip.tar.gz

Just copy into purelibraries(linux)/userlibraries(windows)
Works like the Procedure I posted earlier:

Code: Select all

ip$=GetClientIP(ClienID)
The source is there, but it ain't pretty..
Just a quick hack..

Oh.. And don't forget to remove the lib when Fred add the same command..

Posted: Mon Mar 07, 2005 6:12 pm
by Gansta93
Fred wrote:Ok, it will be added.
Thank you Fred ! :-D

Posted: Wed Mar 09, 2005 5:17 pm
by oliv
Good Idea :D

Posted: Wed Mar 09, 2005 5:26 pm
by DoubleDutch
At the same time can you add client event #4???

-Anthony