Sign in to follow this  
Followers 0
Muscle

TCP Problem

8 posts in this topic

#1 ·  Posted (edited)

Hi at all!

I am new here at the forum but I started to code with autoit several months ago.

I have a big problem with a network tool, I try to code.

The Problem is that only 1 client can connect to the admincenter(server). But

I want to administrate several clients and not only one.

Overview:

Admin(Server): Can send messages, lock clients

Client: gets locked, ....

Who can help me please with my problem?

A lot of Thanks!

Muscle

server.au3

; TCP-Options
; =================
Global $ip = @IPAddress1
Global $MainSocket, $ConnectedSocket = -1
Global $port = IniRead(@DesktopDir&"\database.ini", "Data", "port", "--->Mistake<---")

; TCP-Functions
; ==============
TCPStartup()
$MainSocket = TCPListen($ip, $port, 100)
If $MainSocket = -1 Then
    MsgBox(16, "TCP-Problem", "Program closes!")
    Exit
EndIf
Global $ListenSocket = -1

Func Netzwerk()
    ; => TCP-Befehle senden
    Select
        Case @GUI_CtrlId = $lock_client
            If $ConnectedSocket > -1 Then
                $send = TCPSend($ConnectedSocket, "-LOCK-")             ; Client LOCK
                If @error Then
                    TCPCloseSocket($ConnectedSocket)
                    $ConnectedSocket = -1
                EndIf
            EndIf
        Case @GUI_CtrlId = $unlock_client
            If $ConnectedSocket > -1 Then
                $send = TCPSend($ConnectedSocket, "-UNLOCK-")           ; Client UNLOCK
                If @error Then
                    TCPCloseSocket($ConnectedSocket)
                    $ConnectedSocket = -1
                EndIf
            EndIf
        Case @GUI_CtrlId = $restart_client
            If $ConnectedSocket > -1 Then
                $send = TCPSend($ConnectedSocket, "-RESTART-")          ; Client RESTART
                If @error Then
                    TCPCloseSocket($ConnectedSocket)
                    $ConnectedSocket = -1
                EndIf
            EndIf
        Case @GUI_CtrlId = $shutdown_client
            If $ConnectedSocket > -1 Then
                $send = TCPSend($ConnectedSocket, "-SHUTDOWN-")         ; Client OFF
                If @error Then
                    TCPCloseSocket($ConnectedSocket)
                    $ConnectedSocket = -1
                EndIf
            EndIf
        Case @GUI_CtrlId = $message_client
            $message = InputBox("Send Message ...", "Message:", "", "", 300, 125)
            If $ConnectedSocket > -1 Then
                $send = TCPSend($ConnectedSocket, "-MESSAGE-"&$message) ; Client Message
                If @error Then
                    TCPCloseSocket($ConnectedSocket)
                    $ConnectedSocket = -1
                EndIf
            EndIf
        Case @GUI_CtrlId = $save_autostart
            load_pw()
            $save_autostart_pw = InputBox("Security", "Type in AdminPW!", "", "*M", 250, 140)
            If $save_autostart_pw == $passwort Then
                If $ConnectedSocket > -1 Then
                    If GUICtrlRead($autostart) = $GUI_CHECKED Then
                        $send = TCPSend($ConnectedSocket, "-AUTO~ON-")  ; Client-Autostart ON
                        If @error Then
                            TCPCloseSocket($ConnectedSocket)
                            $ConnectedSocket = -1
                        EndIf
                    ElseIf GUICtrlRead($autostart) = $GUI_UNCHECKED Then
                        $send = TCPSend($ConnectedSocket, "-AUTO~OFF-") ; Client-Autostart OFF
                        If @error Then
                            TCPCloseSocket($ConnectedSocket)
                            $ConnectedSocket = -1
                        EndIf
                    EndIf
                EndIf
            Else
                MsgBox(16, "Mistake", "Password wrong!")
            EndIf
        Case @GUI_CtrlId = $deinstall_client
            Dim $delete_question
            $delete_question = MsgBox(308,"Client deinstall", "Really deinstall "&GUICtrlRead($client)&" ?")
            Select
                Case $delete_question = 6   ; Yes
                    load_pw()
                    $delete_pw = InputBox("Security", "Type in AdminPW!", "", "*M", 250, 140)
                    If $delete_pw == $passwort Then
                        If $ConnectedSocket > -1 Then
                            $send = TCPSend($ConnectedSocket, "-DELETE-") ; Client deinstall
                            If @error Then
                                TCPCloseSocket($ConnectedSocket)
                                $ConnectedSocket = -1
                            EndIf
                        EndIf
                    Else
                        MsgBox(16, "Mistake", "Password wrong!")
                    EndIf
                Case $delete_question = 7   ; No
                    ; do nothing!
            EndSelect
    EndSelect
EndFunc

While 1
    If $ListenSocket > 0 Then
        $receive = TCPRecv($ListenSocket, 512)
        If Not @error Then
            TCPCloseSocket($ListenSocket)
            $ListenSocket = -1
        EndIf
    EndIf
    
    ; => TCP-Connection search
    If $ConnectedSocket = -1 Then
        $ConnectedSocket = TCPAccept($MainSocket)
    Else
        $receive = TCPRecv($ConnectedSocket, 512)
        If $receive <> "" And $receive <> "-CLOSE-" Then
            ; Data for ListView
            If StringLeft($receive, 6) = "-DATA-" Then
                Dim $client_data = StringTrimLeft($receive, 6)
                GUICtrlCreateListViewItem($client_data, $list)
            EndIf
        ElseIf @error Or $receive = "-CLOSE-" Then
            ; delete old!
            TCPCloseSocket($ConnectedSocket)
            $ConnectedSocket = -1
        EndIf
    EndIf
WEnd

Func OnAutoItExit()
    ; close TCP-Socket, TCP-Service
    Local $ListenSocket
    Local $MainSocket
    If $ListenSocket > -1 Then
        TCPSend($ListenSocket, "-CLOSE-")
        Sleep(2000)
        TCPRecv($ListenSocket, 512)
        TCPCloseSocket($ListenSocket)
    EndIf
    TCPCloseSocket($MainSocket)
    TCPShutdown()
EndFunc ; ==>
oÝ÷ ÙÉbz{Z»v®¶­s`¤÷D¶W6WBgV÷C·´cgÒgV÷C²ÂgV÷C¶6Æ÷6RgV÷C² ¤gVæ26Æ÷6R W@¤VæDgVæ0   £²D5ÔVç7FVÆÇVævVࣲÓÓÓÓÓÓÓÓÓÓÓÓÓÓÓÓФvÆö&Âb33c¶öFÖâÒgV÷C³"ãcããSRgV÷C°¤vÆö&Âb33c·÷'BÒ33  £²D5ÔgVæ·FöæVࣲÓÓÓÓÓÓÓÓÓÓÓÓÓÐ¥D57F'GW¢b33cµ6ö6¶WBÒD56öææV7Bb33c¶öFÖâÂb33c·÷'B¤bb33cµ6ö6¶WBÒÓFVà ×6t&÷bÂgV÷C´Ö7F¶RgV÷C²ÂgV÷C´æò6öææV7FöâFò6W'fW"b333²gV÷C² W@¤VæD` £²ÒfwC²6ÆVçBFFVâ6VæFVà¤FÒb33c¶6ÆVçEöFFÒgV÷C·FW7GÆvW7W''GÂgV÷C²f×´FG&W73fײgV÷C·ÂgV÷C²f×´6ö×WFW$æÖRfײgV÷C·Æ¦gV÷C°¢b33c·6VæBÒD56VæBb33cµ6ö6¶WBÂgV÷C²ÔDDÒgV÷C²fײb33c¶6ÆVçEöFF£²ÓÒfwC°  ¥vÆR b33c·&V6VfRÒD5&V7bb33cµ6ö6¶WBÂS" 7vF6b33c·&V6VfP 66RgV÷C²ÔÄô4²ÒgV÷C° ×6t&÷ÂgV÷C·v÷&·2gV÷C²ÂgV÷C´Äô4²gV÷C² 66RgV÷C²ÕTäÄô4²ÒgV÷C° ×6t&÷ÂgV÷C·v÷&·2gV÷C²ÂgV÷CµTäÄô4²gV÷C² 66RgV÷C²Õ$U5D%BÒgV÷C° ×6t&÷ÂgV÷C·v÷&·2gV÷C²ÂgV÷Cµ$U5D%BgV÷C² 66RgV÷C²Õ4UDDõtâÒgV÷C° ×6t&÷ÂgV÷C·v÷&·2gV÷C²ÂgV÷Cµ4UDDõtâgV÷C² 66RgV÷C²ÔDTÄUDRÒgV÷C° ×6t&÷ÂgV÷C·v÷&·2gV÷C²ÂgV÷C´DTÄUDRgV÷C² VæE7vF6  b7G&ætÆVgBb33c·&V6VfRÂÒgV÷C²ÔÔU54tRÒgV÷C²FVà²6ÆVçBÖW76vP b33c¶ÖW76vRÒ7G&æuG&ÔÆVgBb33c·&V6VfR ×6t&÷ÂgV÷C´ÖW76vRg&öÒFÖâgV÷C²Âb33c¶ÖW76vR VæD` b7G&ætÆVgBb33c·&V6VfRÂÒgV÷C²ÔUD÷äôâÒgV÷C²FVà ×6t&÷ÂgV÷C·v÷&·2gV÷C²ÂgV÷C´UDòôâgV÷C² VæD` b7G&ætÆVgBb33c·&V6VfRÂÒgV÷C²ÔUD÷äôdbÒgV÷C²FVà ×6t&÷ÂgV÷C·v÷&·2gV÷C²ÂgV÷C´UDòôdbgV÷C² VæD` bW'&÷"fÇC²fwC²FVâWDÆö÷¥tVæ@  ¤gVæ2öäWFôDWB ²6Æ÷6RD5Õ6ö6¶WBÂD5Õ6W'f6P D56VæBb33cµ6ö6¶WBÂgV÷C²Ô4Äõ4RÒgV÷C² D56Æ÷6U6ö6¶WBb33cµ6ö6¶WB D56WFF÷vâ¤VæDgVæ2²ÓÒfwC°
Edited by Muscle

Share this post


Link to post
Share on other sites



Well, everytime a client runs TCPConnect to your IP/Port, your TCPAccept will go from -1 To like 256. So you need to loop that to find out who's trying to connect. Once they connect, you need to have a way of storing there acceptid, so you put it in a big array of all your clients. Now you need to SEPERATELY check every client to see if they sent something, how are you going to know who sent you something if you check the big array? So Loop every client to see if they sent something.

Your script only checks the first person. I'm not sure what happens when someone else logs on.

Heres an heavily commented server script which holds more then one person.

TCPStartup (); Starts TCP Services
Global $client[2]; Array that holds TCPAccept ids which you use to send\recv
Global $users = 1; Amount of users in array above
Global $sock = TCPListen ( @IPAddress1, 33890 ); Starts the listening sequence
If $sock = -1 Then Exit MsgBox ( 1, "Error", "Failed to start TCP, check to make sure this isn't your second!" ); To make sure its working
Dim $accept = -1, $recv; Declaring some variables

While 1; Start Loop
    $accept = TCPAccept ( $sock ); Check to see if anyones connecting
    If $accept <> -1 Then; If someone as connected
        $client[$users] = $accept; Add them to the list
        $users += 1; Bump up the amount of users connected
        ReDim $client[$users]; Redeclare the array so it won't go over0
    EndIf;Finish for check for clients connecting
    For $i = 1 To UBound ( $client ) - 1 Step 1;Loops to see if anyones sending
        $recv = TCPRecv ( $client[$i], 512 );Checks users to see if they sent anything
        If $recv <> "" Then;If they have sent something
            _Parse ($client[$i], $recv); Parse it here, sending there id and what they sent
        EndIf;Ends the check
    Next;Ends the check
WEnd;End of loop

Func _Parse ($client, $recv)
MsgBox ( 1, $client, $recv ); Displays what user sent it, and what they sent.
EndFunc

Share this post


Link to post
Share on other sites

Thanks a lot for your help!

But my problem is that I don´t understand the script very well.

Can you show me the changes I had to make in my scripts, please?

Do I only have to edit the server.au3 or the client.au3, too?

I am a noob in tcp connections.

Thanks for your help.

Muscle

Share this post


Link to post
Share on other sites

Check my signature for tons of information

Share this post


Link to post
Share on other sites

Thanks a lot for your link. I will try to understand it in the evening.

HAPPY NEW YEAR !!!

Thanks

Muscle

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

Thanks a lot AutoIt Smith! Now it works with multiple clients.

But I still have the Problem that when I send a task/message to

one selected client, every client becomes it.

I want that just the selected client gets the task/message.

What can I change?

I also want the client run when the server is offline and when

he gets online that the client then sends his data to the admin/server.

A lot of thanks to all!

Muscle

server.au3

; TCP-Prefences
; =================
Global $ip = @IPAddress1
Global $port = IniRead(@DesktopDir&"\database.ini", "Daten", "port", "--->Fehler<---")
Global $MaxConc = 100                               ; max clients
Global Const $MaxLength = 512                       ; max data-length

Global $MainSocket = TCPStartServer($port, $MaxConc)
If @error <> 0 Then
    MsgBox(16, "TCP-Mistake", "Program closes!")
    Exit
EndIf
Global $ConnectedSocket[$MaxConc]
Global $CurrentSocket = 0
Local $track = 0
Global Const $MaxConnection = ($MaxConc - 1)

; TCP-Functions
; ==============
For $track = 0 To $MaxConnection Step 1
    $ConnectedSocket[$track] = -1
Next

While 1
    $ConnectedSocket[$CurrentSocket] = TCPAccept($MainSocket)
    If $ConnectedSocket[$CurrentSocket] <> -1 Then
        $CurrentSocket = SocketSearch()
    EndIf
    $track = 0
    For $track = 0 To $MaxConnection Step 1
        ; => get
        If $ConnectedSocket[$track] <> -1 Then
            $data = TCPRecv($ConnectedSocket[$track], $MaxLength)
            If $data <> "" And $data <> "-CLOSE-" Then
                ; Data - ListView
                If StringLeft($data, 6) = "-DATA-" Then
                    Dim $client_data = StringTrimLeft($data, 6)
                    GUICtrlCreateListViewItem($client_data, $list)
                EndIf
            ElseIf @error Or $data = "-CLOSE-" Then
                TCPCloseSocket($ConnectedSocket[$track])
                $ConnectedSocket[$track] = -1
                $CurrentSocket = SocketSearch()
                ; ?????????
                ; ?????????
                ; delete old client in list view?????
                ; ?????????
                ; ????????? Please HELP
            EndIf
        EndIf
    Next
WEnd

Func SocketSearch()
    Local $track = 0
    For $track = 0 To $MaxConnection Step 1
        If $ConnectedSocket[$track] = -1 Then
            Return $track
        Else
            ; Socket is ok!
        EndIf
    Next
EndFunc

Func TCPStartServer($port, $MaxConnect = 1)
    Local $socket
    $socket = TCPStartup()
    Select
        Case $socket = 0
            SetError(@error)
            Return -1
    EndSelect
    $socket = TCPListen($ip, $port, $MaxConnect)
    Select
        Case $socket = -1
            SetError(@error)
            Return 0
    EndSelect
    SetError(0)
    Return $socket
EndFunc

Func Netzwerk()
    ; => send
    Local $track = 0
    For $track = 0 To $MaxConnection Step 1
        If $ConnectedSocket[$track] <> -1 Then
            Select
                Case @GUI_CtrlId = $lock_client
                    $send = TCPSend($ConnectedSocket[$track], "-LOCK-")             ; Client lock
                Case @GUI_CtrlId = $unlock_client
                    $send = TCPSend($ConnectedSocket[$track], "-UNLOCK-")           ; Client unlock
                Case @GUI_CtrlId = $restart_client
                    $send = TCPSend($ConnectedSocket[$track], "-RESTART-")          ; Client restart
                Case @GUI_CtrlId = $shutdown_client
                    $send = TCPSend($ConnectedSocket[$track], "-SHUTDOWN-")         ; Client shutdown
                Case @GUI_CtrlId = $message_client
                    $message = InputBox("Send a message ...", "Message:", "", "", 300, 125)
                    $send = TCPSend($ConnectedSocket[$track], "-MESSAGE-"&$message) ; Client message
                Case @GUI_CtrlId = $save_autostart
                    load_pw()
                    $save_autostart_pw = InputBox("Security", "Type in admin-pw!", "", "*M", 250, 140)
                    If $save_autostart_pw == $passwort Then
                        If GUICtrlRead($autostart) = $GUI_CHECKED Then
                            $send = TCPSend($ConnectedSocket[$track], "-AUTO~ON-")  ; Client-Autostart ON
                        ElseIf GUICtrlRead($autostart) = $GUI_UNCHECKED Then
                            $send = TCPSend($ConnectedSocket[$track], "-AUTO~OFF-") ; Client-Autostart OFF
                        EndIf
                    Else
                        MsgBox(16, "Mistake", "Password wrong!")
                    EndIf
                Case @GUI_CtrlId = $deinstall_client
                    Dim $delete_question
                    $delete_question = MsgBox(308,"Client deinstall", "Delete client "&GUICtrlRead($client)&" ?")
                    Select
                        Case $delete_question = 6   ; Yes
                            load_pw()
                            $delete_pw = InputBox("Security", "Type in admin-pw!", "", "*M", 250, 140)
                            If $delete_pw == $passwort Then
                                $send = TCPSend($ConnectedSocket[$track], "-DELETE-") ; Client deinstall
                            Else
                                MsgBox(16, "Mistake", "Password wrong!")
                            EndIf
                        Case $delete_question = 7   ; No
                            ; do nothing!
                    EndSelect
            EndSelect
        EndIf
    Next
EndFunc

Func OnAutoItExit()
    ; close TCP-Socket, TCP-Service
    Local $MainSocket
    TCPCloseSocket($MainSocket)
    TCPShutdown()
EndFunc ; ==>

forum_server.au3

forum_client.au3

Edited by Muscle

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

All Problems are solved! :P

Big thanks to AutoItSmith and to the professional Coder Helge!!!!

Big Thanks!

Muscle :D

Edited by Muscle

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