Sign in to follow this  
Followers 0
sloppyprogrammer

Simple TCP server

6 posts in this topic

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


[center]They say that there is only 10 kinds of people that understand binary code; those who do, and those who don't...[/center][center]Get it? :P[/center]

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

---

Edited by playlet

Share this post


Link to post
Share on other sites

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


[center]They say that there is only 10 kinds of people that understand binary code; those who do, and those who don't...[/center][center]Get it? :P[/center]

Share this post


Link to post
Share on other sites

It exits as soon as it is oppened.. :/

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

It exits as soon as it is oppened.. :/

twiztedshane,

Welcome to the AutoIt forum. :oops: (sorry Melba32)

It is preferred that users do not revive (post on) old threads unless you have a significant contribution (and I mean significant).

You can see the "age" of a post right at the begging of it. (Posted 21 July 2009 - 03:03 PM)

Most code posted years ago will not work due to AutoIt revisions.

edit: corrected weird double quote

Edited by spudw2k

Share this post


Link to post
Share on other sites

Oh, i'm awfully sorry about that. I didn't mean to cause any trouble. =/

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