Sign in to follow this  
Followers 0
JohnRichard

Get IP of client connected

17 posts in this topic

hello to all. i have this script i can't remember to whom i got this script. its a server script that accepts multiple client connection. script works fine and my client successfully connects to it. my problem is that, i want to return the IP of every successful client connection. there is a function i got that converts IP to socket but i can't return any IP of the socket. it just returns 0 and i don't know why.

can someone help me to take a look on it.

script

Global $MAX_CONNECTION = 4
Global $CurrentSocket
Global $ListenSocket
Global $ConnectedSocket[$MAX_CONNECTION+1]



$TCP = TCPStartup()
If $TCP = 0 Then
    MsgBox(0, "Error", "Unable to startup TCP Services!")
    Exit
EndIf

$ListenSocket = TCPListen(@IPAddress1,7000,$MAX_CONNECTION)
If $ListenSocket = -1 Then
    MsgBox(0, "ERROR", "Unable to start listening on port 1777")
    Exit
EndIf
$CurrentSocket = GetFreeSocket()

While 1
    $ConnectedSocket[$CurrentSocket] = TCPAccept($ListenSocket)
    If $ConnectedSocket[$CurrentSocket] <> -1 Then
        $CurrentSocket = GetFreeSocket()
    EndIf
    If $CurrentSocket = "SERVER_FULL" Then Call("ServerError")
    For $INDEX = 0 To $MAX_CONNECTION
        If $ConnectedSocket[$INDEX] <> -1 Or $ConnectedSocket[$INDEX] <> "" Then
            $Recv = TCPRecv($ConnectedSocket[$INDEX],1024)
            If $Recv <> "" Then                             
                If $Recv = "EXIT" Then Call("Quit")
                If $Recv = "GET_SOCKET" Then TCPSend($ConnectedSocket[$INDEX],$CurrentSocket)
            EndIf
        EndIf
    Next
    Sleep(20)
WEnd

Func GetFreeSocket()
    $FREE = "SERVER_FULL"
    For $INDEX = $MAX_CONNECTION To 0 Step -1
        If $ConnectedSocket[$INDEX] = -1 Or $ConnectedSocket[$INDEX] = "" Then $FREE = $INDEX
    Next
    Return $FREE
EndFunc

Func ServerError()
    TCPSend($ConnectedSocket[$MAX_CONNECTION],"The server is full. Please try again later")
    TCPCloseSocket($ConnectedSocket[$MAX_CONNECTION])
EndFunc

Func _TCP_Server_ClientIP($ConnectedSocket[$CurrentSocket])     
    
    Local $pSocketAddress, $aReturn
    
    $pSocketAddress = DllStructCreate("short;ushort;uint;char[8]")
    $aReturn = DllCall("Ws2_32.dll", "int", "getpeername", "int", $ConnectedSocket[$CurrentSocket], "ptr", DllStructGetPtr($pSocketAddress), "int*", DllStructGetSize($pSocketAddress))
    If @error Or $aReturn[0] <> 0 Then Return 0
    
    $aReturn = DllCall("Ws2_32.dll", "str", "inet_ntoa", "int", DllStructGetData($pSocketAddress, 3))
    If @error Then Return 0
    
    $pSocketAddress = 0
    
    Return $aReturn[0]
    
EndFunc

i received error on this line when trying to return socket connection to IP.

ERROR: _TCP_Server_ClientIP() called with wrong number of args.

Func _TCP_Server_ClientIP($ConnectedSocket[$CurrentSocket])

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

hello to all. i have this script i can't remember to whom i got this script. its a server script that accepts multiple client connection. script works fine and my client successfully connects to it. my problem is that, i want to return the IP of every successful client connection. there is a function i got that converts IP to socket but i can't return any IP of the socket. it just returns 0 and i don't know why.

can someone help me to take a look on it.

script

Global $MAX_CONNECTION = 4
Global $CurrentSocket
Global $ListenSocket
Global $ConnectedSocket[$MAX_CONNECTION+1]



$TCP = TCPStartup()
If $TCP = 0 Then
    MsgBox(0, "Error", "Unable to startup TCP Services!")
    Exit
EndIf

$ListenSocket = TCPListen(@IPAddress1,7000,$MAX_CONNECTION)
If $ListenSocket = -1 Then
    MsgBox(0, "ERROR", "Unable to start listening on port 1777")
    Exit
EndIf
$CurrentSocket = GetFreeSocket()

While 1
    $ConnectedSocket[$CurrentSocket] = TCPAccept($ListenSocket)
    If $ConnectedSocket[$CurrentSocket] <> -1 Then
        $CurrentSocket = GetFreeSocket()
    EndIf
    If $CurrentSocket = "SERVER_FULL" Then Call("ServerError")
    For $INDEX = 0 To $MAX_CONNECTION
        If $ConnectedSocket[$INDEX] <> -1 Or $ConnectedSocket[$INDEX] <> "" Then
            $Recv = TCPRecv($ConnectedSocket[$INDEX],1024)
            If $Recv <> "" Then                             
                If $Recv = "EXIT" Then Call("Quit")
                If $Recv = "GET_SOCKET" Then TCPSend($ConnectedSocket[$INDEX],$CurrentSocket)
            EndIf
        EndIf
    Next
    Sleep(20)
WEnd

Func GetFreeSocket()
    $FREE = "SERVER_FULL"
    For $INDEX = $MAX_CONNECTION To 0 Step -1
        If $ConnectedSocket[$INDEX] = -1 Or $ConnectedSocket[$INDEX] = "" Then $FREE = $INDEX
    Next
    Return $FREE
EndFunc

Func ServerError()
    TCPSend($ConnectedSocket[$MAX_CONNECTION],"The server is full. Please try again later")
    TCPCloseSocket($ConnectedSocket[$MAX_CONNECTION])
EndFunc

Func _TCP_Server_ClientIP($ConnectedSocket[$CurrentSocket])     
    
    Local $pSocketAddress, $aReturn
    
    $pSocketAddress = DllStructCreate("short;ushort;uint;char[8]")
    $aReturn = DllCall("Ws2_32.dll", "int", "getpeername", "int", $ConnectedSocket[$CurrentSocket], "ptr", DllStructGetPtr($pSocketAddress), "int*", DllStructGetSize($pSocketAddress))
    If @error Or $aReturn[0] <> 0 Then Return 0
    
    $aReturn = DllCall("Ws2_32.dll", "str", "inet_ntoa", "int", DllStructGetData($pSocketAddress, 3))
    If @error Then Return 0
    
    $pSocketAddress = 0
    
    Return $aReturn[0]
    
EndFunc

i received error on this line when trying to return socket connection to IP.

ERROR: _TCP_Server_ClientIP() called with wrong number of args.

Func _TCP_Server_ClientIP($ConnectedSocket[$CurrentSocket])

if a client is succes full connected just let him sent the ip adres thats the way i do it and thats works fine

#include <Inet.au3>
$PublicIP = _GetIP()
MsgBox(0, "IP Address", "Your IP Address is: " & $PublicIP)

_getip() is the macro to get the external ip adres

Edited by yucatan

Share this post


Link to post
Share on other sites

lol. wat.

Func _TCP_Server_ClientIP($aSock)        
    
    Local $pSocketAddress, $aReturn
    
    $pSocketAddress = DllStructCreate("short;ushort;uint;char[8]")
    $aReturn = DllCall("Ws2_32.dll", "int", "getpeername", "int", $aSock, "ptr", DllStructGetPtr($pSocketAddress), "int*", DllStructGetSize($pSocketAddress))
    If @error Or $aReturn[0] <> 0 Then Return 0
    
    $aReturn = DllCall("Ws2_32.dll", "str", "inet_ntoa", "int", DllStructGetData($pSocketAddress, 3))
    If @error Then Return 0
    
    $pSocketAddress = 0
    
    Return $aReturn[0]
    
EndFunc

Share this post


Link to post
Share on other sites

if a client is succes full connected just let him sent the ip adres thats the way i do it and thats works fine

#include <Inet.au3>
$PublicIP = _GetIP()
MsgBox(0, "IP Address", "Your IP Address is: " & $PublicIP)

_getip() is the macro to get the external ip adres

hey there yucatan. thanks for the reply. i'll try this one. i didn't know that there is macro for external ip. :P

Share this post


Link to post
Share on other sites

lol. wat.

Func _TCP_Server_ClientIP($aSock)        
    
    Local $pSocketAddress, $aReturn
    
    $pSocketAddress = DllStructCreate("short;ushort;uint;char[8]")
    $aReturn = DllCall("Ws2_32.dll", "int", "getpeername", "int", $aSock, "ptr", DllStructGetPtr($pSocketAddress), "int*", DllStructGetSize($pSocketAddress))
    If @error Or $aReturn[0] <> 0 Then Return 0
    
    $aReturn = DllCall("Ws2_32.dll", "str", "inet_ntoa", "int", DllStructGetData($pSocketAddress, 3))
    If @error Then Return 0
    
    $pSocketAddress = 0
    
    Return $aReturn[0]
    
EndFunc
hello manadar. its me again john. is this the same what you posted?

Func _TCP_Server_ClientIP($ConnectedSocket[$CurrentSocket])     
    
    Local $pSocketAddress, $aReturn
    
    $pSocketAddress = DllStructCreate("short;ushort;uint;char[8]")
    $aReturn = DllCall("Ws2_32.dll", "int", "getpeername", "int", $ConnectedSocket[$CurrentSocket], "ptr", DllStructGetPtr($pSocketAddress), "int*", DllStructGetSize($pSocketAddress))
    If @error Or $aReturn[0] <> 0 Then Return 0
    
    $aReturn = DllCall("Ws2_32.dll", "str", "inet_ntoa", "int", DllStructGetData($pSocketAddress, 3))
    If @error Then Return 0
    
    $pSocketAddress = 0
    
    Return $aReturn[0]
    
EndFunc

on your code, socket is defined only as one connection. this works per one socket connection. i am able to get IP.

Func _TCP_Server_ClientIP($aSock)

but on the script i posted, socket is defined as an array wherein i'm trying to get IP of every successful client socket connection. this returns only 0

Func _TCP_Server_ClientIP($ConnectedSocket[$CurrentSocket])

can you help me with this?

THANKYOU.

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

hello manadar. its me again john. is this the same what you posted?

Func _TCP_Server_ClientIP($ConnectedSocket[$CurrentSocket])     
    
    Local $pSocketAddress, $aReturn
    
    $pSocketAddress = DllStructCreate("short;ushort;uint;char[8]")
    $aReturn = DllCall("Ws2_32.dll", "int", "getpeername", "int", $ConnectedSocket[$CurrentSocket], "ptr", DllStructGetPtr($pSocketAddress), "int*", DllStructGetSize($pSocketAddress))
    If @error Or $aReturn[0] <> 0 Then Return 0
    
    $aReturn = DllCall("Ws2_32.dll", "str", "inet_ntoa", "int", DllStructGetData($pSocketAddress, 3))
    If @error Then Return 0
    
    $pSocketAddress = 0
    
    Return $aReturn[0]
    
EndFunc

on your code, socket is defined only as one connection. this works per one socket connection. i am able to get IP.

Func _TCP_Server_ClientIP($aSock)

but on the script i posted, socket is defined as an array wherein i'm trying to get IP of every successful client socket connection. this returns only 0

Func _TCP_Server_ClientIP($ConnectedSocket[$CurrentSocket])

can you help me with this?

THANKYOU.

I understand your thought flow but this is simply invalid syntax. If you want to call the function on an array, you will need to loop through the array and call it for each element in the array.

Like you've already done here:

For $INDEX = 0 To $MAX_CONNECTION
        If $ConnectedSocket[$INDEX] <> -1 Or $ConnectedSocket[$INDEX] <> "" Then
            $Ip = _TCP_Server_ClientIP($ConnectedSocket[$CurrentSocket])
            ; perform something on the IP here
        EndIf
    Next
Edited by Manadar

Share this post


Link to post
Share on other sites

I understand your thought flow but this is simply invalid syntax. If you want to call the function on an array, you will need to loop through the array and call it for each element in the array.

Like you've already done here:

For $INDEX = 0 To $MAX_CONNECTION
        If $ConnectedSocket[$INDEX] <> -1 Or $ConnectedSocket[$INDEX] <> "" Then
            $Ip = _TCP_Server_ClientIP($ConnectedSocket[$CurrentSocket])
            ; perform something on the IP here
        EndIf
    Next

THANKS MANADAR. i have not thought of thAT. :P i'll try this one.

thnanks very much

Share this post


Link to post
Share on other sites

hey there yucatan. thanks for the reply. i'll try this one. i didn't know that there is macro for external ip. :P

i always use my _getip() and let that send by the client a easy good way always works and not that hard to create

still havent solved my problem with my binary sending but yeah thats another story

Share this post


Link to post
Share on other sites

i always use my _getip() and let that send by the client a easy good way always works and not that hard to create

still havent solved my problem with my binary sending but yeah thats another story

hi there yucatan. i have tried running the _getip() but it does not returning any value. it is running but i could not received any

can you please give me sample as i don't understand much where will i exactly put that code.

thanks.

Share this post


Link to post
Share on other sites

THANKS MANADAR. i have not thought of thAT. :P i'll try this one.

thnanks very much

hello manadar. i tried the script, but it returns only 0. i just put a message box to check if script will return the IP of socket connection but it just returns a value of 0.

For $INDEX = 0 To $MAX_CONNECTION
        If $ConnectedSocket[$INDEX] <> -1 Or $ConnectedSocket[$INDEX] <> "" Then
            $Ip = _TCP_Server_ClientIP($ConnectedSocket[$CurrentSocket])
            MsgBox(0, "IP", $Ip)
; perform something on the IP here
        EndIf
    Next

i'm really confused to where i will insert this code. need help with this.

thanks.

Share this post


Link to post
Share on other sites

hello manadar. i tried the script, but it returns only 0. i just put a message box to check if script will return the IP of socket connection but it just returns a value of 0.

For $INDEX = 0 To $MAX_CONNECTION
        If $ConnectedSocket[$INDEX] <> -1 Or $ConnectedSocket[$INDEX] <> "" Then
            $Ip = _TCP_Server_ClientIP($ConnectedSocket[$CurrentSocket])
            MsgBox(0, "IP", $Ip)
; perform something on the IP here
        EndIf
    Next

i'm really confused to where i will insert this code. need help with this.

thanks.

u need to implement that code in the server

Share this post


Link to post
Share on other sites

i'm really confused to where i will insert this code. need help with this.

thanks.

John, you said you wanted to get the IP of a client. I've showed you how to get the IP of one client. Then you asked how to get them of all clients and I showed you how to get the IPs of all clients.

It is up to you to decide where you want the code because it is your program. No one else can implement the code in your program because you haven't said what you want to do with the IP and when you want to do something with it.

Share this post


Link to post
Share on other sites

u need to implement that code in the server

hi yucatan. yap i put in already in the server. but it always returns 0 value. is there a workaround on this?

i have tried this also..but this returns...0.0.0.0 :P

For $INDEX = 0 To $MAX_CONNECTION
                If $ConnectedSocket[$INDEX] <> -1 Or $ConnectedSocket[$INDEX] <> "" Then
                    $Ipadd = SocketToIP($ConnectedSocket[$CurrentSocket])                   
                    MsgBox(0, "IP", $Ipadd)
           ; perform something on the IP here
                EndIf
            Next


Func SocketToIP($Ipadd)
    Local $sockaddr, $ip

    $sockaddr = DllStructCreate("short;ushort;uint;char[8]")

    $ip = DllCall("Ws2_32.dll", "int", "getpeername", "int", $Ipadd,"ptr", DllStructGetPtr($sockaddr), "ptr", DllStructGetSize($sockaddr))
        $ip = DllCall("Ws2_32.dll", "str", "inet_ntoa", "int", DllStructGetData($sockaddr, 3))
        
    ;If @error Then
            
        If Not @error Then 
        $ip = $ip[0]
        Else
        $ip = 0
    EndIf

    $sockaddr = 0
    Return $ip
EndFunc

Share this post


Link to post
Share on other sites

hi yucatan. yap i put in already in the server. but it always returns 0 value. is there a workaround on this?

i have tried this also..but this returns...0.0.0.0 :P

For $INDEX = 0 To $MAX_CONNECTION
                If $ConnectedSocket[$INDEX] <> -1 Or $ConnectedSocket[$INDEX] <> "" Then
                    $Ipadd = SocketToIP($ConnectedSocket[$CurrentSocket])                   
                    MsgBox(0, "IP", $Ipadd)
          ; perform something on the IP here
                EndIf
            Next


Func SocketToIP($Ipadd)
    Local $sockaddr, $ip

    $sockaddr = DllStructCreate("short;ushort;uint;char[8]")

    $ip = DllCall("Ws2_32.dll", "int", "getpeername", "int", $Ipadd,"ptr", DllStructGetPtr($sockaddr), "ptr", DllStructGetSize($sockaddr))
        $ip = DllCall("Ws2_32.dll", "str", "inet_ntoa", "int", DllStructGetData($sockaddr, 3))
        
;If @error Then
            
        If Not @error Then 
        $ip = $ip[0]
        Else
        $ip = 0
    EndIf

    $sockaddr = 0
    Return $ip
EndFunc

hmm i dont know that mate just let the client sent _getip() problem solved

Share this post


Link to post
Share on other sites

John, you said you wanted to get the IP of a client. I've showed you how to get the IP of one client. Then you asked how to get them of all clients and I showed you how to get the IPs of all clients.

It is up to you to decide where you want the code because it is your program. No one else can implement the code in your program because you haven't said what you want to do with the IP and when you want to do something with it.

hi manadar. thanks for that. yes for one client socket connection it is returning the IP address. this works great. i have tried to put the script so as to track the IP of each successful client socket connection but it is returning a value of zero. forgive me if i put the script in the wrong part of the server script. but i can't seem to work on getting IP of each client socket connection. i put a Message box to check that it will return successful IP but it returns zero.

here is what i tried. i appreciate if you can correct me in writing the code

Global $MAX_CONNECTION = 4
Global $CurrentSocket
Global $ListenSocket
Global $ConnectedSocket[$MAX_CONNECTION+1]

$TCP = TCPStartup()
If $TCP = 0 Then
    MsgBox(0, "Error", "Unable to startup TCP Services!")
    Exit
EndIf

$ListenSocket = TCPListen(@IPAddress1,7000,$MAX_CONNECTION)
If $ListenSocket = -1 Then
    MsgBox(0, "ERROR", "Unable to start listening on port 1777")
    Exit
EndIf
$CurrentSocket = GetFreeSocket()

While 1
    $ConnectedSocket[$CurrentSocket] = TCPAccept($ListenSocket)
    If $ConnectedSocket[$CurrentSocket] <> -1 Then
        $CurrentSocket = GetFreeSocket()
    EndIf
    If $CurrentSocket = "SERVER_FULL" Then Call("ServerError")

    For $INDEX = 0 To $MAX_CONNECTION
        If $ConnectedSocket[$INDEX] <> -1 Or $ConnectedSocket[$INDEX] <> "" Then
            $Recv = TCPRecv($ConnectedSocket[$INDEX],1024)

            $Ip = _TCP_Server_ClientIP($ConnectedSocket[$CurrentSocket])
            MsgBox(0, "IP", $Ip)
            
            If $Recv <> "" Then
                TrayTip("Received new connection from client PC!", $RECV, 10, 1)
                
                If $Recv = "EXIT" Then Call("Quit")
                If $Recv = "GET_SOCKET" Then TCPSend($ConnectedSocket[$INDEX],$CurrentSocket)
            EndIf
        EndIf
    Next
    Sleep(20)
WEnd

Func _TCP_Server_ClientIP($Ip)      
    
    Local $pSocketAddress, $aReturn
    
    $pSocketAddress = DllStructCreate("short;ushort;uint;char[8]")
    $aReturn = DllCall("Ws2_32.dll", "int", "getpeername", "int", $Ip, "ptr", DllStructGetPtr($pSocketAddress), "int*", DllStructGetSize($pSocketAddress))
    If @error Or $aReturn[0] <> 0 Then Return 0
    
    $aReturn = DllCall("Ws2_32.dll", "str", "inet_ntoa", "int", DllStructGetData($pSocketAddress, 3))
    If @error Then Return 0
    
    $pSocketAddress = 0
    
    Return $aReturn[0]
    
EndFunc

Func GetFreeSocket()
    $FREE = "SERVER_FULL"
    For $INDEX = $MAX_CONNECTION To 0 Step -1
        If $ConnectedSocket[$INDEX] = -1 Or $ConnectedSocket[$INDEX] = "" Then $FREE = $INDEX
    Next
    Return $FREE
EndFunc

Func ServerError()
    TCPSend($ConnectedSocket[$MAX_CONNECTION],"The server is full. Please try again later")
    TCPCloseSocket($ConnectedSocket[$MAX_CONNECTION])
EndFunc

Func Quit()
    TCPCloseSocket($ListenSocket)
    TCPShutdown()
    Exit
EndFunc

Share this post


Link to post
Share on other sites

John, you said you wanted to get the IP of a client. I've showed you how to get the IP of one client. Then you asked how to get them of all clients and I showed you how to get the IPs of all clients.

It is up to you to decide where you want the code because it is your program. No one else can implement the code in your program because you haven't said what you want to do with the IP and when you want to do something with it.

basically this will be use to support user. simply i will track each client IP address whenever they successfully connects. in my client code, there is a hotkey which does the connection then a GUI will pop on server, tracking each IP of the client whoever initiates the connection with a button to connect remotely. This is the part where i got troubled. i can only track 1 client IP. But with multiple connection, i could not track each successful client IP.

Then on the server side, it is ready to do the task by initiating a remote connection to the client.

Share this post


Link to post
Share on other sites

hmm i dont know that mate just let the client sent _getip() problem solved

hi yucatan. i can't seem the _getip() working. i put it in the client code but server seems not receiving the IP of the client.

forgive me if i am using the script wrong because i am a newbie here. i appreciate if you can help me with this...

here is my client code...

#include <Inet.au3>
HotKeySet('{PAUSE}', 'Connect1')

Go()

Func Go()
    TCPStartup()
    While 1
        Sleep(500)
    WEnd
EndFunc

Func Connect1()
    
    $socket = TCPConnect("192.168.1.2", 7000)
    If $socket = -1 Then
        ConsoleWrite('Cannot connect' & @CRLF)      
            
        For $a=1 To 5
            Sleep(500)
            ConsoleWrite('Reconnecting in: ' & $a & ' second(s)' & @CRLF)
        Next
        ;will connect to other server           
    Else
        ConsoleWrite('Connected' & @CRLF)

        $PublicIP = _GetIP()
        TCPSend($socket, $PublicIP)
        MsgBox(0, "IP Address", "Your IP Address is: " & $PublicIP)     
    EndIf
EndFunc

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0