Jump to content

sloppyprogrammer

Members
  • Posts

    7
  • Joined

  • Last visited

Everything posted by sloppyprogrammer

  1. Removing ; ====================== was actually something I was thinking about, but when I'm bored, I put in useless crap like that into my scripts, and never take it out... Bad habit I suppose... - SloppyProgrammer
  2. 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
  3. Hmm.... You could try something like this for the "spamming" problem: While 1 ; InputBox for data to transmit $szData = "hello" ; If they cancel the InputBox or leave it blank we exit our forever loop If @error Or $szData = "" Then ExitLoop ; We should have data in $szData... lets attempt to send it through our connected socket. TCPSend($ConnectedSocket, $szData) TCPSend($ConnectedSocket, "") <--------------------------------------------------- !!! ; If the send failed with @error then the socket has disconnected ;---------------------------------------------------------------- If @error Then ExitLoop WEnd So when the message is sendt, it will clear the message sendt via that specific socket! Worked for me! as for the the multi client problem: While 1 ; InputBox for data to transmit $szData = @IPAddress1 & ": hello" <--------------------------------------------------- !!! ; If they cancel the InputBox or leave it blank we exit our forever loop If @error Or $szData = "" Then ExitLoop ; We should have data in $szData... lets attempt to send it through our connected socket. TCPSend($ConnectedSocket, $szData) TCPSend($ConnectedSocket, "") ; If the send failed with @error then the socket has disconnected ;---------------------------------------------------------------- If @error Then ExitLoop WEnd It would work for a LAN, but via the internet, I am not sure how the @IPAddress1 macro works. Possibly it would only send its local ip(ex: 192.168.0.124) and that would not help you in any way... :S You could use the SocketToIP function, but that would require you to make uniqe handles for each connected client, for example that every client would be stored in an array, I belive. But then you would have challenges like: handing out id's to the clients, closing spec sockets as they disconnect, and so on... Hope that this was of some help! =) EDIT: Fixed a formatting problem.
  4. Isn't there a template for this in Koda? :S
  5. Thanks GtaSpider!
  6. Very nice UDF! Thanks for sharing!
×
×
  • Create New...