Jump to content

Recommended Posts

guinness

Nice tutorial. You should probably look at using variable declaration in your examples. For example your Global declaration should be Local as the variable isn't used anywhere else but in Local scope.


UDF List:

 
_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_ArrayFilter/_ArrayReduce_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 22/04/2018

Share this post


Link to post
Share on other sites
caleb41610

I'm sure this will help a lot of people who are new to TCP!

I would love to see an example as detailed as this about maintaining multiple connections!

Share this post


Link to post
Share on other sites
bogQ

server is already set to handle more than 1 connection, you just need to:

get rid of msgbox (use gui commands instead) that pause server

limit if needed numebr of connection that it will handle

change how and when will server disconnect clients

Read server part with explained code for more info.

Edited by bogQ

TCP server and client - Learning about TCP servers and clients connection
Au3 oIrrlicht - Irrlicht project
Au3impact - Another 3D DLL game engine for autoit. (3impact 3Drad related)



460px-Thief-4-temp-banner.jpg
There are those that believe that the perfect heist lies in the preparation.
Some say that it’s all in the timing, seizing the right opportunity. Others even say it’s the ability to leave no trace behind, be a ghost.

 

Share this post


Link to post
Share on other sites
Hubert24

Thanks for the code and tutorial, helpful codes. let me start by saying i am a beginner, so please don't laugh too at my questions!

1. with your codes, I have to run the server and client codes on every PC connected on my network. is there a possibility that I might run the codes for server just once and on the other PCs, i will just have to run but the codes for clients?

2. I wish to have 3 or more Computers, the first computer is my master, where my server runs, the other computers are my client. I wish to send message in both dirrection via TCP as is the case with your codes how am I going to do it?

3. I have tried to open TCP socket via internet but all my attemps have failed. my internet IPaddress is 192.168.1.1

I will be grateful if you find time and provide me some helps.

Best regards

Hubert

Share this post


Link to post
Share on other sites
bogQ

Guess i need to add litle more info about networks on first post

lets expalind it heare like this

I have 2 computers and i need to do TCP over wireless network.

My first computer (he will be server) need to lissen on my second adapter (my first adapter is lan, my second adapter is wireless card), he (wirelles card) is with IP adress @IPAddress2 or 192.168.0.1 (@IPAddress2 is my wireless adapter IP that i manyaly tuped on adapter)

my second computer that have only client on him is with ip address 192.168.0.2 on his wireless lan card and he is connected over that card to my wireless card so that we can be in network, but his IP is not important cos i only need to know server IP adress 192.168.0.1 so that i can connect on him.

so on server im starting it with

$Listen = TCPListen(@IPAddress2, 1018, 500)

or with $Listen = TCPListen('192.168.0.1', 1018, 500), it is identical cos @IPAddress2 will return (in my case) IP address of my second device 192.168.0.1

and on client script im putting

$Socket = TCPConnect('192.168.0.1', 1018)

so im connection on IP adress of server with client

now let's say that i wanna connect more than one pc but not on my wireless network cos it accept only 2 computers, i wanna do it on lan network adapter (my first lan device), on lan i have 6 PC-s connected on network.

My first comp is with ip adress lets say 10.0.252.30 on my local area connection adapter.

How can i confirm that my ip is 10.0.252.30?! From start meny i sellect run (or in win 7 just tupe cmd and hit enter with no run) tupe in cmd and press enter. after that in cmd i type in ipconfig and press enter.

it will list all of your adapters and their ip (IPv4) addresses.

So im gona start server on my network lan adapter

$Listen = TCPListen('10.0.252.30', 1018, 500)

and every client i connect on him will connect to that ip adress

so on client im dooing this

$Socket = TCPConnect('10.0.252.30', 1018)

So i will connect more than one client and only one comp (my first comp) will serve as my server.

Note that somethimes firewall can cose problems, so if you have problems turn it off to see if it will work.

As for internet communication i can't help a lot, i never tried coz my ports are forwarded. Main thing i know is if your behind router form your ISP and your ports arn't forwarded you need to call your ISP on the phone and to tell him to forward that darn port and don't hang up till he say "ok i forwarded it".

If router is in your controll, don't call ISP, take a look at this videos (they are nice tytorials)

and do it yourself ;)

here you can check if others can see you on that port

http://www.yougetsignal.com/tools/open-ports/

or

http://www.canyouseeme.org/

Edited by bogQ

TCP server and client - Learning about TCP servers and clients connection
Au3 oIrrlicht - Irrlicht project
Au3impact - Another 3D DLL game engine for autoit. (3impact 3Drad related)



460px-Thief-4-temp-banner.jpg
There are those that believe that the perfect heist lies in the preparation.
Some say that it’s all in the timing, seizing the right opportunity. Others even say it’s the ability to leave no trace behind, be a ghost.

 

Share this post


Link to post
Share on other sites
coffeeturtle

Thank you bogQ for this terrific tutorial!

The time you put into this is very much appreciated!

Great work!

Would you be okay with taking this to the next level?

-Adding GUI instead of Msgbox

-Having one script work as both server and client to create an Instant Messaging program

-Adding Contact List of all machines that are connected (listed as IP address or @ComputerName

This would probably be a big project, but written in the style that you have already done would be of great help.

I figure it never hurts to ask and certainly you've given us a great base of understanding to work from.

Thank you again!

Edited by coffeeturtle

Share this post


Link to post
Share on other sites
ludocus

Thnx for your helpful topic.

I have a question though. I would like to use TCP to connect (lets say) 2 computers but in different houses. So different IP adresses.

So the connection should go via WAN, not LAN. How can I make my real IP adress, like 77.170.181.98, serve as a server?

Thnx in advance :)

Share this post


Link to post
Share on other sites
bogQ

Guess someone with more experience with internet communication should answer this one but if im wrong they can still correct me or at least confirm if this is correct :P (as i stated before "i can't help a lot, i never tried coz my ports are forwarded")

So "if im not wrong",

one computer is server, his public IP address is 77.170.181.98

one client, we dont need to know his ip adress, but he need to know server public IP address.

Server TCPListen on his addapter IP adress (so that isnt 77.170.181.98, we need adapter address) (port must be forwarded on router)

Client connect to 77.170.181.98 server public IP adress.

So client try to connect to server

*Client send msg to server ip adress

77.170.181.98:8080 -> "are you there?"

*router recive msg for port 8080 and forward it on that port to your listening device 10.0.252.30

->77.170.181.98:8080 -> 10.0.252.30:8080 -> "are you there?"

Edited by bogQ

TCP server and client - Learning about TCP servers and clients connection
Au3 oIrrlicht - Irrlicht project
Au3impact - Another 3D DLL game engine for autoit. (3impact 3Drad related)



460px-Thief-4-temp-banner.jpg
There are those that believe that the perfect heist lies in the preparation.
Some say that it’s all in the timing, seizing the right opportunity. Others even say it’s the ability to leave no trace behind, be a ghost.

 

Share this post


Link to post
Share on other sites
caleb41610

that's basically how it works. i just use TCPListen("0.0.0.0", 8080) and it automatically listens on your default adaptor. no need to even get lan/wan ip on the server.

on the router, forward 8080 to your local server ip.

on the client, connect to the servers WAN.

or use @IPAddress1 instead of 0.0.0.0. either should work fine.

edit: 0.0.0.0 makes it listen on "any available" ip, so it will work with multiple adaptoras, i believe.

Edited by caleb41610

Share this post


Link to post
Share on other sites
ludocus

Ok thnx for the info.

It is still not working though.

This is a picture of the port I forwarded (is this right??):

Posted Image

And then this is the server script line:

$Listen = TCPListen('192.168.2.8', 8080, 500)

And this is the client's line:

$Socket = TCPConnect('77.170.181.98', 8080)

This should be good, right?

It is not working though, please tell me if I did something wrong

Share this post


Link to post
Share on other sites
bogQ

as i told my isp did not forwarded my ports so i can test

in theory it should work

i posted 2 links in first post

http://www.yougetsignal.com/tools/open-ports/

or

http://www.canyouseeme.org/

so can they see you on port 8080?

another question is , is your port 8080 free? no other program is using him?

did you try on some other port? is your wirewall off?


TCP server and client - Learning about TCP servers and clients connection
Au3 oIrrlicht - Irrlicht project
Au3impact - Another 3D DLL game engine for autoit. (3impact 3Drad related)



460px-Thief-4-temp-banner.jpg
There are those that believe that the perfect heist lies in the preparation.
Some say that it’s all in the timing, seizing the right opportunity. Others even say it’s the ability to leave no trace behind, be a ghost.

 

Share this post


Link to post
Share on other sites
caleb41610

Ok thnx for the info.

It is still not working though.

This is a picture of the port I forwarded (is this right??):

Posted Image

And then this is the server script line:

$Listen = TCPListen('192.168.2.8', 8080, 500)

And this is the client's line:

$Socket = TCPConnect('77.170.181.98', 8080)

This should be good, right?

It is not working though, please tell me if I did something wrong

You must also open the port in your firewall.

Share this post


Link to post
Share on other sites
ludocus

Ok still now working.

I changed to port 8081 (just to be sure the port wasn't used by anything else)

I also opened 8081 on my windows firewall.

I checked with the port website, and it says the port is open (if the server is running):

As you see, the port is open.Posted Image

Now when I run the client, I get the following error:

!--> TCPConnect error number ( 10060 ), look in the help file (on MSDN link) on func TCPConnect to get more info about this error.

BTW: I'm running the client and server on the same pc (thus same ip adress). Could this be causing my trouble?

I've been trying to make this work for ages now, please help me out once more!

Thnx :)

Edited by ludocus

Share this post


Link to post
Share on other sites
bogQ

yes it can, if you ever had experience with wamp server or wow servers for example, you couldn't never connect to the site or gameserver on internet connection ip adress from the computer that is running that server, but you can go to that adress if you go to some proxy with browser site and connecting with it with no problems.

so

WSAETIMEDOUT 10060

Connection timed out.

A connection attempt failed because the connected party did not properly respond after a period of time, or the established connection failed because the connected host has failed to respond.

can be cuz of that, and in addition it can be cuz of port problem, or something third.

So please do try to connect with 2 different computers with 2 different internet connection on them. (call some friend and tell him to run your client)

if it dont work try some other random ports eg 3724 8129 28960...

and try to lisssen on ip 0.0.0.0

for some other info to "can it realy work" you can look at this post to see that TCP do work on internet if everything is configyred in the correct way and your isp isnt playing with you.

http://en.wikipedia.org/wiki/List_of_TCP_and_UDP_port_numbers

Edited by bogQ

TCP server and client - Learning about TCP servers and clients connection
Au3 oIrrlicht - Irrlicht project
Au3impact - Another 3D DLL game engine for autoit. (3impact 3Drad related)



460px-Thief-4-temp-banner.jpg
There are those that believe that the perfect heist lies in the preparation.
Some say that it’s all in the timing, seizing the right opportunity. Others even say it’s the ability to leave no trace behind, be a ghost.

 

Share this post


Link to post
Share on other sites
bogQ

just a quick note from my side, after i convince my isp to geave me some free ports for next month, i tryed to connect over internet on port 8080 today to test do it work or not

tcp lisen on 0.0.0.0 and after that on @IPAddress1 with port 8080, connected to server public IP and message recived, sended, and recived with no problems

port 80 did have probably cos my wamp did use it when i try it on.

So it do work with no problem if everything is configured correctly.


TCP server and client - Learning about TCP servers and clients connection
Au3 oIrrlicht - Irrlicht project
Au3impact - Another 3D DLL game engine for autoit. (3impact 3Drad related)



460px-Thief-4-temp-banner.jpg
There are those that believe that the perfect heist lies in the preparation.
Some say that it’s all in the timing, seizing the right opportunity. Others even say it’s the ability to leave no trace behind, be a ghost.

 

Share this post


Link to post
Share on other sites
TurionAltec

Excellent. This was exactly what I needed.

I have another script I'm building that's generating text output. I wanted to be able to call a separate script running a TCP server, pass the data over Stdin, and have the Server duplicate out whatever data is was sent to whatever clients are connected at the time, and manage listening, opening, and closing Sockets.

I have something like this compiled to .EXE, though if running as an .au3 file, it will prompt you with an Input box for data to send. Connect and disconnect as many Telnet (Putty, Teraterm) sessions as you want and they start getting output.

 

#include <Array.au3>
#include <WinAPIProc.au3>
;Ref https://www.autoitscript.com/forum/topic/144987-learning-about-tcp-servers-and-clients-connection/
    $ParentPID = _WinAPI_GetParentProcess()
TCPStartup()
OnAutoItExitRegister("OnAutoItExit")
    Dim $Socket_Data[1]
$Socket_Data[0] = 0
$Listen = TCPListen("0.0.0.0", 1018, 500)
If @error Then
    ConsoleWrite('!--> TCPListen error number ( ' & @error & ' ), look in the help file (on MSDN link) on func TCPListen to get more info about this error.' & @CRLF)
    Exit
EndIf
    While ProcessExists($ParentPID)
    If @Compiled=0 Then
        $TCPdata=InputBox("TCPServer","Type Data to send")
    Else
        $TCPData = ConsoleRead()
        EndIf
        If StringLen($TCPData) > 0 Then
        For $x = $Socket_Data[0] To 1 Step -1
            TCPSend($Socket_Data[$x], $TCPData)
            If @error Then
                _CloseConnection()
            EndIf
        Next
    EndIf
    _Accept()
    Sleep(500)
WEnd
    Func _Accept()
    Local $Accept = TCPAccept($Listen)
    If $Accept <> -1 Then
        _ArrayAdd($Socket_Data, $Accept)
        $Socket_Data[0] += 1
    EndIf
EndFunc   ;==>_Accept
    Func _CloseConnection()
    _ArrayDelete($Socket_Data, $x)
    $Socket_Data[0] -= 1
EndFunc   ;==>_CloseConnection
    Func OnAutoItExit()
    TCPShutdown() ; Close the TCP service.
EndFunc   ;==>OnAutoItExit

    I then use this script to establish the link to Stdin:
     

    #include <AutoItConstants.au3>
$iPID = Run("tcpserver.exe", "", @SW_HIDE, $STDIN_CHILD)
While 1
    $Data=InputBox("Input","data")
    StdinWrite($iPID,$Data)
Wend
   

 

Edited by TurionAltec
  • Like 1

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

  • Similar Content

    • Spartan117
      By Spartan117
      Hi everyone,
      I wonder if there is a cloud service that can offer a physical server so that AutoIT can turn as it is in a physical machine.
      Thank you
    • ripdad
      By ripdad
      I have had several people ask for this, so I decided to work the algorithm for it and this is the result.
      What is it?
      A Gateway Proxy Sends and Receives Data Unmodified.
      https://en.wikipedia.org/wiki/Proxy_server
      What is it used for?
      You can use it as a gateway, relay or router between two known static IP addresses.
      More information is in the header of the script.
      Download: LocalGatewayProxy.au3
      You will need WSA_NBTCP.au3 from here:
      https://www.autoitscript.com/forum/topic/191954-wsa_nbtcp-v100-udf/
      If you have any questions or problems, let me know.
       
    • ripdad
      By ripdad

      This script is based on algorithm code from EnrMa.
      Updated: January 22, 2018
      Made improvements. Changes are in the script header.
      Known Issues: AutoIt x64 does not work properly with this script.
      Download: LocalProxyServer_v1.00.zip
       
    • ripdad
      By ripdad
      WSA_NBTCP.au3  (Windows Sockets API - Non-Blocking Transmission Control Protocol)
      Version: 1.00
      Type: UDF
      This is an accumulation of WSA code from many sources and modified to suit myself.
      These functions have been thoroughly tested using a Local Proxy Server, which
      is about the most strenuous test you can use.
      Includes my rendition of how a TCPRecv Timeout should work. Also includes a
      timewait/timeout using Select for TCP Send, which works great for that function.
      You will need a loop to use _WSA_TCPRecv(). An example will be forthcoming in a second post.
      Functions:
      #CURRENT_USER_FUNCTIONS
      _WSA_Cleanup
      _WSA_FormatMessage
      _WSA_GetLastError
      _WSA_TCPAccept
      _WSA_TCPCloseSocket
      _WSA_TCPConnect
      _WSA_TCPListen
      _WSA_TCPRecv
      _WSA_TCPSend

      #INTERNAL_FUNCTIONS
      __TimeoutManager
      __TimeoutReset

      #EXTRA_FUNCTIONS
      _WSA_GetAddrInfo
      _WSA_GetHostByAddr
      _WSAAsyncGetHostByName
      _WSAAsyncGetHostByName_Callback
      _WSA_GetNameInfo
       
      Requirements:
      - AutoIt Versions: 3.3.8.1 thru 3.3.15.0 (32Bit only).
      - TCPStartup() at beginning of script on startup.
      - TCPShutDown() and _WSA_Cleanup() on exit.
      Download UDF: WSA_NBTCP.au3
       
    • tarretarretarre
      By tarretarretarre
      AutoIt-SocketIo
      Yep yep, this is pretty much an attempt to port the existing project's concept https://socket.io/ to AutoIt's Codebase. So i will not go in to so much detail.
      This is how the communication is done http://i.imgur.com/0mMfsBD.png Each client is isolated to the server http://i.imgur.com/rVO2LFb.png Features
      Easy API VarType Translation (Example: If the server sends an int, the client will receive an int and vice versa) Fully featured examples Data encryption (Using Autoit's UDF Crypt.au3) Limitations / Drawbacks
      It is not possible to Broadcast/Emit objects Only 1D-arrays are allowed to be Broadcasted/Emitted (2D arrays will probably never be supported) Changelog
      Version 1.5.0 (This update DOES NOT break scripts)
      Added AutoIt like docs under Docs\. The docs are generated so its a 1 to 1 reflection of the UDF headers. Print of documentation Added a production ready server example. Added a new method: _Io_DevDebug which will display useful information when debugging. Added a new method: _Io_SetMaxRecvPackageSize which defaults to whatever _Io_setRecvPackageSize is set to. Added a new method: _Io_setOnPrefix which defaults to _On_ Added a new default client & server event called flood. Flood occurs when exceeding the $__g_io_nMaxPacketSize. $__g_io_nMaxPacketSize is set by _Io_SetMaxRecvPackageSize Fixed the 16 parameter limit when sending data with _Io_Emit, _Io_Broadcast, _Io_BroadcastToAll and _Io_BroadcastToRoom. This works on the same premise that AutoIt's Call Does Fixed a TRUNCATION problem when receiving packages which could cause crashes! Fixed a programming error which caused $__g_ionPacketSize to reset to default 4096 if _Io_Connect or _Io_listenwere called after _Io_setRecvPackageSize Fixed _Io_setEventPreScript and _Io_setEventPostScript They didnt work. Lol. Changed how events are fired so the client cannot crash the server by sending the wrong number of parameters (This also allows for optional parameters on callbacks) Changed _Io_On. The second parameter $fCallback can now be set to null. Doing this, the function assumes that the callback is: _On_<eventName>.  
      Changelog History
      Api methods
      Please see the docs provided
      Default events
      Server events
      connection Client events
      banned Server and Client events
      disconnect flood View source on github
       
      Autoit-Socket-IO-1.0.0.zip (OLD!)
      Autoit-Socket-IO-1.1.0.zip (OLD)
      Autoit-Socket-IO-1.3.0.zip (OLD)
      Autoit-Socket-IO-1.4.0.zip (OLD)
      Autoit-Socket-IO-1.5.0.zip (NEWEST 2017-12-20)
×