Jump to content

TCP Examples and Scripts


themax90
 Share

Recommended Posts

I've had alot of requests for TCP information and examples, as well as UDP, but just in general TCP. So, too refrain from reasking questions and automated responses I have made a collection of my posted scripts and examples, and will be updating with a FAQ, tutorials, and other things of that sort soon.

AutoIt-ITS Database Editor - Recommended for Secure Servers

The project AutoIt ITS is dead and all files can be found here : http://www.autoitscript.com/forum/index.php?showtopic=57470

ToDo List:

None.

Here is some rather "Technical" information on TCP : Click Here

Examples:

TCP Made Easy:

This can help you understand return values, and has everything on the TCP structure documented with a working example of Client/Server. TCPStartServer is highly recommended to use.

AdminDB:

A type of TCP Server that will store variables in memory allowing remote access and updating of more advanced type servers. A preset administrative password (if obfuscated) will make stealing ANY information in it near impossible. This server is used to store ITS Server data in realtime however the updated version is not released for security purposes. Converses in Hex.

Remote Server *NEW*:

Newest version support's almost ALL of AutoIt's functionality. Recommended for developer use only.

Remote Server *OLD*:

Older version of a "Remote" Server which has tons of nested pre-built functions. Recommended for home use.

Server Communicator:

Great for learning Syntax of other protocols such as POP3, SMTP, and if adapted correctly MySQL.

Optimized TCP Generic Server:

You can use this as just a BASIC background to making a server. All the code is there, and should be fully functional. Wrap your code around it and you have your own server! Highly Recommended. Update 1

TCP Client Example:

Global $MainSocket
Local $MaxLength = 512; Maximum Length Of Text
Local $Port = 1000; Port Number
Local $Server = @IPAddress1; Server IpAddress
TCPStartup()
$MainSocket = TCPConnect($Server, $Port)
If $MainSocket = -1 Then Exit MsgBox(16, "Error", "Unable to connect.")
While 1
    $Data = TCPRecv($MainSocket, $MaxLength)
    If $Data = "~bye" Then
        MsgBox(16, "Session Ended", "Connection Terminated.")
        Exit
    ElseIf $Data <> "" Then
; Unconditional Receive
        MsgBox(0, "Received Packet", $Data)
    EndIf
WEnd
Func OnAutoItExit()
    If $MainSocket <> - 1 Then
        TCPSend($MainSocket, "~bye")
        TCPCloseSocket($MainSocket)
    EndIf
    TCPShutdown()
EndFunc;==>OnAutoItExit

TCP Server Example:

Global $MainSocket = -1
Global $ConnectedSocket = -1
Local $MaxConnection = 1; Maximum Amount Of Concurrent Connections
Local $MaxLength = 512; Maximum Length Of String
Local $Port = 1000; Port Number
Local $Server = @IPAddress1; Server IpAddress
TCPStartup()
$MainSocket = TCPListen($Server, $Port)
If $MainSocket = -1 Then Exit MsgBox(16, "Error", "Unable to intialize socket.")
While 1
    $Data = TCPRecv($ConnectedSocket, $MaxLength)
    If $Data = "~bye" Then
        MsgBox(16, "Session Ended", "Connection Terminated.")
        Exit
    ElseIf $Data <> "" Then
; Unconditional Receive
        MsgBox(0, "Received Packet", $Data)
    EndIf
    If $ConnectedSocket = -1 Then
        $ConnectedSocket = TCPAccept($MainSocket)
        If $ConnectedSocket <> -1 Then
; Someone Connected
            TCPSend($ConnectedSocket, "Connected!")
        EndIf
    EndIf
WEnd
Func OnAutoItExit()
    If $ConnectedSocket <> - 1 Then
        TCPSend($ConnectedSocket, "~bye")
        TCPCloseSocket($ConnectedSocket)
    EndIf
    If $MainSocket <> -1 Then TCPCloseSocket($MainSocket)
    TCPShutdown()
EndFunc;==>OnAutoItExit

You can also check my fileman at the below address for examples and random scripts I make. Please note : If a forum post is out of date, check the file man. All scripts are in there, however some may not be updated to work in the current release or beta.

http://www.autoitscript.com/fileman/users/AutoIt%20Smith/

AutoIt Smith

Updated July 27th, 2006

Edited by AutoIt Smith
Link to comment
Share on other sites

  • Replies 47
  • Created
  • Last Reply

Top Posters In This Topic

Top Posters In This Topic

Remote Server *NEW*:

Newest version support's almost ALL of AutoIt's functionality ONLY if AutoIt is installed on host computer. May also hardcode the AutoIt.exe using FileInstall for users without AutoIt installed primarily. Recommended for developer use only.

When you compile a script, such as the server, AutoIt writes all the built-in functions into the exe. Therefore, when you run a command using @Autoitexe and /Autoit3executeline, you're running it from the server's compiled exe. In this case, you do not need to have AutoIt installed.

Edit - Even with AutoIt installed, if you compile a script and run it, it is simply running from that compiled script. Check the Task Manager when you run a compiled script - the task will be "name of the script".exe.

Edited by greenmachine
Link to comment
Share on other sites

I'm sure it's a common mistake. The only time you need the actual AutoIt exe is when you're running a script (au3), or when you're running an encrypted script (a3x) and you're not using another compiled script to do so.

On a more positive note, instead of just pointing out errors I will say this: these scripts are very handy. Thanks for making them, they really helped me get a basis of understanding TCP functions.

Edited by greenmachine
Link to comment
Share on other sites

  • 2 weeks later...
  • 3 weeks later...

Email!!!

Recently I have been researching and developing Email technologies. All that remains to make a true pop3/smtp client is having the strings sorted according to the server. A very simple task. So here is an example script demonstrating Pop3 protocol.

Global $Recv
Global $Server = "mail.comcast.net"
Global $ServerIp = TCPNameToIP($Server)
TCPStartup()
Global $MainSocket = TCPConnect($ServerIp, 110)
If $MainSocket = -1 Then Exit MsgBox(0, "Failure", "Failed to contact server.")
Global $GUI = GUICreate("Pop Speak Testing", 180, 300)
Global $PopSpeak = GUICtrlCreateInput("", 10, 10, 160, 20)
Global $Send = GUICtrlCreateButton("Send", 90, 40, 80, 15)
Global $Help = GUICtrlCreateButton("Help", 10, 40, 75, 15)
Global $History = GUICtrlCreateEdit("Contacting " & $Server & " > " & $ServerIp, 10, 65, 160, 225, 0x00200000)
GUISetState(@SW_SHOW)
While 1
    $msg = GUIGetMsg()
    Select
    Case $msg = -3
        Exit
    Case $msg = $Send
        TCPSend2($MainSocket, GUICtrlRead($PopSpeak) & @CRLF)
    Case $msg = $Help
        TCPSend2($MainSocket, "help" & @CRLF)
    EndSelect
    $Recv = TCPRecv($MainSocket, 512)
    Select
    Case $Recv <> ""
        FileWriteLine("Log.txt", $Recv)
        GUICtrlSetData($History, GUICtrlRead($History) & @CRLF & "In: " & $Recv)
    Case Else
    ; There is no message.
    EndSelect
WEnd
Func OnAutoItExit()
        TCPSend($MainSocket, "quit" & @CRLF)
    TCPCloseSocket($MainSocket)
    TCPShutdown()
EndFunc
Func TCPSend2($Socket, $Text)
    TCPSend($Socket, $Text)
    FileWriteLine("Log.txt", $Text)
    GUICtrlSetData($History, GUICtrlRead($History) & @CRLF & "Out: " & $Text)
    GUICtrlSetData($PopSpeak, "")
EndFunc

NOTES ON POP3 INTERFACE

For any TCP interfacing using AutoIt you must send the text followed by a clean line.

Example : TCPSend($Socket, $Data & @CRLF)

Usually getting an IpAddress using TCPNameToIP($Name) will sort out most connection issues.

Do not login using a TCP program made by AutoIt and abruptly close the connection without first sending quit, otherwise AutoIt will not be allowed to connect on the Pop3 socket until your next reboot.

Hope you enjoy and people can make things out of my notes.

- AutoIt Smith

Link to comment
Share on other sites

I think some people may also find this useful:

http://www.networksorcery.com/enp/topic/ipsuite.htm

Just in case anyone asks me what are layers, here they are:

Layer 7: Application : Where the actual Application is (i.e. your autoit app)

Layer 6: Presentation: Responsible for data translation and code formatting

Layer 5: Session :Setting up, managing, then tearing down sessions (NFS, SQL, RPC, X Window, ASP, DNA SCP)

Layer 4: Transport : Reassembles data into a data stream (TCP, UDP: Working wirh IP addresses)

Layer 3: Network : Manages device addressing, location of device on network and determines the best way to move data (Routers: Working with MAC addresses [the one that is burnt into your network card]) {DATA PACKETS}

Layer 2: Data Link : Error notification, network topology, and flow control. Ensure devivered to proper lan using Physical(MAC) addresses (Switches, Bridges) {DATA FRAMES}

Layer 1: Physical : Sends 1s and 0s (Hubs)

Edited by nfwu
Link to comment
Share on other sites

Not my first time doing Networking code, but it's my first time in AutoIt.

1) Are the TCP/UDP functions Blocking (i.e. They wait for data before returning) or are they Non-Blocking (i.e. They check the stream and return nothing if no data, otherewise, they return the data)

2) If the TCP/UDP functions are blocking, is there a timeout? (I think Opt("TCPTimeout", 100) is what i'm looking for)

3) How is the TCP/UDP data returned? As a string or an array?

Edited by nfwu
Link to comment
Share on other sites

I am currently doing something which I have never thought I would do. I am releasing AdminDB.

Go here to find what it is :

http://www.autoitscript.com/forum/index.php?showtopic=22371

I never thought I would release this because it was the secret weapon to hide passwords in my server system, but I see no folly as the password can be a long random string no one can guess, and soon I will make my own encryption system, making it uneasy to break into.

Edited by AutoIt Smith
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...