Sign in to follow this  
Followers 0
slaughter

Speed up TCPrec TCPsend

2 posts in this topic

When there is connected 3~4 users works fine

when it reaches 5~more user users starts fealing interupsts. it seams that scrips rec and send interval is to hight so while it is recieving data it does not send to other users.

alowed_ip.txt

192.168.*.*

vncList.txt

192.168.2.2
192.168.2.3
192.168.2.4
192.168.2.5
192.168.2.6
192.168.2.7
192.168.2.8

Code.au3

#include <GUIConstantsEx.au3>
#include <GUIConstants.au3>
#include <array.au3>
#include <inet.au3>
#include <String.au3>
#Include <File.au3>
#include <GuiListView.au3>
#include <Date.au3>
#include <Timers.au3>
Opt('MustDeclareVars', 0)

;==============================================
;==============================================
;SERVER!! Start Me First !!!!!!!!!!!!!!!
;==============================================
;==============================================





    Global $szIPADDRESS = InputBox("P o P :D", "IP Of server to listen", @IPAddress1)
    Global $nPORT = "5901"
    Global $MainSocket, $GOOEY, $szIP_Accepted1, $szIP_Accepted2,  $Free_socket, $msg, $recv1, $recv2, $Track, $hListView, $vnc_alowed
    Global $edit
    Global $server_to_adr
    Global $server_to_port = "5900"
    Global Const $MaxConnection = 100; Maximum user connections
    Global $ConnectedSocket1[ ($MaxConnection + 1) ]
    Global $ConnectedSocket2[ ($MaxConnection + 1) ]

    Dim $vnc_servers_tmp
    Dim $vnc_alowed
    _FileReadToArray("vncList.txt",$vnc_servers_tmp)
    _FileReadToArray("alowed_ip.txt",$vnc_alowed)

;Atvaidavimo array
    Global $gui_display
    Dim $gui_display[UBound($vnc_servers_tmp)][7]
;Serverio vidiniams skaiciavimams
    Global $vnc_servers
    Dim $vnc_servers[UBound($vnc_servers_tmp)][7]

For $Track=1 to $vnc_servers_tmp[0] Step 1
$vnc_servers[$Track][0] = $vnc_servers_tmp[$Track]
$vnc_servers[$Track][1] = -1
$gui_display[$Track][0] = $vnc_servers_tmp[$Track]
Next



;~ _ArrayDisplay($vnc_servers, "f")



Example()

Func Example()
   ; Set Some reusable info
   ; Set your Public IP address (@IPAddress1) here.
;   Local $szServerPC = @ComputerName
;   Local $szIPADDRESS = TCPNameToIP($szServerPC)


; Null all socets
;---------------------------------------------
    For $Track = 0 To $MaxConnection Step 1
        $ConnectedSocket1[$Track] = -1
        $ConnectedSocket2[$Track] = -1
    Next
;--------------------------------------------


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

   ; Create a Listening "SOCKET".
   ;   Using your IP Address and Port 33891.
   ;==============================================

  $MainSocket = TCPListen($szIPADDRESS, $nPORT)

   ; If the Socket creation fails, exit.
    If $MainSocket = -1 Then Exit


   ; Create a GUI for messages
   ;==============================================
    $GOOEY = GUICreate("Proxy Of Proxy :D (IP: " & $szIPADDRESS & ")", 800, 200)
;~     $edit = GUICtrlCreateEdit("", 10, 10, 480, 180)
    $hListView = GUICtrlCreateListView("", 10, 10, 780, 180)
        _GUICtrlListView_AddColumn($hListView, "VNC Server IP", 90)
        _GUICtrlListView_AddColumn($hListView, "S", 30)
        _GUICtrlListView_AddColumn($hListView, "Client IP", 90)
        _GUICtrlListView_AddColumn($hListView, "Connection Time", 150)
        _GUICtrlListView_AddColumn($hListView, " ", 50)
        _GUICtrlListView_AddColumn($hListView, " ", 50)
        _GUICtrlListView_AddArray($hListView, $gui_display)
    GUISetState()


   ; Initialize a variable to represent a connection
   ;==============================================
;~     $ConnectedSocket1 = -1
;~     $ConnectedSocket2 = -1


AdlibEnable("Gui_update", 5000)

   ; GUI Message Loop
   ;==============================================
    While 1

       ;Wait for and Accept a connection
   ;==============================================
    $Free_socket = SocketSearch()

$ConnectedSocket1[$Free_socket] = TCPAccept($MainSocket)

If $ConnectedSocket1[$Free_socket] <> -1 Then


;Check socket  usage
;~         Scan_sockets()
        $server_to_adr = find_vnc($Free_socket)
            _FileReadToArray("alowed_ip.txt",$vnc_alowed)
                match_ip_array($vnc_alowed, SocketToIP($ConnectedSocket1[$Free_socket]))
            if @error Then
                openSocket($Free_socket)
                write_log("Blocked: IP "&SocketToIP($ConnectedSocket1[$Free_socket])&" is not in list to accept")
            Else
                write_log("Acepted: IP "&SocketToIP($ConnectedSocket1[$Free_socket])&" is in list to accept")



    ; Connect to other host whre to send recieved data
            $ConnectedSocket2[$Free_socket] = TCPConnect($server_to_adr, $server_to_port)
    ; Get IP of client connecting
                $szIP_Accepted1 = SocketToIP($ConnectedSocket1[$Free_socket])
                $szIP_Accepted2 = SocketToIP($ConnectedSocket2[$Free_socket])
                $vnc_servers[$Free_socket+1][2]= $szIP_Accepted1
                $gui_display[$Free_socket+1][2]= $szIP_Accepted1
                $gui_display[$Free_socket+1][3]= @YEAR &"-"& @MON &"-"& @MDAY &"-"& @HOUR &":"& @MIN &":"& @SEC
                ConsoleWrite($gui_display[$Free_socket+1][3])
            EndIf
EndIf

        $msg = GUIGetMsg()

       ; GUI Closed
       ;--------------------
    If $msg = $GUI_EVENT_CLOSE Then ExitLoop


For $Track = 0 To $MaxConnection Step 1
    If $ConnectedSocket1[$Track] <> - 1 Then
        $vnc_id = Get_srv_id($Track)
    ; Try to receive (up to) 2048 bytes
       ;----------------------------------------------------------------
        $recv1 = TCPRecv($ConnectedSocket1[$Track], 2048)
        If @error Then
            openSocket($Track)
        EndIf

       ; If the receive failed with @error then the socket has disconnected
       ;----------------------------------------------------------------
;~         If @error Then ExitLoop
       ; Try to receive (up to) 2048 bytes
       ;----------------------------------------------------------------
        $recv2 = TCPRecv($ConnectedSocket2[$Track], 2048)
        If @error Then
            openSocket($Track)
        EndIf
        ; If the receive failed with @error then the socket has disconnected
       ;----------------------------------------------------------------
;~         If @error Then ExitLoop

       ; Update the edit control with what we have received
       ;----------------------------------------------------------------
        Local $ret

        If $recv1 <> "" Then
        $ret = TCPSend($ConnectedSocket2[$Track], $recv1)
        EndIf

        If $recv2 <> "" Then
        $ret = TCPSend($ConnectedSocket1[$Track], $recv2)
        EndIf

;~         If $ConnectedSocket1[$Track] = -1 Then
;~         TCPCloseSocket($ConnectedSocket1[$Track])
;~         Add_msg("User disconected " & SocketToIP($ConnectedSocket2[$Track]))
;~         EndIf
    EndIf

Next

WEnd

;~ 192.168.1.135





;~     Add_msg("Shudown..")
;~     TCPShutdown()
EndFunc  ;==>Example


Func find_vnc($SHOCKET)
    Local $Trackm
For $Trackm=1 to $vnc_servers_tmp[0] Step 1
    if $vnc_servers[$Trackm][1] = -1 Then
    $vnc_servers[$Trackm][1] = $SHOCKET
    Return $vnc_servers[$Trackm][0]
    EndIf
Next

EndFunc




Func openSocket($SHOCKETm)
    Local $iIndex , $Trackg
        write_log("User disconected Socket: " &$SHOCKETm& " IP: "& SocketToIP($ConnectedSocket1[$SHOCKETm]))
    TCPCloseSocket($ConnectedSocket2[$SHOCKETm])
    TCPCloseSocket($ConnectedSocket1[$SHOCKETm])
        $ConnectedSocket1[$SHOCKETm] = -1
        $ConnectedSocket2[$SHOCKETm] = -1

For $Trackg = 1 to UBound($vnc_servers)-1
            ;ConsoleWrite("Track: "&$Trackg&"  "&$vnc_servers[$Trackg][1] &" = "& $SHOCKETm &""&@CRLF)
    If $vnc_servers[$Trackg][1] = $SHOCKETm Then

        $vnc_servers[$Trackg][1] = -1
        $gui_display[$Trackg][2] = ""
        $gui_display[$Trackg][3] = ""
;~      Break
    EndIf

Next


;~     $iIndex = _ArraySearch($vnc_servers, $SHOCKETm, 0, 0, 0, 1, 1)
;~     If @error Then
;~         Add_msg("Cand find server to free up for disconected socket" & $SHOCKETm)
;~     Else
;~         $vnc_servers[$iIndex][1] = -1
;~         Add_msg("Server sucesfuly found VNC server to free up for disconected socket" & $SHOCKETm)
;~      Add_msg("Array NO: " & $iIndex)
;~     EndIf

EndFunc

Func Scan_sockets()
;~     Add_msg("Socket scandig start")
;~     Local $ret1, $ret2, $Trackp
;~  Global $vnc_servers
;~     For $Trackp = 0 To $MaxConnection Step 1
;~     if $ConnectedSocket1[$Trackp] <> -1 Then
;~      $ret1 = TCPSend($ConnectedSocket1[$Trackp], "")
;~      $ret1 = TCPRecv($ConnectedSocket1[$Trackp], 0)
;~         Select
;~          Case @error
;~              openSocket($Trackp)
;~          Case $ret1 < 0
;~              openSocket($Trackp)
;~      EndSelect

;~      $ret2 = TCPSend($ConnectedSocket2[$Trackp], "")
;~      $ret2 = TCPRecv($ConnectedSocket2[$Trackp], "")
;~         Select
;~          Case @error
;~              openSocket($Trackp)
;~          Case $ret2 < 0
;~              openSocket($Trackp)
;~      EndSelect
;~     EndIf
;~ $len1 = StringLen($ret1)
;~ $len2 = StringLen($ret2)

;~ $len1 = BinaryLen($ret1)
;~ $len2 = BinaryLen($ret2)
;~ $srv_arr_id = Get_srv_id($Trackp)
;~ $vnc_servers[$srv_arr_id][2] = $len1
;~ $vnc_servers[$srv_arr_id][3] = $len2

;~ Add_msg("LEN1: "& $len1 & "    LEN2: "&$len2)
;~  Next
;~     Add_msg("Socket scandig end")
EndFunc

Func Get_srv_id($SHOCKETmm)
;~  Local $SHOCKETmm, $Trackgg
    For $Trackgg = 1 to UBound($vnc_servers)-1
    If $vnc_servers[$Trackgg][1] = $SHOCKETmm Then
        Return $Trackgg
;~      Break
    EndIf

Next
EndFunc


; Function to return IP Address from a connected socket.
;----------------------------------------------------------------------
Func SocketToIP($SHOCKET)
    Local $sockaddr = DllStructCreate("short;ushort;uint;char[8]")

    Local $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  ;==>SocketToIP

;Function to add text to gui text area
;----------------------------------------------------------------------
Func Add_msg($msg)
    Global $edit
    GUICtrlSetData($edit, _NowTime() & " >  " & $msg & @CRLF & GUICtrlRead($edit))
EndFunc;==>Add_msg

Func write_log($log)
_FileWriteLog(@ScriptDir & "\" & @YEAR & @MON & @MDAY & ".log",$log)

EndFunc

Func SocketSearch()
    For $Track = 0 To $MaxConnection Step 1
        If $ConnectedSocket1[$Track] = -1 Then Return $Track
    Next
EndFunc  ;==>SocketSearch

Func Gui_update()
    Scan_sockets()
        _GUICtrlListView_DeleteAllItems(GUICtrlGetHandle($hListView)); items added with UDF function can be deleted using UDF function
    _GUICtrlListView_AddArray($hListView, $gui_display)
EndFunc

;mattches ip A_ip is with wild card or simple
;B_ip is given ip
;if ip matches returns 1 if not returns -1
Func Match_ip($a_ip, $b_ip)
$a_arr = StringSplit($a_ip,".")
$b_arr = StringSplit($b_ip,".")
$Ret = 0

        if $a_arr[1] <> "*" Then
        if $a_arr[1] <> $b_arr[1] Then
            $Ret = -1
        EndIf
    EndIf

        if $a_arr[2] <> "*" Then
        if $a_arr[2] <> $b_arr[2] Then
            $Ret = -1
        EndIf
    EndIf


        if $a_arr[3] <> "*" Then
        if $a_arr[3] <> $b_arr[3] Then
            $Ret = -1
        EndIf
    EndIf

        if $a_arr[4] <> "*" Then
        if $a_arr[4] <> $b_arr[4] Then
            $Ret = -1
        EndIf
    EndIf

If $Ret = -1 Then
    SetError(1)
EndIf

Return $Ret
EndFunc


Func match_ip_array($ip_array, $ip)
For $x=1 to UBound($ip_array)-1
$check = Match_ip($ip_array[$x], $ip)
if Not @error Then
    Return 1
EndIf
Next
SetError(1)
$err = -1
Return $err

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