Hi!
This is my script for a very simple server.
It works very nice, and it took me about 15 min to make, so it's kinda sloppy... I've cleaned up the code somewhat, but if I've bothered, I could have made it much cleaner...
I know this isn't advanced or anything, but since it's so well commented, if i may say so, I think that it may help out ppl who are new to networking in AutoIt.
global $l_IP = @IPAddress1 ; Set the listening IP
global $conned = False ; set the conned var to false (really no use for this... Used it in an older version. will be removed soon.)
global $ConnectedSocket = ""
; ======================================================= ;
TCPStartUp() ; Starts TCP services
; ======================================================= ;
$MainSocket = TCPListen($l_IP, 65432, 100 ) ; start a listening socket
If $MainSocket = -1 Then Exit ; if a socket can't be created, exit the server
; ======================================================= ;
_waitForConn() ; Start the loop that makes the program look for clients.
func _waitForConn()
While $conned = False ; look for client connection loop
$ConnectedSocket = TCPAccept( $MainSocket) ; check if somebody wants to connect
If $ConnectedSocket >= 0 Then ; if a client tries to connect, let him do so.
beep(500,120) ; make a beep (for debugging purposes)
$conned = true ; set the conned var to true (really no use for this... Used it in an older version. will be removed soon.)
_iAmConned() ; start the connected loop.
EndIf
Wend
EndFunc
; ======================================================= ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; ======================================================= ;
func _iAmConned()
While $conned = True ; if connected, then...
$pack = TCPRecv($ConnectedSocket,2048) ; recive packages...
if $pack <> "" then _payload($pack) ; open them and check if they are empty. If they are NOT empty, run the payload function.
WEnd
EndFunc
; ======================================================= ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; ======================================================= ;
func _payload($myPayload) ; starts the payload function.
Switch $myPayload ; Check the contents of the last recived package.
case "doSomething1" ; If the package contained the message "doSomething1" then
; ... ; Do something
; ... ; Do something
; ... ; Do something
case "doSomething2" ; If the package contained the message "doSomething2" then
; ... ; Do something
; ... ; Do something
; ... ; Do something
EndSwitch ; more can be added...
EndFunc
; ======================================================= ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; ======================================================= ;
func _resetConnection()
TCPCloseSocket ($ConnectedSocket) ; Closes the socket
$ConnectedSocket = -1 ; Resets the ConnectedSocket placeholder
$conned = False ; set the conned var to false (really no use for this... Used it in an older version. will be removed soon.)
_waitForConn() ; go back to the "searching for a connection" function.
EndFunc
; ======================================================= ;
Use it for whatever you want!
Credit not needed, but would be nice! =)
Cheers!
- SloppyProgrammer