Jump to content

timing issue when running script from another script


Recommended Posts

I'm running into an issue, possibly timing related, I cannot for the life of me figure out how to solve it.

There's script A, that's starting several instances of script B. Some of the instances of script B however won't work fully. They receive data just fine, but won't send back a reply. If however I make script A pause a while between launching the various instances, it works just fine.

Here they are:

Server:

#include <TCP.au3>

OnAutoItExitRegister("_Exit")

HotKeySet("1", "_test")

Global $i = 0
Global $begin = 0
Global $count = 0
Global $num_ca = 15

$hServer = _TCP_Server_Create(88); A server. Tadaa!
_TCP_RegisterEvent($hServer, $TCP_RECEIVE, "Received")
_TCP_RegisterEvent($hServer, $TCP_NEWCLIENT, "NewClient")
_TCP_RegisterEvent($hServer, $TCP_DISCONNECT, "Disconnect")

For $i = $num_ca To 1 Step -1
    Run("client.exe" & " " & $i)
    sleep(100); <---------- comment this line out for testing <----------
Next

While 1
    Sleep(100)
WEnd

Func _test($search_execute = 0)
    Sleep(500)
    For $i = 1 To 15
        _send($i, "ping")
        Sleep(1000)
    Next
EndFunc   ;==>_test

Func NewClient($hSocket, $iError); Yo, check this out! It's a $iError parameter! (In case you didn't noticed: It's in every function)
    ;
EndFunc   ;==>NewClient

Func Disconnect($hSocket, $iError); Damn, we lost a client. Time of death: @Hour & @Min & @Sec :P
    ;msgbox(0, "client lost", $iError)
EndFunc   ;==>Disconnect

Func Received($hSocket, $sReceived, $iError); And we also registered this! Our homemade do-it-yourself function gets called when something is received.
    MsgBox(0, "received", $sReceived, 0.5)
EndFunc   ;==>Received

Func _send($receiver, $message)
    $sa = _TCP_Server_ClientList()
    If IsArray($sa) Then
        _TCP_Send($sa[$receiver], $message)
    EndIf
EndFunc   ;==>_send

Func _Exit()
    Opt("WinWaitDelay", 10)
    For $i = $num_ca To 0 Step -1
        WinClose("client_" & $i)
    Next
    For $i = $num_ca To 0 Step -1
        WinWaitClose("client_" & $i)
    Next
EndFunc   ;==>_Exit

Client:

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Compression=4
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
#include <TCP.au3>
#include <guiconstantsex.au3>

GUICreate("client_" & $CmdLine[1])

Global $fsocket

$hClient = _TCP_Client_Create(@IPAddress1, 88); Create the client. Which will connect to the local ip address on port 88

_TCP_RegisterEvent($hClient, $TCP_RECEIVE, "Received"); Function "Received" will get called when something is received
_TCP_RegisterEvent($hClient, $TCP_CONNECT, "Connected"); And func "Connected" will get called when the client is connected.
_TCP_RegisterEvent($hClient, $TCP_DISCONNECT, "Disconnected"); And "Disconnected" will get called when the server disconnects us, or when the connection is lost.

While 1
    Sleep(100)
    If GUIGetMsg() = $gui_event_close Then _exit()
WEnd

Func Connected($hSocket, $iError); We registered this (you see?), When we're connected (or not) this function will be called.
    $fsocket = $hSocket
    If Not $iError Then; If there is no error...
        ;
    Else; ,else...
        ;
    EndIf
EndFunc   ;==>Connected

Func Received($hSocket, $sReceived, $iError); And we also registered this! Our homemade do-it-yourself function gets called when something is received.
    ;msgbox(0, "client " & $CmdLine[1], "received", 0.5)
    _send("pong " & $CmdLine[1])
EndFunc   ;==>Received

Func Disconnected($hSocket, $iError); Our disconnect function. Notice that all functions should have an $iError parameter.
    ;
EndFunc   ;==>Disconnected

Func _send($sReply)
    _TCP_Send($fsocket, $sReply)
EndFunc   ;==>_send

Func _exit()
    Exit
EndFunc   ;==>_exit

Include for both: Kip's TCP.au3: http://www.autoitscript.com/forum/index.php?showtopic=74325&st=0

or the file attached to this thread.

Steps to reproduce:

- compile client.au3

- run server.au3

- press button >1< to start

- count the message box popups. There should be 15 of them

Exit server.au3 and comment out this line in server.au3:

sleep(100); <---------- comment this line out for testing <----------

- run server.au3 again

- press button >1< to start

- count the pop ups. There are going to be less than 15 of them

Any idea what's causing this? I'd really like to have all client processes up as quickly as possible without having to artificially delay their startup. Any help will be greatly appreciated.

TCP.au3

Edited by Sven
Link to comment
Share on other sites

Your steps to reproduce didn't work for me. Namely, I didn't get any message boxes at all... The server only makes messages when it receives something...but your clients never seem to send...

Thank you for trying. You did also press the "1" button, didn't you? Forgot to mention that in the OP, sorry.

Dammit, even my postings need bugfixing...

Edited by Sven
Link to comment
Share on other sites

I enabled a bunch of dirty logging trying to find the issue. On some of the clients, when they send their "pong" the underlaying TCPSend() function fails with "10038". Which means "Socket operation on nonsocket." according to http://msdn.microsoft.com/en-us/library/ms740668.aspx

It might be a bug in Kips library, but somehow, your "_TCP_Send($fsocket, $sReply)" line on the client is having an invalid $fsocket.

By the way, the internal function _TCP_Server_Broadcast() is what you should probably being doing instead of your own looping on the server... Not that it breaks or fixes anything this case.

Link to comment
Share on other sites

Thank you, looks like I found a solution. Moved

$fsocket = $hSocket
from the Clients "connected()" function to the "received()" function.

I'm individually calling each client, because that's what I have to do in the final script. Each of the clients will be responsible for gathering information from a specific location out of a pool of locations, and reporting them back to the server for processing. This approach will allow me to run all the data gathering processes at once instead of sequentially, which should quite significantly cut back on the time it takes to gather all the data. Since I have to tell each client where to look on each single run, individually messaging them seems like the best approach to me.

Link to comment
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
 Share

  • Recently Browsing   0 members

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