Page 1 of 1

Tip: Get IP of a peer

Posted: Wed Sep 11, 2002 2:04 pm
by BackupUser
Restored from previous forum. Originally posted by tranquil.

Finaly I found it! :)

After some weeks of damn search through the MSDN. The following code tells your app which IP is connected to the socket you specified. Put it in your code where you check for incomming connections (networkserverevent=1)

Code: Select all

        bufferlength.b=255
        *buffer=GlobalAlloc_(#GMEM_FIXED|#GMEM_ZEROINIT, 255)
        res=getpeername_(YOUR SOCKET HERE,*buffer,@bufferlength)
        If res=0 ; All okay
          val1.b=PeekB(*buffer+4)
          val2.b=PeekB(*buffer+5)
          val3.b=PeekB(*buffer+6)
          val4.b=PeekB(*buffer+7)
          ip$=StrU(val1,#Byte)+"."+StrU(val2,#Byte)+"."+StrU(val3,#Byte)+"."+StrU(val4,#Byte)
        else 
          error=wsagetlasterror()
          ;errorhandling here
        endif
        globalfree_(*buffer)

Hope it may help the one or other ppl. :)

Mike

Tranquilizer/ Secretly!
http://www.secretly.de
Registred PureBasic User

Posted: Wed Sep 11, 2002 2:06 pm
by BackupUser
Restored from previous forum. Originally posted by tranquil.

Oh I forgot to mention, if you replace "getpeername_()" with "getsockname_()" you will get the IP on which the peer connected.
(If your server has more then one IP)

okay, all for now.

Mike

Tranquilizer/ Secretly!
http://www.secretly.de
Registred PureBasic User

Posted: Wed Sep 11, 2002 2:54 pm
by BackupUser
Restored from previous forum. Originally posted by MrVainSCL.

Hi Mike
Very nice work :wink:


PIII450, 256MB Ram, 6GB HD, RivaTNT, DirectX8.1, SB AWE64, Win2000 + all Updates...

greetz
MrVainSCL! aka Thorsten

Posted: Wed Sep 11, 2002 2:55 pm
by BackupUser
Restored from previous forum. Originally posted by tranquil.

Thanks! :)

Tranquilizer/ Secretly!
http://www.secretly.de
Registred PureBasic User

[Kaputt] Tip: Get IP of a peer

Posted: Tue Sep 17, 2002 12:58 pm
by BackupUser
Kaputt: Contains only partial code...

Restored from previous forum. Originally posted by Pupil.
Originally posted by tranquil

Finaly I found it! :)
After some weeks of damn search through the MSDN. The following code tells your app which IP is connected to the socket you specified. Put it in your code where you check for incomming connections (networkserverevent=1)
Ok, i've found some things that need to be adjusted:

* The variable bufferlength should be a long i.e. bufferlength.l = 16
* You need only to allocate 16 bytes to hold the IP address structure.

this could be another way of handling this stuff:

Code: Select all

Structure IPType
  Reserved.w
  Port.w
  StructureUnion
    IPLong.l
    IP.b[4]
  EndStructureUnion
  Zeros.l[2]
EndStructure

length.l = SizeOf(IPType)
result.l = GetPeerName_(ClientID, @IP.IPType, @length)
If result = 0
  ip$ = StrU(IP\IP[0],#Byte)+"."+StrU(IP\IP[1], #Byte)+"."
  ip$ + StrU(IP\IP[2],#Byte)+"."+StrU(IP\IP[3], #Byte)+":"+StrU(IP\Port,#Word)
Else
  result = WSAGetLastError_()
  ; Error handling.
EndIf

Posted: Sat Sep 21, 2002 4:56 pm
by BackupUser
Restored from previous forum. Originally posted by MrVainSCL.

Hi @ all
Just take a look to following link, to learn more about all kind of network and how to code it:

http://www.codeproject.com/internet/

PIII450, 256MB Ram, 6GB HD, RivaTNT, DirectX8.1, SB AWE64, Win2000 + all Updates...

greetz
MrVainSCL! aka Thorsten