Jump to content
ripdad

WSA_NBTCP v1.00 UDF

Recommended Posts

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

 

Edited by ripdad

"The mediocre teacher tells. The Good teacher explains. The superior teacher demonstrates. The great teacher inspires." -William Arthur Ward

Share this post


Link to post
Share on other sites

These are benchmark scripts I made for the above UDF.

They also serve as examples on how to use it. I will probably post more examples eventually.

Run _WSA_TCPRecv_Benchmark.au3 first and then _WSA_TCPSend_Benchmark.au3 second.

send.png

recv.png

 

Download:

_WSA_TCPRecv_Benchmark.au3

_WSA_TCPSend_Benchmark.au3

 

Edited by ripdad

"The mediocre teacher tells. The Good teacher explains. The superior teacher demonstrates. The great teacher inspires." -William Arthur Ward

Share this post


Link to post
Share on other sites

Below are some things you can read to understand the behavior of TCP communications:

The "TCPSend Speed" shows how long it takes for each chunk transmission to reach
its destination through "Acknowledgements". Results depend on how fast the remote
is receiving the data. Speed is affected by network conditions, loop speed and
buffer size at the receiving end. The lower the buffer size, the more time it
will take to transfer data.

With the "Simulate Server Delay" button, you can test the reactions on the
TCPRecv GUI. Push the button and observe a time-out in progress. If the button
is pushed again (ie: resuming), it will cancel the time-out. Otherwise, it will
continue until it times out with a WSA 10060 error (WSAETIMEDOUT).

More about TCP can be found here:

http://www.inetdaemon.com/tutorials/internet/tcp/flow_control.shtml
http://www.inetdaemon.com/tutorials/internet/tcp/data_transfer.shtml
http://www.inetdaemon.com/tutorials/internet/tcp/3-way_handshake.shtml

More about blocking and non-blocking sockets; pros and cons:

https://www.scottklement.com/rpg/socktut/nonblocking.html
http://www.developerfusion.com/article/28/introduction-to-tcpip/8/

 

Edited by ripdad

"The mediocre teacher tells. The Good teacher explains. The superior teacher demonstrates. The great teacher inspires." -William Arthur Ward

Share this post


Link to post
Share on other sites

This is a slimmed down example for a receiver. It has no GUI, counters or timers -- and is only one shot.

A summary will be displayed when finished.

#include 'WSA_NBTCP.au3'

Opt('MustDeclareVars', 1)
Opt('TrayAutoPause', 0)
;
;=== IP And Port Settings ===
Local $sLocalIP = '127.0.0.1'
Local $nLocalPort = '61000'
;============================
;
Local $sTitle = 'AutoIt v' & @AutoItVersion & ' - WSA_TCPRecv'
TraySetToolTip($sTitle)
;
If TCPStartup() <> 1 Then
    MsgBox(8208, $sTitle, 'Error: TCPStartup' & @TAB, 5)
    Exit
EndIf
;
OnAutoItExitRegister('ExitApp')
;
Local $nListenSocket = _WSA_TCPListen($sLocalIP, $nLocalPort); listen for traffic
Local $nError = @error
If $nError Then
    MsgBox(8208, $sTitle, $nError & ' Error: TCPListen could not bind socket' & @TAB, 5)
    Exit
EndIf
;
Local $AcceptSocket, $ActiveSocket, $nActive, $nBytesReceived, $nBytesTotal, $sData, $sRecv
;
While 1
    Do
        $AcceptSocket = _WSA_TCPAccept($nListenSocket); listen for an active socket
        If $AcceptSocket > 0 Then
            $ActiveSocket = $AcceptSocket; copy accept socket to active socket
            $nBytesTotal = 0
            $nActive = 1; set active
            $sData = ''
        EndIf
    Until $nActive > 0
    ;
    $sRecv = _WSA_TCPRecv($ActiveSocket, 65536, 0, 10); <- 10 second timeout
    $nError = @error
    $nBytesReceived = @extended
    ;
    If $nError Then; an error occurred
        _WSA_TCPCloseSocket($ActiveSocket)
        $nActive = 0; set inactive
        ExitLoop
    ElseIf $nBytesReceived Then; data received
        $nBytesTotal += $nBytesReceived
        $sData &= $sRecv; <-- gather and do something with received data
    EndIf
WEnd
;
MsgBox(8256, $sTitle & ' - Session Summary', 'Disconnected: ' & $nError & @CRLF & 'Total Bytes Received: ' & $nBytesTotal & '  (' & Round(($nBytesTotal / 1024) / 1000) & ' MB)' & @CRLF & @CRLF & 'Sample of Data Received: ' & StringLeft($sData, 200))
Exit
;
Func ExitApp()
    _WSA_TCPCloseSocket($nListenSocket)
    _WSA_Cleanup()
    TCPShutdown()
    Exit
EndFunc
;

 


"The mediocre teacher tells. The Good teacher explains. The superior teacher demonstrates. The great teacher inspires." -William Arthur Ward

Share this post


Link to post
Share on other sites

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2020-03-26 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2020-03-21 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
Task Scheduler (2019-12-03 - Version 1.5.1.0) - Download - General Help & Support - Wiki

Tutorials:
ADO - Wiki, WebDriver - Wiki

 

Share this post


Link to post
Share on other sites

This is a simple send and shutdown example...

#include 'WSA_NBTCP.au3'
;
Opt('MustDeclareVars', 1)
Opt('TrayAutoPause', 0)
OnAutoItExitRegister('ExitApp')
;
;=== IP And Port Settings ===
Local $sLocalIP = '127.0.0.1'
Local $nLocalPort = '61000'
;============================
;
Local $String = 'xxxxxxxxxxxxxxxxxxxxxxxxx This Is A Test String xxxxxxxxxxxxxxxxxxxxxxxxx'
;
Local $nBytes = TCP_Send($sLocalIP, $nLocalPort, $String)
Local $nError = @error
MsgBox(0, 'Results', 'Error: ' & $nError & @CRLF & 'BytesSent: ' & $nBytes)
Exit
;
;
Func TCP_Send($sIPAdrress, $nPort, $sData)
    If TCPStartup() <> 1 Then Return SetError(-1)
    ;
    Local $nSocket = _WSA_TCPConnect($sIPAdrress, $nPort)
    If @error Then Return SetError(-2)
    ;
    _WSA_TCPSend($nSocket, $sData)
    Local $nError = @error
    Local $nBytesSent = @extended
    ;
    _WSA_TCPCloseSocket($nSocket)
    ;
    If $nError Then
        Return SetError($nError, 0, 0)
    Else
        Return SetError(0, 0, $nBytesSent)
    EndIf
EndFunc
;
Func ExitApp()
    _WSA_Cleanup()
    TCPShutdown()
    Exit
EndFunc
;

 

Let me know if you need a particular example that wasn't shown.

 


"The mediocre teacher tells. The Good teacher explains. The superior teacher demonstrates. The great teacher inspires." -William Arthur Ward

Share this post


Link to post
Share on other sites

Thanks for these functions ripdad

whats for the $hGUI parameter in _WSAAsyncGetHostByName()
didn't get to test it out yet, can it be defaulted to $hGUI = AutoItWinGetTitle() and it should work like that ?

with what i have tested using: https://github.com/jesobreira/TCPClient-UDF
all works with improved speed all over
except _WSA_TCPRecv gives me the WSAEFAULT error -->  https://social.msdn.microsoft.com/Forums/windowsdesktop/en-US/0fe72216-f1ad-4b15-aea1-3d75604cebdc/wsarecv-return-10014-wsaefault?forum=wsk
didn't yet get to try and figure a way to fix this .. 

One general question :
concerning this HTTPS-everywhere plugin from HTTPS://www.eff.org/https-everywhere/development
  
anyone by chance knows how to deal with the incoming data that appears to be encrypted coming from a browser's Socket that happens to be using this plugin ?

Thanks again

Edited by Deye

Share this post


Link to post
Share on other sites
2 hours ago, Deye said:

whats for the $hGUI parameter in _WSAAsyncGetHostByName()

Use the ID (handle) of your GUI.

2 hours ago, Deye said:

except _WSA_TCPRecv gives me the WSAEFAULT error

With all the testing I've done, I have never had this problem or error. Mind if I ask what kind of data is being received? I would need some way to reproduce it also.

Quote

From MSDN:

WSAEFAULT 10014 Bad address.

The system detected an invalid pointer address in attempting to use a
pointer argument of a call. This error occurs if an application passes
an invalid pointer value, or if the length of the buffer is too small.
For instance, if the length of an argument, which is a sockaddr structure,
is smaller than the sizeof(sockaddr).

What version of Autoit are you using? And, 32 or 64 Bit?

 

2 hours ago, Deye said:

One general question :
concerning this HTTPS-everywhere plugin

Not sure if I understand what you want to do here. The browser initiates the connection to the remote site. Once connected, the data is passed back and forth encrypted in such a way that only the browser and the remote site can decipher -- unless you are using it some other way.

 

Edited by ripdad

"The mediocre teacher tells. The Good teacher explains. The superior teacher demonstrates. The great teacher inspires." -William Arthur Ward

Share this post


Link to post
Share on other sites

I took a look at the code in the TCPClient-UDF...

Func __TCPClient_Recv()
    For $i = 1 To $__TCPClient_Sockets[0]
        Dim $sData
        If Not $__TCPClient_Sockets[$i] Then ContinueLoop
        $recv = TCPRecv($__TCPClient_Sockets[$i], 1000000); <---
        If @error Then
            __TCPClient_KillConnection($i)
            ContinueLoop
        EndIf
        If $recv Then
            $sData = $recv
            Do
                $recv = TCPRecv($__TCPClient_Sockets[$i], 1000000); <---
                If @error Then
                    ConsoleWrite('log 2')
                    __TCPClient_KillConnection($i)
                    ContinueLoop (2)
                EndIf
                $sData &= $recv
            Until $recv = ""
            If $_TCPClient_AutoTrim Then
                $sData = StringStripWS($sData, 1 + 2)
            EndIf
            If $_TCPClient_DebugMode Then __TCPClient_Log("Client " & _TCPClient_SocketToIP($__TCPClient_Sockets[$i]) & " sent " & StringLeft($sData, 255))
            Call($_TCPClient_OnReceiveCallback, $__TCPClient_Sockets[$i], _TCPClient_SocketToIP($__TCPClient_Sockets[$i]), $sData, $__TCPClient_Pars[$i])
        EndIf
    Next
EndFunc ;==>__TCPClient_Recv

One million bytes seems to be a little much for a buffer size, in my opinion. Maybe if you were to change it to something more reasonable, it might make a difference. Perhaps 100000 or lower?

 


"The mediocre teacher tells. The Good teacher explains. The superior teacher demonstrates. The great teacher inspires." -William Arthur Ward

Share this post


Link to post
Share on other sites

I used it like so using the Latest version of autoit with its beta in 32 bit

$recv = _WSA_TCPRecv($__TCPClient_Sockets[$i], 65536, 0, 10)

as to the  HTTPS-everywhere plugin i'm not completely sure what is exactly happening ,didn't do any special modding 

may be it has to do with how i'm sending the text back to the browser where  that plugin is active ,without it being active every thing seems normal

will need to test it again to find out more ..

Edited by Deye

Share this post


Link to post
Share on other sites

I rewrote the function (Not Tested) to take advantage of the BytesReceived feature and a few other things.

Wrote a few comments along with it. Hopefully, it will improve the situation.

Func __TCPClient_Recv()
    Local $nBytesReceived, $nError, $sData, $recv
    ;
    For $i = 1 To $__TCPClient_Sockets[0]
        If Not $__TCPClient_Sockets[$i] Then
            ContinueLoop
        EndIf
        ;
        $recv = _WSA_TCPRecv($__TCPClient_Sockets[$i], 65536, 1, 10); socket, buffer, binary, timeout
        $nError = @error
        $nBytesReceived = @extended
        ;
        If $nError Then
            ConsoleWrite('__TCPClient_Recv Error-1: ' & $nError & @CRLF)
            __TCPClient_KillConnection($i)
            ContinueLoop
        ElseIf $nBytesReceived Then
            $sData = BinaryToString($recv); It's best to use binary for encrypted data returning from receive.
        Else
            ContinueLoop
        EndIf
        ;
        Do
            $recv = _WSA_TCPRecv($__TCPClient_Sockets[$i], 65536, 1, 10); socket, buffer, binary, timeout
            $nError = @error
            $nBytesReceived = @extended
            ;
            If $nError Then
                ConsoleWrite('__TCPClient_Recv Error-2: ' & $nError & @CRLF)
                __TCPClient_KillConnection($i)
                ContinueLoop 2
            ElseIf $nBytesReceived Then
                $sData &= BinaryToString($recv)
            EndIf
        Until $nBytesReceived = 0
        ;
        ; *** This is probably not a good idea, since headers generally have at least one @CRLF at the end, which is definitely a monkey wrench if removed.
        ;If $_TCPClient_AutoTrim Then
            ;$sData = StringStripWS($sData, 1 + 2)
        ;EndIf
        ;
        If $_TCPClient_DebugMode Then __TCPClient_Log("Client " & _TCPClient_SocketToIP($__TCPClient_Sockets[$i]) & " sent " & StringLeft($sData, 255))
        ;
        ; *** At first glance, this looks like Async Sockets, but I can't find any code supporting that notion.
        ; *** FYI, The UDF has not been tested with a mix of Async Sockets and Non-Blocking Sockets. So, I have no idea what the effects are.
        Call($_TCPClient_OnReceiveCallback, $__TCPClient_Sockets[$i], _TCPClient_SocketToIP($__TCPClient_Sockets[$i]), $sData, $__TCPClient_Pars[$i])
    Next
EndFunc

 


"The mediocre teacher tells. The Good teacher explains. The superior teacher demonstrates. The great teacher inspires." -William Arthur Ward

Share this post


Link to post
Share on other sites

Deye,

On your WSAEFAULT Error, what socket protocol are you using?

I have only allowed for IPv4.

If you're using IPv6 or something else, I would have to consider making some modifications.

---

Does anyone else have this issue or something like it?

 


"The mediocre teacher tells. The Good teacher explains. The superior teacher demonstrates. The great teacher inspires." -William Arthur Ward

Share this post


Link to post
Share on other sites
5 hours ago, ripdad said:

On your WSAEFAULT Error

I cannot reproduce it anymore since I'm now using it with what you had rewritten for TCPClient.au3 + this mod:

especially:      If $nError <> 10054 And $nError 

If $nError <> 10054 And $nError Then
            ConsoleWrite('__TCPClient_Recv Error-1: ' & $nError & @CRLF)
            MsgBox(0,'Recieve Error -1 ' ,$nError )
            __TCPClient_KillConnection($i)
            ContinueLoop
        ElseIf $nBytesReceived Then
            $sData = BinaryToString($recv) ; It's best to use binary for encrypted data returning from receive.
        Else
            ContinueLoop
        EndIf

So, Used with the builtin TCPRecv on the server side the _WSA_TCPRecv responds well on the client's side
Testing it on with TCPServer.au3 (server's side) doesn't consistently work for me though ..

i think my IPv6  is completely turned off ..

Share this post


Link to post
Share on other sites
25 minutes ago, Deye said:

i think my IPv6  is completely turned off ..

Okay, I was racking my brain trying to figure out what was causing that error. Never seen it before.

Whats with bypassing the 10054 error? I assume you are having problems with it?

--edit--

If you can post your TCPServer.au3, I will attempt to find the problem.

 

Edited by ripdad

"The mediocre teacher tells. The Good teacher explains. The superior teacher demonstrates. The great teacher inspires." -William Arthur Ward

Share this post


Link to post
Share on other sites

I needed to bypass the 10054 error incase the server was supposed to just send something and right after close the connection
So in case some valid data was already received then entering the loop with the 10054 error was needed to get bypassed

https://github.com/jesobreira/TCPServerUDF

Edited by Deye

Share this post


Link to post
Share on other sites
2 hours ago, Deye said:

I needed to bypass the 10054 error incase the server was supposed to just send something and right after close the connection

_WSA_TCPRecv() returns blank on any error.

10053 and 10054 errors are caused when either the client or server disconnects.

So, it's doubtful you will get any more data from that socket. It's best to close it.

 

Edited by ripdad

"The mediocre teacher tells. The Good teacher explains. The superior teacher demonstrates. The great teacher inspires." -William Arthur Ward

Share this post


Link to post
Share on other sites

Deye,

I looked at the TCPServer UDF.

It appears __TCPServer_Recv() is written about the same way as was the client.

What issues are you having with it?

 


"The mediocre teacher tells. The Good teacher explains. The superior teacher demonstrates. The great teacher inspires." -William Arthur Ward

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

    • By argumentum
      I can TCP/IP in AutoIt, hence, make a HTTP deamon. Now, how can I HTTPS to use SSL !??
      Well, Apache has this "mod_proxy.so" module that can let me have SSL and what not is in Apache.
      All that is needed is to tell Apache what I wanna do by editing httpd.conf .
      # Implements a proxy/gateway for Apache. # 1. Open /Applications/XAMPP/etc/httpd.conf # 2. Enable the following Modules by removing the # at the front of the line. # - LoadModule rewrite_module modules/mod_rewrite.so # - LoadModule proxy_module modules/mod_proxy.so # - LoadModule proxy_http_module modules/mod_proxy_http.so # # 3. Copy and Paste below to the bottom of httpd.conf # <IfModule mod_proxy.c> ProxyRequests On <Proxy *> Order deny,allow Allow from all </Proxy> ProxyVia Off ProxyPreserveHost Off ProxyPass /home/ http://127.0.0.1:84/home/ ProxyPassReverse /home/ http://127.0.0.1:84/home/ SetEnv proxy-nokeepalive 1 # ..since we are not using "keep-alive", we are using "close" </IfModule> ...et voila  
      I'm using XAMPP ( https://www.apachefriends.org/download.html )
      and this is my solution to avoid coding in PHP, as I feel more comfortable coding in AutoIt.
      A "muli-thread or concurrency" can be done by forking the socket ( https://www.autoitscript.com/forum/topic/199177-fork-udf-ish/ )
      but responses are under 20 ms., so I feel fine with a single thread.
      I modified an example ( attached below ), so can try out the concept.
      PS: I am not an Apache guru. I just discovered this and it opens a world of possibilities. In my case, I'm thinking of an API to query SQLite 
      PS2: I'm not gonna make Poll but do click like if you do  
       
      201673-json-http-post-serverlistener.au3
    • By tarretarretarre
      Autoit-Socket-IO
      Introduction
      Autoit-Socket-IO is a event driven TCP/IP wrapper heavily inspired from Socket.IO with focus on user friendliness and long term sustainability.
      I created this UDF because I was fascinated how Socket.IO made a such scary task "reliable and secure networking" so simple for the developer. So this was my main motivation.
      I constantly want to make this UDF faster and better, so if you have any suggestions. Do not hesitate to make requests!
      Features
      Flexiable and easy to understand API Above avarage documentation "Fully featured" examples Security in form of data encryption and middleware-support Limitations
      Speed. Because I want this UDF to be as flexible and simple as possible. Sometimes speed is sacrificed, but that does not mean i don't try to . It is not possible to emit objects mainly because autoit does not support serialization. Only 1D-arrays can be emitted (2D arrays will probably never be supported) Success story
      Since December 2017-now I have used version 1.5.0 in an production environment for 40+ clients with great success, the only downtime is planned windows updates and power outages.
      Getting started
      Download the script from AutoIt or pull it from the official github repo git@github.com:tarreislam/Autoit-Socket-IO.git and checkout the tag 2.0.0 The documentation is located at Docs\index.html Take a look in the examples/ folder Changelog
      Version 2.0.0 (This update break scripts. Please consult the upgrade.md for guidance)
      All global internal variables has been renamed. Added a bunch of new API methods: _Io_RegisterMiddleware, _Io_whoAmI, _Io_IsClient, _Io_IsServer, _Io_getAllByProperty and _Io_getFirstByProperty and some more. Read more about these in the documentation. _Io_socketGetProperty now has a setter method called _Io_socketSetProperty which can be used to set custom properties. _Io_socketGetProperty now has a third parameter "default" which is used when a property is not found Removed _Io_setEventPostScript and _Io_setEventPretScript in favor of _Io_RegisterMiddleware Improved documentation (It still needs some love though) Improved the verbosity of _Io_DevDebug  
      Newest version (2019-09-29!)
      Autoit-Socket-IO-2.0.0.zip
      Older versions (Not supported anymore)
      Autoit-Socket-IO-1.0.0.zip Autoit-Socket-IO-1.1.0.zip Autoit-Socket-IO-1.3.0.zip Autoit-Socket-IO-1.4.0.zip Autoit-Socket-IO-1.5.0.zip
    • By AoRaToS
      I started working on this program in the summer of 2008 then I stopped cause I faced some problems I couldn't overcome back then. Now that I've practiced more and have become a better scripter/programmer I'm releasing the program to the public to get some opinions. I know it's not a new concept but it's the first program I started besides some small stuff I did just for practice! I won't post the source code yet because it's still under construction, although I'm sure I've posted early stages of the code with bugs in the past in some topic...
      What I wanted was a simple, small, serverless program that would work without installation cause I wanted it for where I work, so I ended up with this!
      I have attached some images of various versions, also visit the forum thread.
      The package includes s!mpL3 LAN Messenger and the full change log.
      Current version 2.9.9.1! [04/07/2019]
      Check the Change Log below!
       
       
      http://www.autoitscript.com/forum/index.php?showtopic=88782
       
       
       
      Read the license before using this software.
       
    • By HansHenrik
      ----------
      edit: this is probably in the wrong place, can a moderator move it to wherever it belongs?
      ----------

      is there any way to completely disable TCPTimeout and make TCPRecv() wait indefinitely? maybe setting it to 0 or -1 or something? 
      the docs doesn't seem to mention any way to disable it

      - the underlying C code would set SO_RCVTIMEO to 0 , aka

      DWORD timeout=0;
      setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout));
    • 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.
       
×
×
  • Create New...