Jump to content

TCP UDF, Event driven!


Kip
 Share

Recommended Posts

To be more precise a have problems with 10035 error, which is related with non-blocking mode while sending file, but I am not experienced with TCP\IP, so I am not sure. If it is not necessary I don't want to provide my code, because it is very messy now (i am testing every idea I can find out).

Link to comment
Share on other sites

Is it safe for a script to create both servers and clients? How does that work?

Something like this?:

#include <TCP.au3>

$hServer = _TCP_Server_Create(88)

_TCP_RegisterEvent($hServer, $TCP_NEWCLIENT, "NewClient")
_TCP_RegisterEvent($hServer, $TCP_RECEIVE, "RecvFromClient")
_TCP_RegisterEvent($hServer, $TCP_DISCONNECT, "DisconFromClient")

$hClient = _TCP_Client_Create("127.0.0.1", 88)

_TCP_RegisterEvent($hClient, $TCP_CONNECT, "ConnectedToServer")
_TCP_RegisterEvent($hClient, $TCP_RECEIVE, "RecvFromServer")
_TCP_RegisterEvent($hClient, $TCP_DISCONNECT, "DisconFromServer")

While (1)
    ; Do things.
WEnd

Func NewClient($hSocket, $iError);
    If Not ($iError) Then
        
    Else
    
    EndIf
EndFunc

Func RecvFromClient($hSocket,$sReceived,$iError)
    If Not ($iError) Then
        
    Else
    
    EndIf
EndFunc

Func DisconFromClient($hSocket, $iError)
    If Not ($iError) Then
        
    Else
    
    EndIf
 EndFunc
 
Func ConectedToServer($hSocket, $iError)
    If Not ($iError) Then
        
    Else
    
    EndIf
EndFunc

Func RecvFromServer($hSocket, $sReceived, $iError)
    If Not ($iError) Then
        
    Else
    
    EndIf
EndFunc

Func DisconFromServer($hSocket, $iError)
    If Not ($iError) Then
        
    Else
    
    EndIf
EndFunc

Time + Effort = Constant

Link to comment
Share on other sites

Why would you need client and server in one Script?

*GERMAN* [note: you are not allowed to remove author / modified info from my UDFs]My UDFs:[_SetImageBinaryToCtrl] [_TaskDialog] [AutoItObject] [Animated GIF (GDI+)] [ClipPut for Image] [FreeImage] [GDI32 UDFs] [GDIPlus Progressbar] [Hotkey-Selector] [Multiline Inputbox] [MySQL without ODBC] [RichEdit UDFs] [SpeechAPI Example] [WinHTTP]UDFs included in AutoIt: FTP_Ex (as FTPEx), _WinAPI_SetLayeredWindowAttributes

Link to comment
Share on other sites

someone had an idea for the detection server / client goes in the sense that the 2 client detects a server connection?

May i point out the obvious and just say use it backwards. use 2 servers and 1 client. they can talk back and forth, it isn't a one way TCP connection.

Server just means its the one that provides services.

in this case it means the part of the program that manages connections and accepts them. the client is the one that initiates the connection.

what you do after that is entirely up to how you code it.

Edited by Yeik
func get_quote()
   local $quote 
   switch random(1, 3, 1)
    case 1
     $quote = '"' & "A human being should be able to change a diaper, plan an invasion, butcher a hog, conn a ship, " & _
       "design a building, write a sonnet, balance accounts, build a wall, set a bone, comfort the dying, take orders, give " & _
       "orders, cooperate, act alone, solve equations, analyze a new problem, pitch manure, program a computer, cook a tasty meal, " & _
       "fight efficiently, die gallantly. Specialization is for insects." & '"' & " Robert A. Heinlein"
    case 2
     $quote =  '"' & "Within each of us lies the power of our consent to health and sickness, to riches and poverty, to freedom " & _
       "and to slavery. It is we who control these, and not another." & '"' & " Richard Bach (Illusions)"
    case 3
     $quote ='"' & "Don't handicap your children by making their lives easy." & '"' & " Robert A. Heinlein"
   EndSwitch
   MsgBox(0, "Quote for the moment", $quote & @CRLF)
EndFunc

get_quote()
Link to comment
Share on other sites

Func Disconnect($hSocket, $iError); Damn, we lost a client. Time of death: @Hour & @Min & @Sec :P
    
     ToolTip("SERVER: Client disconnected.",10,30); Placing a tooltip right under the tooltips of the client.
    
EndFunc

As you can get the ip of the user disconnects =?

_TCP_Server_ClientIP() not found

Link to comment
Share on other sites

Kip, someone trying to use my example script noticed an error when using my script with your newest udf.

In my code I have set it so you must declare variables. and you forgot to declare variables for $uBound.

Just a suggestion that you should use this, or make sure you test with it at least, helps those of us that are picky about our code that use it not come into problems later.

func get_quote()
   local $quote 
   switch random(1, 3, 1)
    case 1
     $quote = '"' & "A human being should be able to change a diaper, plan an invasion, butcher a hog, conn a ship, " & _
       "design a building, write a sonnet, balance accounts, build a wall, set a bone, comfort the dying, take orders, give " & _
       "orders, cooperate, act alone, solve equations, analyze a new problem, pitch manure, program a computer, cook a tasty meal, " & _
       "fight efficiently, die gallantly. Specialization is for insects." & '"' & " Robert A. Heinlein"
    case 2
     $quote =  '"' & "Within each of us lies the power of our consent to health and sickness, to riches and poverty, to freedom " & _
       "and to slavery. It is we who control these, and not another." & '"' & " Richard Bach (Illusions)"
    case 3
     $quote ='"' & "Don't handicap your children by making their lives easy." & '"' & " Robert A. Heinlein"
   EndSwitch
   MsgBox(0, "Quote for the moment", $quote & @CRLF)
EndFunc

get_quote()
Link to comment
Share on other sites

  • 2 weeks later...

One question.. How would I efficiently check if there is a server available to connect to in a loop?

something like:

if TCPConnect($server,$port)<>-1 then
;server is available...
endif

This way i don't know how use the handle from TCPconnect in UDF... and i don't know how to do this with UDF.

I guess the actual question is: How to write a loop so that when the server is unavailable it can try again.

Edited by dexto
Link to comment
Share on other sites

Sorry, I'm new to TCP so here are my thoughts on it.

Would this confuse the server into creating a fake socket before it create a real connection?

While TCPConnect($server, $port) = -1
    sleep(5000)
WEnd
$hClient = _TCP_Client_Create($server, $port)
_TCP_RegisterEvent($hClient, $TCP_RECEIVE, "Received")
_TCP_RegisterEvent($hClient, $TCP_DISCONNECT, "Disconnected")
ToolTip("CLIENT: Connecting...", 10, 10)
Link to comment
Share on other sites

Will this be ok?

#include "TCP.au3"
$server=@IPAddress1
$port=888
$tcp = 1
$debug=0
While True
    While $tcp
        ;While TCPConnect($server, $port) = -1
        ;   sleep(5000)
        ;WEnd
        ToolTip("CLIENT: Connecting...", 10, 10)
        $hClient = _TCP_Client_Create($server, $port)
        _TCP_RegisterEvent($hClient, $TCP_CONNECT, "Connected")
        Sleep(5000)
    WEnd 
    Sleep(1000)
WEnd

Func Connected($hSocket, $iError)
    If Not $iError Then
        $debug+=1
        $tcp = 0
        _TCP_RegisterEvent($hClient, $TCP_RECEIVE, "Received")
        _TCP_RegisterEvent($hClient, $TCP_DISCONNECT, "Disconnected")
        ToolTip("CLIENT: Connected: "&$debug, 10, 10)
    Else
        ToolTip("CLIENT: Could not connect.", 10, 10)
    EndIf
EndFunc   ;==>Connected


Func Received($hSocket, $sReceived, $iError)
    ToolTip("CLIENT: We received this: " & $sReceived, 10, 10)
EndFunc   ;==>Received

Func Disconnected($hSocket, $iError)
    ToolTip("CLIENT: Connection closed or lost.", 10, 10)
    $tcp = 1
EndFunc   ;==>Disconnected
Link to comment
Share on other sites

$tcp = 0 disabling the loop when client connects so that inner loop will not run anymore when its connected until Disconnected runs and sets $tcp = 1 activating inner loop again that is trying to connect again. I dono if this is valid or may be there is a better way let me know. Thank you.

Link to comment
Share on other sites

$tcp = 1
$debug=0
While True
    While $tcp
       ;While TCPConnect($server, $port) = -1
       ;   sleep(5000)
       ;WEnd
        ToolTip("CLIENT: Connecting...", 10, 10)
        $hClient = _TCP_Client_Create($server, $port)
        _TCP_RegisterEvent($hClient, $TCP_CONNECT, "Connected")
        Sleep(5000)
    WEnd
    Sleep(1000)
WEnd
I don't see you setting $tcp to 0 in this piece of code.

Link to comment
Share on other sites

Not in the loop but

_TCP_RegisterEvent($hClient, $TCP_CONNECT, "Connected"); this calls Connected function that sets $tcp=0 if there is no error

Alsow, I found this to work better thou i don't fully understand why.

#include "TCP.au3"
$server=@IPAddress1
$port=888
$tcp = 1
$debug=0
While True
    While $tcp; loop untill we are connected (again)
        ToolTip("CLIENT: Connecting...", 10, 10)
        $hClient = _TCP_Client_Create($server, $port)
        _TCP_RegisterEvent($hClient, $TCP_CONNECT, "Connected");call connected to see if the server is available
        Sleep(5000)
    WEnd 
    Sleep(1000)
WEnd

Func Connected($hSocket, $iError)
    If Not $iError Then
        ; server is ok, we connected
        $debug+=1
        $tcp = 0
        _TCP_RegisterEvent($hClient, $TCP_CONNECT, ""); since we are connected don't connect anymore
        _TCP_RegisterEvent($hClient, $TCP_RECEIVE, "Received"); set the function to receve data
        _TCP_RegisterEvent($hClient, $TCP_DISCONNECT, "Disconnected"); set the function to disconnect
        ToolTip("CLIENT: Connected: "&$debug, 10, 10)
    Else
        ToolTip("CLIENT: Could not connect.", 10, 10)
    EndIf
EndFunc   ;==>Connected

Func Received($hSocket, $sReceived, $iError)
    ToolTip("CLIENT: We received this: " & $sReceived, 10, 10)
EndFunc   ;==>Received

Func Disconnected($hSocket, $iError)
    _TCP_RegisterEvent($hClient, $TCP_RECEIVE, ""); since we are disconnected no point is receving anything
    _TCP_RegisterEvent($hClient, $TCP_DISCONNECT, ""); alsow we can't disconnect whats already disconnected
    ToolTip("CLIENT: Connection closed or lost.", 10, 10)
    $tcp = 1
EndFunc   ;==>Disconnected
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...