Jump to content

WSA_NBTCP v1.00 UDF


ripdad
 Share

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

Link to comment
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

Link to comment
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

Link to comment
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

Link to comment
Share on other sites

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
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

Link to comment
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
Link to comment
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

Link to comment
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

Link to comment
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
Link to comment
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

Link to comment
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

Link to comment
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 ..

Link to comment
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

Link to comment
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

Link to comment
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

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

×
×
  • Create New...