Sign in to follow this  
Followers 0
jiglei

Connecting two autoit programs with tcp functions.

9 posts in this topic

#1 ·  Posted (edited)

Hey there guys, I've been messing up my head cause of this problem... I'm trying to connect two autoit compiled(or not) scripts through the internet. I've searched the forum and found a very usefull UDF that allows to create servers and clients, sorry that I can't recall the scripters name.

The UDF is TCP.au3, dunno if you guys have it or not, either ways, im attaching it.

So... I've tried to connect two programs, a server one that recieves the connectios, and another that is a client.

Doing it on my pc, works fabously great, but when I try to do it on a friends pc, to connect to mine, it just doesn't work, the connection fails...

Here's the code of the server:

#include <WindowsConstants.au3>
#include <GuiEdit.au3>
#include <ScrollBarConstants.au3>
#include <GUiListView.au3>
#include <TCP.au3>
Global $Server, $Start,$IP=@IPAddress2
Opt("GUIONEVENTMODE", 1)
Opt("GUICloseOnESC", 0)
Opt("TrayOnEventMode", 1)
Opt("TrayAutoPause", 0)
Opt("TrayMenuMode", 1)
Opt("TrayIconHide", 1)
TraySetOnEvent(-13, "_Show")
Global $ini = @ScriptDir & "\servercfg.ini"
$Form1 = GUICreate("O-Bot Server Window", 1100, 518, -1, -1)
$Group1 = GUICtrlCreateGroup("Clients List", 0, 0, 233, 169)
$ListView1 = GUICtrlCreateListView("VIP ID|Socket|Login", 8, 16, 218, 150)
GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 0, 75)
GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 1, 60)
GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 2, 70)
GUICtrlCreateGroup("", -99, -99, 1, 1)
$Group2 = GUICtrlCreateGroup("Packets/Messages - IN", 240, 0, 481, 169)
$Edit2 = GUICtrlCreateEdit("", 248, 16, 465, 145, BitOR($ES_AUTOVSCROLL, $ES_READONLY, $ES_WANTRETURN, $WS_VSCROLL))
GUICtrlSetBkColor(-1, 0xFFFFFF)
GUICtrlCreateGroup("", -99, -99, 1, 1)
$Group3 = GUICtrlCreateGroup("Packets/Messages - OUT", 240, 168, 481, 169)
$Edit3 = GUICtrlCreateEdit("", 248, 184, 465, 145, BitOR($ES_AUTOVSCROLL, $ES_READONLY, $ES_WANTRETURN, $WS_VSCROLL))
GUICtrlSetBkColor(-1, 0xFFFFFF)
GUICtrlCreateGroup("", -99, -99, 1, 1)
$Group4 = GUICtrlCreateGroup("Enviar mensagem manual", 240, 336, 481, 57)
$Input1 = GUICtrlCreateInput("", 248, 360, 353, 21)
$Combo1 = GUICtrlCreateCombo("Selecionar Client", 608, 360, 105, 25)
GUICtrlSetData(-1, "Todos os clients")
GUICtrlCreateGroup("", -99, -99, 1, 1)
$Button1 = GUICtrlCreateButton("Criar novo VIP", 8, 184, 75, 25, $WS_GROUP)
$Group5 = GUICtrlCreateGroup("Clients Status", 728, 0, 369, 169)
$ListView2 = GUICtrlCreateListView("VIP ID|Estado|Estado de Heist|Último Login", 736, 16, 354, 150)
GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 0, 75)
GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 1, 88)
GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 2, 87)
GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 3, 100)
GUICtrlCreateGroup("", -99, -99, 1, 1)
$Button2 = GUICtrlCreateButton("Editar VIP", 8, 224, 75, 25, $WS_GROUP)
$Button3 = GUICtrlCreateButton("Apagar VIP", 8, 264, 75, 25, $WS_GROUP)
$Button4 = GUICtrlCreateButton("Iniciar Servidor", 832, 256, 75, 25, $WS_GROUP)
$Input2 = GUICtrlCreateInput("", 848, 200, 49, 21)
$Label1 = GUICtrlCreateLabel("Server Port:", 784, 203, 60, 17)
$Button5 = GUICtrlCreateButton("Disconectar", 928, 256, 75, 25, $WS_GROUP)
$Button6 = GUICtrlCreateButton("Kick VIP", 112, 224, 75, 25, $WS_GROUP)
$Label2 = GUICtrlCreateLabel("Server IP Reference:", 784, 232, 104, 17)
$Label3 = GUICtrlCreateLabel($IP, 928, 232, 200, 17)
$Button7 = GUICtrlCreateButton("Emergency BAN", 104, 264, 91, 25, $WS_GROUP)
$Label4 = GUICtrlCreateLabel("ESTADO ACTUAL:", 736, 296, 113, 19)
GUICtrlSetFont(-1, 10, 800, 0, "Times New Roman")
$Label5 = GUICtrlCreateLabel("OFF", 848, 296, 200, 19)
GUICtrlSetFont(-1, 10, 800, 0, "Times New Roman")
GUICtrlSetColor(-1, 0xFF0000)
$Group7 = GUICtrlCreateGroup("Log", 0, 400, 1097, 113)
$Edit1 = GUICtrlCreateEdit("", 8, 414, 1077, 96, BitOR($ES_AUTOVSCROLL, $ES_WANTRETURN, $WS_VSCROLL))
GUICtrlCreateGroup("", -99, -99, 1, 1)
#EndRegion ### END Koda GUI section ###
_SetOnEvent()
_Log("Program Startup!")
_UpdateStatus()
_ReadIni()
GUISetState(@SW_SHOW)
While 1
    Sleep(20000)
WEnd
Func _Exit()
    #Region --- CodeWizard generated code Start ---
    ;MsgBox features: Title=Yes, Text=Yes, Buttons=Yes and No, Icon=Question
    If Not IsDeclared("iMsgBoxAnswer") Then Local $iMsgBoxAnswer
    $iMsgBoxAnswer = MsgBox(36, "Confirmação", "Deseja mesmo sair?")
    Select
        Case $iMsgBoxAnswer = 6 ;Yes
            Exit
        Case $iMsgBoxAnswer = 7 ;No

    EndSelect
    #EndRegion --- CodeWizard generated code Start ---
EndFunc   ;==>_Exit
Func _Hide()
    WinSetState("O-Bot Server Window", "", @SW_HIDE)
    Opt("TrayIconHide", 0)
EndFunc   ;==>_Hide
Func _Show()
    WinSetState("O-Bot Server Window", "", @SW_SHOW)
    WinSetState("O-Bot Server Window", "", @SW_RESTORE)
    Opt("TrayIconHide", 1)
EndFunc   ;==>_Show
Func _Save()
    IniWrite(@ScriptDir & "\servercfg.ini", "Settings", "Port", GUICtrlRead($Input2))
EndFunc   ;==>_Save
Func _Log($Text, $Type = 0)
    $Hour = "[" & @HOUR & ":" & @MIN & ":" & @SEC & "] * "
    $Hour2 = "[" & @HOUR & ":" & @MIN & ":" & @SEC & "]->"
    $Error = "**ERROR!** ==>"
    Switch $Type
        Case 0
            Select
                Case GUICtrlRead($Edit1) = ""
                    GUICtrlSetData($Edit1, $Hour & $Text)
                Case Else
                    GUICtrlSetData($Edit1, GUICtrlRead($Edit1) & @CRLF & $Hour & $Text)
                    _GUICtrlEdit_Scroll($Edit1, $SB_SCROLLCARET)
            EndSelect
        Case 1
            Select
                Case GUICtrlRead($Edit1) = ""
                    GUICtrlSetData($Edit1, $Text)
                Case Else
                    GUICtrlSetData($Edit1, GUICtrlRead($Edit1) & @CRLF & $Text)
            EndSelect

        Case 2
            Select
                Case GUICtrlRead($Edit1) = ""
                    GUICtrlSetData($Edit1, $Hour & $Text)
                Case Else
                    GUICtrlSetData($Edit1, GUICtrlRead($Edit1) & @CRLF & $Hour & "**Warrior Buffing: " & $Text & "**")
                    _GUICtrlEdit_Scroll($Edit1, $SB_SCROLLCARET)
            EndSelect
        Case 3
            Select
                Case GUICtrlRead($Edit1) = ""
                    GUICtrlSetData($Edit1, $Hour & $Text)
                Case Else
                    GUICtrlSetData($Edit1, GUICtrlRead($Edit1) & @CRLF & $Hour & $Error & $Text)
                    _GUICtrlEdit_Scroll($Edit1, $SB_SCROLLCARET)
            EndSelect
        Case 4
            Select
                Case GUICtrlRead($Edit2) = ""
                    GUICtrlSetData($Edit2, $Hour & $Text)
                Case Else
                    GUICtrlSetData($Edit2, GUICtrlRead($Edit2) & @CRLF & $Hour & $Error & $Text)
                    _GUICtrlEdit_Scroll($Edit2, $SB_SCROLLCARET)
            EndSelect

    EndSwitch
EndFunc   ;==>_Log
Func _CreateVIP()
    Opt("guioneventmode", 0)
    $Form2 = GUICreate("New VIP", 134, 89, -1, -1)
    GUICtrlCreateGroup("VIP Login", 1, -1, 129, 89)
    GUICtrlCreateLabel("VIP ID:", 9, 15, 38, 17)
    $Input4 = GUICtrlCreateInput("", 49, 14, 73, 21)
    GUICtrlCreateLabel("PW:", 21, 37, 25, 17)
    $Input3 = GUICtrlCreateInput("", 49, 38, 73, 21, $ES_PASSWORD)
    $Button8 = GUICtrlCreateButton("Create", 33, 63, 73, 21, $WS_GROUP)
    GUICtrlCreateGroup("", -99, -99, 1, 1)
    GUISetState(@SW_SHOW)
    #EndRegion ### END Koda GUI section ###
    While 1
        $nMsg = GUIGetMsg()
        Switch $nMsg
            Case $GUI_EVENT_CLOSE
                GUIDelete($Form2)
                _SetOnEvent()
                ExitLoop
            Case $Button8
                IniWrite($ini, "Accounts", "AccountsNumber", IniRead($ini, "Accounts", "AccountsNumber", 0) + 1)
                $i = IniRead($ini, "Accounts", "AccountsNumber", 0)
                IniWrite($ini, "Accounts", "AccountID" & $i, GUICtrlRead($Input4))
                IniWrite($ini, "Accounts", "AccountPW" & $i, GUICtrlRead($Input3))
                IniWrite($ini, "Accounts", "AccountState" & $i, "Offline")
                IniWrite($ini, "Accounts", "AccountHState" & $i, "---")
                IniWrite($ini, "Accounts", "LastLogin" & $i, "---")
                GUIDelete($Form2)
                _SetOnEvent()
                ExitLoop
        EndSwitch
    WEnd
EndFunc   ;==>_CreateVIP
Func _EditVIP()
    ShellExecute($ini)
EndFunc   ;==>_EditVIP
Func _DelVIP()
EndFunc   ;==>_DelVIP
Func _StartServer()
    Select
        Case $Start = 1
        Case $Start = 0
            $Start = 1
            $Server = _TCP_Server_Create(GUICtrlRead($Input2))
            _Log("Server iniciado! Socket: " & $Server)
            _SocketRegister()
            GUICtrlSetData($Label5,"Conectado")
            GUICtrlSetColor($Label5, 0x00FF00)
            AdlibRegister("_ClientManage",30000)
    EndSelect
EndFunc   ;==>_StartServer
Func _StopServer()
    Select
        Case $Start = 1
            _TCP_SErver_stop()
            _Log("Server desligado!")
            GUICtrlSetData($Label5,"OFF")
            GUICtrlSetColor($Label5, 0xFF0000)
            $Start = 0
        Case $Start = 0
    EndSelect

EndFunc   ;==>_StopServer
Func _KickVip()
EndFunc   ;==>_KickVip
Func _EmergencyBan()
EndFunc   ;==>_EmergencyBan
Func _SendManualMessage()
    GUICtrlSetData($Input1, "")
EndFunc   ;==>_SendManualMessage
Func _SetOnEvent()
    Opt("GUIONEVENTMODE", 1)
    GUISetOnEvent($GUI_EVENT_CLOSE, "_Exit")
    GUISetOnEvent($GUI_EVENT_MINIMIZE, "_Hide")
    GUICtrlSetOnEvent($Input2, "_Save")
    GUICtrlSetOnEvent($Button1, "_CreateVIP")
    GUICtrlSetOnEvent($Button2, "_EditVIP")
    GUICtrlSetOnEvent($Button3, "_DelVIP")
    GUICtrlSetOnEvent($Button4, "_StartServer")
    GUICtrlSetOnEvent($Button5, "_StopServer")
    GUICtrlSetOnEvent($Button6, "_KickVip")
    GUICtrlSetOnEvent($Button7, "_EmergencyBan")
    GUICtrlSetOnEvent($Input1, "_SendManualMessage")
EndFunc   ;==>_SetOnEvent
Func _UpdateStatus()
    For $Start = 1 To IniRead($ini, "Accounts", "AccountsNumber", 0)
        If _GUICtrlListView_FindInText($ListView2, IniRead($ini, "Accounts", "AccountID" & $Start, 0)) = -1 Then
            GUICtrlCreateListViewItem(IniRead($ini, "Accounts", "AccountID" & $Start, 0) & "|" & IniRead($ini, "Accounts", "AccountState" & $Start, 0) & "|" & _
                    IniRead($ini, "Accounts", "AccountHState" & $Start, 0) & "|" & IniRead($ini, "Accounts", "LastLogin" & $Start, 0), $ListView2)
        Else
            $index = _GUICtrlListView_FindInText($ListView2, IniRead($ini, "Accounts", "AccountID" & $Start, 0))
            _GUICtrlListView_SetItemText($ListView2, $index, IniRead($ini, "Accounts", "AccountID" & $Start, 0), 0)
            _GUICtrlListView_SetItemText($ListView2, $index, IniRead($ini, "Accounts", "AccountState" & $Start, 0), 1)
            _GUICtrlListView_SetItemText($ListView2, $index, IniRead($ini, "Accounts", "AccountHState" & $Start, 0), 2)
            _GUICtrlListView_SetItemText($ListView2, $index, IniRead($ini, "Accounts", "LastLogin" & $Start, 0), 3)
        EndIf
    Next
EndFunc   ;==>_UpdateStatus
Func _ReadIni()
    $Port = IniRead($ini, "Settings", "port", "")
    GUICtrlSetData($Input2, $Port)
EndFunc   ;==>_ReadIni
Func _SocketRegister()
    _TCP_RegisterEvent($Server,$TCP_DISCONNECT,"_ServerStop")
    _TCP_RegisterEvent($Server,$TCP_NEWCLIENT,"_AddClient")
    _TCP_RegisterEvent($Server,$TCP_RECEIVE,"_TCPRead")
EndFunc
Func _AddClient($hSocket,$iError)
    GUICtrlCreateListViewItem("---" & "|" & $hSocket,$ListView1)
    _Log("New Client with socket " & $hSocket & " has connected!")
EndFunc
Func _TCPRead($hSocket, $sReceived, $iError)
_log("Recieved: " & $sReceived & " from socket " & $hSocket,4)
EndFunc
Func _ClientManage()
EndFunc

it's pretty incomplete since i didn't made it work through the internet, I didn't want to waste time on something that could not work...

And the client will have a function to connect that shall work something like this:

$IP = ;The created server's ip
$Client = _TCP_CreateClient(3714,$IP)

Can anyone tell me what I'm doing wrong? Can this UDF do what I want it to do? I've tried with different Ip's but only @IPADRESS2 reference seems to work on my pc, and anyother seemed to work over the net.

I'd apprecciate any type of help, suggestions on other UDF's, or anything.

Thanks in advance, jiglei

TCP.au3

Edited by jiglei

Share this post


Link to post
Share on other sites



Before making any script changes you need to be sure that the 2 computers (yours and your friend's) are on the same network (can you ping the other computer?)

If you can't ping the other computer your script will fail.

Solve the network part first, then see what happens with your script.


SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script

wannabe "Unbeatable" Tic-Tac-Toe

Paper-Scissor-Rock ... try to beat it anyway :)

Share this post


Link to post
Share on other sites

So they must be in the same network?

Isn't there a way to do it without it? :mellow: (a simple one)

Or is there anyway to make distant computers (distant locations) stay in the same network? Would hamachi program do it?

Share this post


Link to post
Share on other sites

Of course there is a way but it's much more complicated.

You need that your router to have that particular port you're using for your TCP server open (to listen on) ... maybe port-forwarding? and your friend needs to connect to your WAN IP on that specific port. Then you'll have a chance.


SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script

wannabe "Unbeatable" Tic-Tac-Toe

Paper-Scissor-Rock ... try to beat it anyway :)

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

Seems pretty much hard to me :/

Can you tell how hard it is? Shall I search about that port-forwarding thing? Or you think that's too much for a medium coder like me?

(If I can ask your opinion :mellow:)

Edited by jiglei

Share this post


Link to post
Share on other sites

I'm afraid it's too much for you.

That's my opinion.


SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script

wannabe "Unbeatable" Tic-Tac-Toe

Paper-Scissor-Rock ... try to beat it anyway :)

Share this post


Link to post
Share on other sites

I'm persistant :P

I'll try some workarounds to what i want then...

Thanks for your precious help :mellow:

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

Hi, you should try the following step by step:

- First step, connection on your local machine:

First, compile the example-script of TCPRecv() from the autoit helpfile to SERVER.EXE and run it on your computer. Some firewalls want to block this, allow it or deactivate the firewall.

Then load the example-script of TCPSend() from the helpfile and run the script (you could name it CLIENT.au3). If the input from the clientwindow appears in the server window, the first step is done. If not, then you have a problem in your network, fix it!

- Second step, connection in the local network.

If you have 2 computers in your local network, start the SERVER.EXE at the other computer (remember the firewall...). In the top border of the SERVER-window appears an IP-Address, something like 192.168.105.101. Replace the @IPAddress1 in the line 18 in the CLIENT-script (    Local $szIPADDRESS = @IPAddress1) with the IP from the SERVER-window. Like this:

Local $szIPADDRESS = "192.168.105.101"
DONT take the 192.168.105.101, take the IP from the SERVER-window....

Then run the client script at your local computer. The data you put into the client-window should be appear in the SERVER-window. Step 2 is done....If not, fix the problem in your network.....

- Third step, connection via internet. 

Run the SERVER.EXE on your local computer. If you have a router, you have to "inform" this router, that a server process (which is expect data) is running at the local machine. Take a look at the router-handbook or ask a friend who knows something about "port forwarding" or "Firewall rules". The router MUST know the IP of the SERVER (shown into the server-window) and the PORT (you find it in the AutoIt-sourcecode of the SERVER-script $nPort=xxxxx) so that the router knows to whom he must pass on the ip-packets where are coming from the internet. 

Next step, find out your WAN-IP. This is the IP that your router has been assigned by your internet provider. You can receive it from www.whatismyip.com (from your local computer). Then you should give this WAN-IP to your friend, which has the CLIENT-Script at his computer. (There shold be no problems with Step 1 and Step 2 on the computer of your friend!). He could try to ping this (your) WAN-IP. If your router allows pings, he gets an answer. Everything is fine!

Your friend needs to to replace the @IPAddress1 in line 18 of the CLIENT-script with "YOUR.WAN.IP" (which you got from www.whatismyip.com). If he runs the CLIENT-sript and the data from his input will arrive at your SERVER, step 3 is is completed successfully! There is no need to make any "port forwarding" at a client computer!

If your WAN-IP changes often, its time to get a "personal URL" from a DNS-Service-Provider for free. This URL (something like "randomname.dns-serviceprovider.net") must be registered into your router. Then your friend don't have to ask you every day which WAN-IP actually you have. Replace the "YOUR.WAN.IP" in the CLIENT-script with TCPNameToIP("randomname.dns-serviceprovider.net") and he can connect to you regardless of your current WAN-IP.

I hope it is understandable, what I have written here.  :mellow:

Happy TCP-ing....  :P

Andy

Edited by AndyG

Share this post


Link to post
Share on other sites

Wow, ty AndyG, I'll take a look.

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