how to get the ip address of a user loged on?

Just starting out? Need help? Post your questions and find answers here.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Henrik.

Hi
Is there anyway to get the ip address, of a user loged on through the internet, to your server?

Thanks.

Best regards
Henrik.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Pupil.

Yes, i think there's even a functional code snipp somewhere on this forum, if i remenber correctly. For starters you could always look up the win API command 'GetPeerName()'.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Henrik.

Thanks Pupil
But the only thing i have found on the subject is for local connection, but that
does'nt mean that it not there somewere just that i haven't found it yet ;o)

I'am not good at the API thing, but i will look it up anyway.
Thanks ;o)

Best regards
Henrik.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Henrik.

Hi Found it :o) Thankyou very much Pupil, Tranquil & MrVainSCL
If anyboddy els need the info it is here:
viewtopic.php?t=3806

i could'nt have done the API stuff by myself, so Thanks:O)
Best regards
Henrik.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

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

Hi Found it :o) Thankyou very much Pupil, Tranquil & MrVainSCL
If anyboddy els need the info it is here:
viewtopic.php?t=3806

i could'nt have done the API stuff by myself, so Thanks:O)
Best regards
Henrik.
I just want to point out that since this code was posted we've got a new excellent command for creating IP-strings namely the 'IPString()' command... The way the ip-string was created in the post you found was really awkward..
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Henrik.

Thanks Pupil
i just tested your version.
ip addresse seems to work okay but i'am getting wierd port number
but i off to bed now, i try it after work tomorrow.

cya.
Best regards
Henrik.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Pupil.

How do you mean weird? Like (-) minus nuber or so? If you get minus numbers that's because PB uses signed numbers so either you can use the StrU(port.w, #Word) command to get the string or you could put the port number into a long value like this:

Code: Select all

; word 2 long..
long.l = 0
long | yourword.w ; now you got a non negative value of
                  ; 'yourword' in 'long'
Else if you think that you get the wrong number when you've got a server on a specific port and expect that this is the port that should be reported -well then you got things all wrong, cause the clients can connect to your server port from ANY port. If people connecting to your server is sitting behind a gateway using NAT you'll get connections from them to your server ranging from port 20000- 65000 aprox.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Henrik.

Well Pupil it's the last case you mention.

First off all it was late yesterday, so
I just cut and paste your code into at altered version ofthe Atomic webserver.
and i then loged on to it from the same computer.
Netstat sayes about the connection:

Tcp > 62.243.122.27:80 62.243.122.27:1326

And the webserver should report the ip of the client and the port he uses Right?

but the webserver sayes: 62.243.122.27:11781 and not 1326.

But in all fairness i didn't really check if it was my mistake, i was just satisfied with the fact that i could read the clients ip. ;o)

I still have work to do.. Yack..;((
And with sport in the TV.(HÃ¥nbold) i might not have the time to day..

But i'am gonna look in to it' as soon i can ti would be nice if i could get some coherent resoult here. ;o)


cya.
Regards
Henrik
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Pupil.

It seems, one of the programs is doing things the wrong way, i checked in hex what the two different values would be:

Code: Select all

11781 = $2E05
1326  = $052E
See the similarity, the only difference is that the bytes have switched places, which one is right? I really don't know, perhaps the WinSock uses highendian(or is it lowendian) byte order like most unix system does? I've checked the win sdk some but couldn't find any evidence that points in either direction, anyone else who knows whats up with this?

EDIT:
Actually i just found this on MSDN:
Quote from MSDN

"... sockaddr contents are expressed in network byte order."
You seem to have to switch the byte order of the port value..
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Henrik.

huh that's funny Pupil (kind off anyway)

I got no idea why it's switched around, but it is.I have tried several times now.
i would never have thought checking the value in hex, they just seemed random to me.

the api stuff is beyond me so i'am to no help here.

SouuulTaker Win API Tutorial :O)

But Thanks Pupil for your help so far ;O)
cya.
Regards
Henrik
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Pupil.

I'll show you how to switch the byte order for you if your uncertain of how it's done. Below is three small examples of how it could be done:

Code: Select all

; Using logical bit operators
port.w = $2e05
newport.w = 0

newport | ((port&$ff) > 8))

Debug Hex(newport)

; or do it in assembly - compiler option for this
; MUST be turned on

newport = 0 ; not really needed here

mov ax, port
mov bl, ah
mov bh, al
mov newport, bx

Debug Hex(newport)

; or by using pointers and a structure
Structure WordUnionType
  StructureUnion
    Word.w
    Byte.b[2]
  EndStructureUnion
EndStructure

newport = 0

*ptr1.WordUnionType = @port
*ptr2.WordUnionType = @newport
*ptr2\Byte[0] = *ptr1\Byte[1]
*ptr2\Byte[1] = *ptr1\Byte[0]

Debug Hex(newport)
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Henrik.

hi Takker så mycket Pupil ;O)
Me trying to say thank you in Swedish..

It's weird though that doh.. that just remind me that i have
downloade a' netstat app. made in pure, somewere on my harddisk
sure it was with full source. doh i had the source to read the ip
all along.

i have to find it and see if it dose the same with the port number.

Cya
Henrik.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Henrik.

Well Found it.
It's coded by Seigfried Rings.
I think he called it something with "Redirect Outputs into Memory"
but i have saved it with the name "Netstat" cuz that what it dose.

It dose a Netstat command and redirect the output to uhm "the memory of the application self"
Gosh my English s*ck's to day "Also"..
Of course that will show the port corect.

Enough english for to day! ;(

Cya.
Regards.
Henrik.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Rings.

Hi Hendrik,
it is >Redirect Outputs into Memory )



Its a long way to the top if you wanna .....CodeGuru
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Henrik.

Lol Rings ;o)
I was thinking about editing my first post but i gave up ;o)
I have been working too much lately.
But then again, I have all friday off.... And it's Dedicated to Pure. He he.

And btw. Yes it's cool Rings.
I learn better/faster thru code snipperts than by theoretical how to stuff.

Cya
Regards
Henrik.
Post Reply