Sign in to follow this  
Followers 0
PcExpert

Make a program remotely managable

14 posts in this topic

#1 ·  Posted (edited)

Hi all,

I've made a program to block certain programs and a remote management console It all works, I can send commands to the main program, but one function to unblock, won't work. When the command is executed to the remote machine it should unblock it, by just sleeping. Normally this works fine, but it doesnt work when remotely called. Why? Because When I change de sleep() function to a messagebox it works, it does display the msgbox, so the connction is there. Any Ideas?

Thanks!

Edited by PcExpert

Share this post


Link to post
Share on other sites



Hi,

Can the included script call functions from the main script?

Yes, have you tried it? :)

 

Spoiler

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

Spoiler

AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
 
AutoIt_Icon_small.pngExamples: 
ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

 

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Yes now I did. But all functions are working, but the unblock funtion doesnt. Why? The unblock function is like this:

Function unblockr()

Sleep(7200000)

EndFunc()

When running standalone, without he remote consol, it works fine, but when called with the remote console it doesnt.

EDIT:

I just tried the following:

I replaced the original unblock function with a simple msgbox. I've discovered that when a function is called, thats from the main program, and I send another command that calls the unblock or another function from the main program. That it will not call it.

So I think that the program can only handle one function from the main program, and then it doesnt callany command from the main program anymore, but why? When I just put that function inside the included script, it works and I can send the command as much as I want.

Edited by PcExpert

Share this post


Link to post
Share on other sites

anybody?

Share this post


Link to post
Share on other sites

Unblock by sleeping...?

Perhaps others, like me, have no idea what you are talking about. Post a demo we can run to see your issue.

:)


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

The sleeping is not the problem, that should work. The problem is that if I send a command to the reveiver, it calls a function from the main program. But the problem is, that after i've sended a second command, wich function is in the main program, it just doesnt call it. The first command, to block it wich function is also in the main program, works fine, but after the first command, it fails to do a econd one.

TCP Receiver:

Dim $szIPADDRESS = @IPAddress1, $nPORT = 44890, $ConnectedSocket = -1, $MainSocket, $msg, $recv, $szIP_Accepted

; Start The TCP Services

;==============================================

TCPStartUp()

; Create a GUI for messages

;Wait for connection and convert socket into IP

Listen()

; GUI Message Loop

;==============================================

; Try to receive (up to) 2048 bytes

;----------------------------------------------------------------

While 1

$recv = TCPRecv( $ConnectedSocket, 2048 )

; If the receive failed with @error then the socket has disconnected

;----------------------------------------------------------------

If @error Then ExitLoop

; Update the edit control with what we have received

;----------------------------------------------------------------

Select

Case $recv = "ENDING_BYE"

TCPCloseSocket($ConnectedSocket)

TCPCloseSocket($MainSocket)

Listen()

Case $recv = "block"

blockr()

Case $recv = "shutdown"

shutdown(6)

Case $recv = "unblock"

MsgBox(64, "", "Inet Block deactivated")

Case $recv = "!block"

MsgBox(64, "", "Block active")

Case $recv = "!unblock"

MsgBox(64, "", "Block deactivated")

#comments-start

Case $recv <> ""

TraySetState(1)

TraySetState(4)

TrayTip("Bericht van " & $szIP_Accepted, $recv, 15)

sleep(15000)

TrayTip("", "", 15)

TraySetState(8)

TraySetState(2)

#comments-end

EndSelect

WEnd

If $ConnectedSocket <> -1 Then TCPCloseSocket($ConnectedSocket)

TCPShutDown()

; 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_ptr",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 Listen()

; 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

;Wait for and Accept a connection

;==============================================

Do

$ConnectedSocket = TCPAccept($MainSocket)

Until $ConnectedSocket <> -1

; Get IP of client connecting

Dim $szIP_Accepted = SocketToIP($ConnectedSocket)

EndFunc

Program:

#include "tcp.au3"

While 1

Sleep(10)

WEnd

Func blockr()

While 1

Sleep(10)

if WinExists("Eigenschappen voor Datum en tijd") Then

WinClose("Eigenschappen voor Datum en tijd")

sleep(10)

EndIf

if ProcessExists("msmsgs.exe") Then

ProcessClose("msmsgs.exe")

Sleep(10)

EndIf

if ProcessExists("wlmail.exe") Then

ProcessClose("wlmail.exe")

Sleep(10)

EndIf

if ProcessExists("firefox.exe") Then

ProcessClose("firefox.exe")

Sleep(10)

EndIf

if ProcessExists("iexplore.exe") Then

ProcessClose("iexplore.exe")

Sleep(10)

EndIf

if ProcessExists("msnmsgr.exe") Then

ProcessClose("msnmsgr.exe")

Sleep(10)

EndIf

WEnd

EndFunc

Func unblockr()

While 1

Sleep(10)

WEnd

EndFunc

Edited by PcExpert

Share this post


Link to post
Share on other sites

The sleeping is not the problem, that should work. The problem is that if I send a command to the reveiver, it calls a function from the main program. But the problem is, that after i've sended a second command, wich function is in the main program, it just doesnt call it. The first command, to block it wich function is also in the main program, works fine, but after the first command, it fails to do a econd one.

Was the first function you called that Two Hour Sleep? Did you expect it to be responsive while that sleep was active?

:P

(You still haven't posted a demo/reproducer. :) )


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

No ofcourse it didnt sleep in that function. See my edited post for the codes

Share this post


Link to post
Share on other sites

Ah, code! Amazing what a difference that makes! Try this:

Server (start this one first):

;SERVER!  Start me first, before the CLIENT!!!!!!!!!!!!!!!
Global $szIPADDRESS = @IPAddress1, $nPORT = 44890, $ConnectedSocket = -1, $MainSocket, $msg, $recv, $szIP_Accepted

; Start The TCP Services
TCPStartup()

;Wait for connection and convert socket into IP
Listen()

; Try to receive (up to) 2048 bytes
While 1
    $recv = TCPRecv($ConnectedSocket, 2048)
    ; If the receive failed with @error then the socket has disconnected
    If @error Then ExitLoop
    Select
        Case $recv = "ENDING_BYE"
            TCPCloseSocket($ConnectedSocket)
            TCPCloseSocket($MainSocket)
            MsgBox(64, "ENDING_BYE", "$recv = ENDING_BYE", 3)
            ExitLoop
        Case $recv = "block"
            MsgBox(64, "block", "$recv = block", 3)
        Case $recv = "shutdown"
            MsgBox(64, "shutdown", "$recv = shutdown", 3)
        Case $recv = "unblock"
            MsgBox(64, "unblock", "$recv = unblock", 3)
        Case $recv = "!block"
            MsgBox(64, "!block", "$recv = !block", 3)
        Case $recv = "!unblock"
            MsgBox(64, "!unblock", "$recv = !unblock", 3)
        Case $recv <> ""
            MsgBox(64, "Unrecognized", "$recv = " & $recv & @LF, 3)
    EndSelect
WEnd
TCPCloseSocket($ConnectedSocket)
TCPShutdown()

; Create socket and wait for connection
Func Listen()
    ; Create a Listening "SOCKET".
    $MainSocket = TCPListen($szIPADDRESS, $nPORT)
    
    ; If the Socket creation fails, exit.
    If $MainSocket = -1 Then Exit
    
    ;Wait for and Accept a connection
    Do
        $ConnectedSocket = TCPAccept($MainSocket)
    Until $ConnectedSocket <> -1
    
    ; Get IP of client connecting
    Dim $szIP_Accepted = SocketToIP($ConnectedSocket)
EndFunc   ;==>Listen

; 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   ;==>SocketToIPoÝ÷ ØÚ-zØZ·*.­*ëS ߺw-î±çZuêkyÆ­yÚ®¢Ø§¶kªê-¾'ªê-{jºp'!q«¬yÖ§zºè®)÷Û]4
XÚî§íz»®÷«È(xºÚ"µÍÐÓQS   ÌÌÎÈÝYHÙXÛÛYÝ[ÈHÑTTÌÌÎÉÌÌÎÉÌÌÎÉÌÌÎÉÌÌÎÉÌÌÎÉÌÌÎÉÌÌÎÉÌÌÎÉÌÌÎÉÌÌÎÉÌÌÎÉÌÌÎÉÌÌÎÉÌÌÎÂÈÝHÔÙXÙÂÔÝ

BÛØ[  ÌÍÜÞÙÈHÛÛ][YK   ÌÍÜÞTQTÔÈHTYÜÌK ÌÍÛÔH

L   ÌÍÐÛÛXÝYÛØÚÙ]HLK  ÌÍÜÞ]BÐ][ÈÛÛXÝÈÑTT]]ÈT[ÔÌÎLBÌÍÐÛÛXÝYÛØÚÙ]HÔÛÛXÝ
    ÌÍÜÞTQTÔË ÌÍÛÔ
BYÜ[SÙÐÞ
LL  ][ÝÑÜ][ÝË  ][ÝÕÔÛÛXÝZ[YÚ]ÔÐHÜ    ][ÝÈ  [ÈÜBNÈYHÈÈÜÛÜ[[]ÞÜ]BNÈÈÙ[ÈHÑTT[ÙBNÓÛÜÜ]ÚÚ[ÈÜ]HÈÙ[ÈHÑTTUÚ[HBBNÈ[]ÞÜ]HÈ[ÛZ]BIÌÍÜÞ]HH[]Þ
    ][ÝÑ[]HÈÙ[ÈÙ][ÝË    [È [È ][ÝÑSS×ÐQKØÚË[ØÚË ÌÌÎØØÚË  ÌÌÎÝ[ØÚÎ][ÝÊBBRYÜÜ   ÌÍÜÞ]HH ][ÝÉ][ÝÈ[^]ÛÜBNÈ][ÈÙ[]HÝYÚÛÛXÝYÛØÚÙ]BUÔÙ[
    ÌÍÐÛÛXÝYÛØÚÙ] ÌÍÜÞ]JBBRYÜ[^]ÛÜUÑ[[YÈÛBÔÚ]ÝÛ

This one works for me.

:)


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

This works, BUT this isnt a problem, to call functions from the tcp.au3 script. The problem is to call a function from the main program:

Dont think anymore of anything I said, I start over and i'll try to be as clear as possible:

First of all, the COMPLETE codes:

Main Program

#include "tcp.au3"

; Script Start - Add your code below here

Global $checkvar

;Global $checkvar2

Opt("TrayIconHide", 1)

HotKeySet("!{HOME}", "unlock")

boot()

Func boot()

If IniRead("settings.ini", "Settings", "Date", "") = @MDAY & "-" & @MON & "-" & @YEAR Then

Shutdown(5)

Else

RegWrite("HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System", "DisableTaskMgr", "REG_DWORD", "1")

While 1

Sleep(10)

check()

WEnd

EndIf

EndFunc

Func check()

Sleep(10)

if WinExists("Eigenschappen voor Datum en tijd") Then

WinClose("Eigenschappen voor Datum en tijd")

sleep(10)

EndIf

If @HOUR>19 Then

block()

EndIf

EndFunc

Func block()

Sleep(10)

if WinExists("Eigenschappen voor Datum en tijd") Then

WinClose("Eigenschappen voor Datum en tijd")

sleep(10)

EndIf

if ProcessExists("msmsgs.exe") Then

ProcessClose("msmsgs.exe")

Sleep(10)

EndIf

if ProcessExists("wlmail.exe") Then

ProcessClose("wlmail.exe")

Sleep(10)

EndIf

if ProcessExists("firefox.exe") Then

ProcessClose("firefox.exe")

Sleep(10)

EndIf

if ProcessExists("iexplore.exe") Then

ProcessClose("iexplore.exe")

Sleep(10)

EndIf

if ProcessExists("msnmsgr.exe") Then

ProcessClose("msnmsgr.exe")

Sleep(10)

EndIf

If @MIN>19 Then

txt1()

EndIf

EndFunc

Func txt1()

If $checkvar = 0 Then

MsgBox(64, "Internet Block", "De computer gaat automatisch uit om 20:30. Er komt GEEN melding meer als de computer ook daadwerkelijk wordt afgesloten dus houd de klok goed in de gaten :)", 10)

$checkvar = 1

EndIf

IniWrite("settings.ini", "Settings", "Date", @MDAY & "-" & @MON & "-" & @YEAR)

checkf()

EndFunc

Func checkf()

Sleep(10)

if WinExists("Eigenschappen voor Datum en tijd") Then

WinClose("Eigenschappen voor Datum en tijd")

sleep(10)

EndIf

sleep(10)

if ProcessExists("msmsgs.exe") Then

ProcessClose("msmsgs.exe")

Sleep(10)

EndIf

if ProcessExists("wlmail.exe") Then

ProcessClose("wlmail.exe")

Sleep(10)

EndIf

if ProcessExists("firefox.exe") Then

ProcessClose("firefox.exe")

Sleep(10)

EndIf

if ProcessExists("iexplore.exe") Then

ProcessClose("iexplore.exe")

Sleep(10)

EndIf

if ProcessExists("msnmsgr.exe") Then

ProcessClose("msnmsgr.exe")

Sleep(10)

EndIf

If @MIN>29 Then

Shutdown(5)

EndIf

EndFunc

Func Unlock()

$unlock = InputBox("Internet Block", "Please enter your password:", "", "*")

If $unlock = "Makey01yz12" Then

unlock1()

EndIf

EndFunc

Func unlock1()

$unlock1 = InputBox("Internet Block", "Please specify the amount of seconds to allow internet. Use * for unlimited")

If $unlock1 = "*" Then

Exit

Else

$time = $unlock1 * 60

Sleep($time)

check()

EndIf

EndFunc

;==========================REMOTE MANAGEMENT FUNCTIONS

Func blockr()

While 1

Sleep(10)

if WinExists("Eigenschappen voor Datum en tijd") Then

WinClose("Eigenschappen voor Datum en tijd")

sleep(10)

EndIf

if ProcessExists("msmsgs.exe") Then

ProcessClose("msmsgs.exe")

Sleep(10)

EndIf

if ProcessExists("wlmail.exe") Then

ProcessClose("wlmail.exe")

Sleep(10)

EndIf

if ProcessExists("firefox.exe") Then

ProcessClose("firefox.exe")

Sleep(10)

EndIf

if ProcessExists("iexplore.exe") Then

ProcessClose("iexplore.exe")

Sleep(10)

EndIf

if ProcessExists("msnmsgr.exe") Then

ProcessClose("msnmsgr.exe")

Sleep(10)

EndIf

WEnd

EndFunc

Func unblockr()

While 1

Sleep(10)

WEnd

EndFunc

;========================REMOTE MANAGEMENT FUNCTIONS

TCP.au3

Dim $szIPADDRESS = @IPAddress1, $nPORT = 44890, $ConnectedSocket = -1, $MainSocket, $msg, $recv, $szIP_Accepted

; Start The TCP Services

;==============================================

TCPStartUp()

; Create a GUI for messages

;Wait for connection and convert socket into IP

Listen()

; GUI Message Loop

;==============================================

; Try to receive (up to) 2048 bytes

;----------------------------------------------------------------

While 1

$recv = TCPRecv( $ConnectedSocket, 2048 )

; If the receive failed with @error then the socket has disconnected

;----------------------------------------------------------------

If @error Then ExitLoop

; Update the edit control with what we have received

;----------------------------------------------------------------

Select

Case $recv = "ENDING_BYE"

TCPCloseSocket($ConnectedSocket)

TCPCloseSocket($MainSocket)

Listen()

Case $recv = "block"

blockr()

Case $recv = "shutdown"

shutdown(6)

Case $recv = "unblock"

blockr()

Case $recv = "!block"

BlockInput(1)

Case $recv = "!unblock"

BlockInput(0)

EndSelect

WEnd

If $ConnectedSocket <> -1 Then TCPCloseSocket($ConnectedSocket)

TCPShutDown()

; 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_ptr",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 Listen()

; 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

;Wait for and Accept a connection

;==============================================

Do

$ConnectedSocket = TCPAccept($MainSocket)

Until $ConnectedSocket <> -1

; Get IP of client connecting

Dim $szIP_Accepted = SocketToIP($ConnectedSocket)

EndFunc

Second: The problem

The problem is that the program can only execute a function thats in the main program once. Explaination: If I do the command block then, i cannot do the command unblock because thats also a function from the main program, not from tcp.au3. So when I want to block a PC I send block, that works. But if I want to unblock and I send unblock, it doesnt work anymore. Any Ideas?

Edited by PcExpert

Share this post


Link to post
Share on other sites

Both blockr() and unblockr() are infinite loops. Once either one is called there will be no returning to the While/WEnd loop that contains the TCPRecv().

You need to control that Blocked/UnBlocked state with a global flag. Those functions should only be toggling the flag. Then the same loop with TCPRecv() can also conditionally perform the "blocking" function, or just toggle an AdLibEnable()/AdLibDisable() that does the blocking.

:)


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

Can you help me with that? Sorry, but I dont know where to start.

Share this post


Link to post
Share on other sites

*bump*

Share this post


Link to post
Share on other sites

Use this modified server script with the same client from my post #9:

;SERVER!  Start me first, before the CLIENT!!!!!!!!!!!!!!!
Global $szIPADDRESS = @IPAddress1, $nPORT = 44890, $ConnectedSocket = -1, $MainSocket, $msg, $recv, $szIP_Accepted
Global $fBlock = False, $fBangBlock = False

; Start The TCP Services
TCPStartup()

;Wait for connection and convert socket into IP
Listen()

; Try to receive (up to) 2048 bytes
While 1
    ; Check for inputs from client
    $recv = TCPRecv($ConnectedSocket, 2048)
    ; If the receive failed with @error then the socket has disconnected
    If @error Then ExitLoop
    Select
        Case $recv = "ENDING_BYE"
            TCPCloseSocket($ConnectedSocket)
            TCPCloseSocket($MainSocket)
            MsgBox(64, "ENDING_BYE", "$recv = ENDING_BYE", 3)
            ExitLoop
        Case $recv = "block"
            $fBlock = True
            MsgBox(64, "block", "$recv = block", 3)
        Case $recv = "shutdown"
            MsgBox(64, "shutdown", "$recv = shutdown", 3)
            Exit
        Case $recv = "unblock"
            $fBlock = False
            MsgBox(64, "unblock", "$recv = unblock", 3)
        Case $recv = "!block"
            $fBangBlock = True
            MsgBox(64, "!block", "$recv = !block", 3)
        Case $recv = "!unblock"
            $fBangBlock = False
            MsgBox(64, "!unblock", "$recv = !unblock", 3)
    EndSelect
    
    ; Act on the control flags
    If $fBlock Then ConsoleWrite("Do whatever 'block' does..." & @LF)
    If $fBangBlock Then ConsoleWrite("Do whatever '!block' does..." & @LF)

    Sleep(100)
WEnd
TCPCloseSocket($ConnectedSocket)
TCPShutdown()

; Create socket and wait for connection
Func Listen()
    ; Create a Listening "SOCKET".
    $MainSocket = TCPListen($szIPADDRESS, $nPORT)

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

    ;Wait for and Accept a connection
    Do
        $ConnectedSocket = TCPAccept($MainSocket)
    Until $ConnectedSocket <> -1

    ; Get IP of client connecting
    $szIP_Accepted = SocketToIP($ConnectedSocket)
EndFunc   ;==>Listen

; 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

You can see that the same single loop checks for new commands from the network connection, modifies the Global flags based on those commands, and then acts on the condition of those flags.

:)


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

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