Jump to content

Recommended Posts

Posted

i m trying to make some simple VNC like server and client but there is a problem, i want to list connected ip addresses on my Listview gui item, but it shows only server ip,tried over 3 computers when they are all connected to my server there is just my own server IP address i can see.i want to show every single one ip connected to server

here my code

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <GUIListBox.au3>
#include <GuiStatusBar.au3>
#include <StaticConstants.au3>
#include <TabConstants.au3>
#include <WindowsConstants.au3>
Global $List1
Global $recv
Global $ipclient
Global $BagliSoket
;#####################################################
$ipserver = @IPAddress1
$port = 33891
TCPStartup()
$AnaSoket = TCPListen($ipserver, $port)
If $AnaSoket = -1 Then MsgBox(0, "", "Soket Hatası")
$BagliSoket = -1
$BagliSoket = TCPConnect($ipserver, $port)
$ipclient = _SocketGetIP($BagliSoket)
Do
    $BagliSoket = TCPAccept($AnaSoket)
Until $BagliSoket <> -1
$recv = TCPRecv($BagliSoket, 2048)
If $BagliSoket <> -1 Then TCPCloseSocket($BagliSoket)
;#####################################################
#Region ### START Koda GUI section ### Form=c:\users\sercan\documents\server.kxf
$Form1_1 = GUICreate("Form1", 630, 423, 192, 145)
$StatusBar1 = _GUICtrlStatusBar_Create($Form1_1)
_GUICtrlStatusBar_SetMinHeight($StatusBar1, 25)
$List1 = GUICtrlCreateListView("Makinalar", 4, 24, 169, 344)
$ilgilen = GUICtrlCreateButton("Bağlan", 4, 368, 170, 25, $WS_GROUP)
$Label1 = GUICtrlCreateLabel("Açık ve Bağlı Bilgisayarlar", 28, 7, 124, 17)
$Tab1 = GUICtrlCreateTab(177, 8, 450, 385)
GUICtrlSetResizing(-1, $GUI_DOCKWIDTH + $GUI_DOCKHEIGHT)
$Programlar = GUICtrlCreateTabItem("Programlar")
GUICtrlSetState(-1, $GUI_SHOW)
$list = ProcessList()
$List2 = GUICtrlCreateList("", 184, 40, 433, 318)
For $i = 1 To $list[0][0]
    GUICtrlSetData($List2, $list[$i][0])
Next
$Kapat = GUICtrlCreateButton("Kapat", 184, 360, 217, 25, $WS_GROUP)
$Calistir = GUICtrlCreateButton("Yenile", 400, 360, 217, 25, $WS_GROUP)
$Sistem = GUICtrlCreateTabItem("Sistem")
$Label2 = GUICtrlCreateLabel("Makina", 184, 32, 39, 17)
$Makina = GUICtrlCreateInput(@UserName, 184, 48, 433, 21, BitOR($ES_AUTOHSCROLL, $ES_READONLY))
$Label3 = GUICtrlCreateLabel("İşletim Sistemi", 184, 72, 69, 17)
$OS = GUICtrlCreateInput(@OSVersion & @CPUArch, 184, 88, 433, 21, BitOR($ES_AUTOHSCROLL, $ES_READONLY))
$Label4 = GUICtrlCreateLabel("IP Adresi", 184, 112, 46, 17)
$ipadresi = GUICtrlCreateInput(@IPAddress1, 184, 128, 433, 21, BitOR($ES_AUTOHSCROLL, $ES_READONLY))
GUICtrlCreateTabItem("")
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Kapat
            $sercan = GUICtrlRead($List2)
            ProcessClose($sercan)
        Case $Calistir
            $list23 = ProcessList()
            For $i = 1 To $list23[0][0]
                GUICtrlSetData($List2, $list23[$i][0])
            Next
        Case $ilgilen
            GUICtrlCreateListViewItem($ipclient, $List1)

    EndSwitch

WEnd
TCPShutdown()


; Function to return IP Address from a connected socket.
;----------------------------------------------------------------------
Func _SocketGetIP($Data)
    Local $Struct, $Return
    $Struct = DllStructCreate('short;ushort;uint;char[8]')
    $Return = DllCall('Ws2_32.dll', 'int', 'getpeername', 'int', $Data, 'ptr', DllStructGetPtr($Struct), 'int*', DllStructGetSize($Struct))
    If @error Or $Return[0] <> 0 Then Return 0
    $Return = DllCall('Ws2_32.dll', 'str', 'inet_ntoa', 'int', DllStructGetData($Struct, 3))
    If @error Then Return 0
    $Struct = 0
    Return $Return[0]
EndFunc   ;==>_SocketGetIP
Posted (edited)

something like this? just store the IP's in an array?

tcpstartup ()
$mainsocket = tcplisten (@IPAddress1,4444)
Dim $socketlist[100][2]
For $n = 0 To UBound ($socketlist)-1
    $socketlist[$n][0] = 0
Next
while 1
    _IncommingConnection ()
wend
For $n = 0 to ubound ($socketlist)-1
    TCPClosesocket($socketlist[$n][0])
next
tcpclosesocket ($mainsocket)
TCPshutdown ()
exit
func _IncommingConnection ()
    local $n, $socket = tcpaccept ($mainsocket)
    If $socket = -1 then return
    If SocketToIP ($socket) = TCPNameToIP ('Google.ca') Then Return ;Line Of Interest!!!
    For $n = 0 To UBound ($socketlist)-1
        If $socketlist[$n][0] = 0 Then 
                $socketlist[$n][0] = $socket
                $socketlist[$n][1] = SocketToIP ($socket)
        endif
    Next
EndFunc
Func SocketToIP($SHOCKET)
    Local $sockaddr, $aRet
    $sockaddr = DllStructCreate('short;ushort;uint;char[8]')
    $aRet = DllCall('Ws2_32.dll', 'int', 'getpeername', 'int', $SHOCKET,'ptr', DllStructGetPtr($sockaddr),'int*', DllStructGetSize($sockaddr))
    If Not @error And $aRet[0] = 0 Then
        $aRet = DllCall('Ws2_32.dll', 'str', 'inet_ntoa', 'int', DllStructGetData($sockaddr, 3))
        If Not @error Then $aRet = $aRet[0]
    Else
        $aRet = 0
    EndIf
    $sockaddr = 0
    Return $aRet
EndFunc
Edited by CodyBarrett
Posted (edited)

im unable to convert your code into mine, can you help a little? its my first time with tcp functions

Edited by Sercankd
Posted

try this. its a bit better.

Hotkeyset ('#x','End') ;Sets a hotkey to close the script.
tcpstartup () ;Starts TCP services (only need to call this once).
$mainsocket = tcplisten (@IPAddress1,4444) ;This creates a listening ear (socket) for incomming connections.
Dim $socketlist[100] ;creates an array to capture the connected sockets, 0-99 including 0 it would be 100 possible elements.
For $n = 0 To UBound ($socketlist)-1 ;For beggining To End Of Array.
    $socketlist[$n] = 0 ;Sets The Elements To 0.
Next
while 1
    _IncommingConnection () ;alternately you could use ADLIBREGISTER ('_IncommingConnection') which might actually be better but for now this is good enough.
wend
func _IncommingConnection ()
    local $n, $socket = tcpaccept ($mainsocket) ;tcpaccept accepts a pending connection. (Called From TCPCONNECT())
    If $socket = -1 then return ;If error (no pending connections, then return).
    ;------------------LINE OF INTEREST---------------------------------
    If SocketToIP ($socket) = TCPNameToIP ('Google.ca') Then ;Use this line for whatever IP you want to block.
        TCPCloseSocket ($socket) ;Closes the established Socket because you blocked its IP.
        Return
    EndIf
    ;-------------------------------------------------------------------
    For $n = 0 To UBound ($socketlist)-1
        If $socketlist[$n] = 0 Then ;searching for an empty elemtent in the array.
                $socketlist[$n] = $socket ;once the first empty element is found then use it.
                Return
        endif
    Next
    TCPCloseSocket ($socket) ;This is called if there is no open elements.
EndFunc
Func SocketToIP($SHOCKET) ;im not entirely sure what this function does but it returns an IP or 0.
    Local $sockaddr, $aRet
    $sockaddr = DllStructCreate('short;ushort;uint;char[8]')
    $aRet = DllCall('Ws2_32.dll', 'int', 'getpeername', 'int', $SHOCKET,'ptr', DllStructGetPtr($sockaddr),'int*', DllStructGetSize($sockaddr))
    If Not @error And $aRet[0] = 0 Then
        $aRet = DllCall('Ws2_32.dll', 'str', 'inet_ntoa', 'int', DllStructGetData($sockaddr, 3))
        If Not @error Then $aRet = $aRet[0]
    Else
        $aRet = 0
    EndIf
    $sockaddr = 0
    Return $aRet
EndFunc
Func End ()
    For $n = 0 to ubound ($socketlist)-1
        If $socketlist[$n] = 0 Then
            TCPClosesocket($socketlist[$n]) ;Closes EVERY Socket that is still connected.
        EndIf
    next
    tcpclosesocket ($mainsocket) ;CLoses the Listening EAR.
    TCPshutdown () ;shuts down the TCP services.
EndFunc
Posted

what have you used to connect to them? what is your client code?

Posted (edited)

client

Opt('MustDeclareVars', 1)

;==============================================
;==============================================
;CLIENT! Start Me after starting the SERVER!!!!!!!!!!!!!!!
;==============================================
;==============================================

Example()

Func Example()
    ; Set Some reusable info
    ;--------------------------
    Local $ConnectedSocket, $szData
    ; Set $szIPADDRESS to wherever the SERVER is. We will change a PC name into an IP Address
;   Local $szServerPC = @ComputerName
;   Local $szIPADDRESS = TCPNameToIP($szServerPC)
    Local $szIPADDRESS = "192.168.2.16"
    Local $nPORT = 33891

    ; Start The TCP Services
    ;==============================================
    TCPStartup()

    ; Initialize a variable to represent a connection
    ;==============================================
    $ConnectedSocket = -1

    ;Attempt to connect to SERVER at its IP and PORT 33891
    ;=======================================================
    $ConnectedSocket = TCPConnect($szIPADDRESS, $nPORT)

    ; If there is an error... show it
    If @error Then
        MsgBox(4112, "Error", "TCPConnect failed with WSA error: " & @error)
        ; If there is no error loop an inputbox for data
        ;   to send to the SERVER.
    Else
        ;Loop forever asking for data to send to the SERVER
        While 1
            ; InputBox for data to transmit
            ; If they cancel the InputBox or leave it blank we exit our forever loop
            $szData = "Sercan"
            If @error Or $szData = "" Then ExitLoop

            ; We should have data in $szData... lets attempt to send it through our connected socket.
            TCPSend($ConnectedSocket, $szData)

            ; If the send failed with @error then the socket has disconnected
            ;----------------------------------------------------------------
            If @error Then ExitLoop
        WEnd
    EndIf
EndFunc   ;==>Example

server

Edited by Sercankd
Posted (edited)

these worked fine for me. i only changed a few lines, a console write in the INCOMING CONNECTION func, and you IP and PORT numbers. you had the client connecting to something that isn't there.

Opt('MustDeclareVars', 1)

;==============================================
;==============================================
;CLIENT! Start Me after starting the SERVER!!!!!!!!!!!!!!!
;==============================================
;==============================================

Example()

Func Example()
    ; Set Some reusable info
    ;--------------------------
    Local $ConnectedSocket, $szData
    ; Set $szIPADDRESS to wherever the SERVER is. We will change a PC name into an IP Address
;   Local $szServerPC = @ComputerName
;   Local $szIPADDRESS = TCPNameToIP($szServerPC)
;~     Local $szIPADDRESS = "192.168.2.16"
    Local $szIPADDRESS = @IPAddress1
    Local $nPORT = 4444
;~     Local $nPORT = 33891

    ; Start The TCP Services
    ;==============================================
    TCPStartup()

    ; Initialize a variable to represent a connection
    ;==============================================
    $ConnectedSocket = -1

    ;Attempt to connect to SERVER at its IP and PORT 33891
    ;=======================================================
    $ConnectedSocket = TCPConnect($szIPADDRESS, $nPORT)

    ; If there is an error... show it
    If @error Then
        MsgBox(4112, "Error", "TCPConnect failed with WSA error: " & @error)
        ; If there is no error loop an inputbox for data
        ;   to send to the SERVER.
    Else
        ;Loop forever asking for data to send to the SERVER
        While 1
            ; InputBox for data to transmit
            ; If they cancel the InputBox or leave it blank we exit our forever loop
            $szData = "Sercan"
            If @error Or $szData = "" Then ExitLoop

            ; We should have data in $szData... lets attempt to send it through our connected socket.
            TCPSend($ConnectedSocket, $szData)

            ; If the send failed with @error then the socket has disconnected
            ;----------------------------------------------------------------
            If @error Then ExitLoop
        WEnd
    EndIf
EndFunc   ;==>Example
Hotkeyset ('#x','End') ;Sets a hotkey to close the script.
tcpstartup () ;Starts TCP services (only need to call this once).
$mainsocket = tcplisten (@IPAddress1,4444) ;This creates a listening ear (socket) for incomming connections.
Dim $socketlist[100] ;creates an array to capture the connected sockets, 0-99 including 0 it would be 100 possible elements.
For $n = 0 To UBound ($socketlist)-1 ;For beggining To End Of Array.
    $socketlist[$n] = 0 ;Sets The Elements To 0.
Next
while 1
    _IncommingConnection () ;alternately you could use ADLIBREGISTER ('_IncommingConnection') which might actually be better but for now this is good enough.
wend
func _IncommingConnection ()
    local $n, $socket = tcpaccept ($mainsocket) ;tcpaccept accepts a pending connection. (Called From TCPCONNECT())
    If $socket = -1 then return ;If error (no pending connections, then return).
    ;------------------LINE OF INTEREST---------------------------------
    If SocketToIP ($socket) = TCPNameToIP ('Google.ca') Then ;Use this line for whatever IP you want to block.
        TCPCloseSocket ($socket) ;Closes the established Socket because you blocked its IP.
        Return
    EndIf
    ;-------------------------------------------------------------------
    For $n = 0 To UBound ($socketlist)-1
        If $socketlist[$n] = 0 Then ;searching for an empty elemtent in the array.
                $socketlist[$n] = $socket ;once the first empty element is found then use it.
                ConsoleWrite ('NEW CLIENT:' & $socket & @TAB & 'IP:' & SocketToIP ($socket) & @CR)
                Return
        endif
    Next
    TCPCloseSocket ($socket) ;This is called if there is no open elements.
EndFunc
Func SocketToIP($SHOCKET) ;im not entirely sure what this function does but it returns an IP or 0.
    Local $sockaddr, $aRet
    $sockaddr = DllStructCreate('short;ushort;uint;char[8]')
    $aRet = DllCall('Ws2_32.dll', 'int', 'getpeername', 'int', $SHOCKET,'ptr', DllStructGetPtr($sockaddr),'int*', DllStructGetSize($sockaddr))
    If Not @error And $aRet[0] = 0 Then
        $aRet = DllCall('Ws2_32.dll', 'str', 'inet_ntoa', 'int', DllStructGetData($sockaddr, 3))
        If Not @error Then $aRet = $aRet[0]
    Else
        $aRet = 0
    EndIf
    $sockaddr = 0
    Return $aRet
EndFunc
Func End ()
    For $n = 0 to ubound ($socketlist)-1
        If $socketlist[$n] = 0 Then
            TCPClosesocket($socketlist[$n]) ;Closes EVERY Socket that is still connected.
        EndIf
    next
    tcpclosesocket ($mainsocket) ;CLoses the Listening EAR.
    TCPshutdown () ;shuts down the TCP services.
EndFunc
Edited by CodyBarrett

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...