Jump to content
ripdad

WSA_NBTCP v1.00 UDF

Recommended Posts

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

 

Edited by ripdad
  • Like 2

"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
ripdad

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
ripdad

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
ripdad

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
water
  • Like 1

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-12-03 - Version 1.4.11.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-10-31 - Version 1.3.4.1) - Download - General Help & Support - Example Scripts - 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
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites
ripdad

Thanks water.


"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
ripdad

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
Deye

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
ripdad
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
ripdad

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
Deye

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
ripdad

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
ripdad

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
Deye
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
ripdad
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
Deye

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
x_bennY

turn off your firewall

Share this post


Link to post
Share on other sites
ripdad
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
ripdad
1 hour ago, x_bennY said:

turn off your firewall

For what reason?

 


"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
ripdad

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

    • 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
       
    • 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)
    • timmalos
      By timmalos
      Hello all.
      In case this is interesting few of you, I share my AMCP 2.1 protocol UDF in AutoIT. This protocol is used by CasparCG server, which is a Windows and Linux software used to play out professional graphics, audio and video to multiple outputs as a layerbased real-time compositor. It has been in 24/7 broadcast production since 2006. It's free and opensource.
      The UDF I share allows communication between an AutoIt based client and the CasparCG, based on following documentation : http://casparcg.com/wiki/CasparCG_2.1_AMCP_Protocol
      If you want more details on CasparCG :  official WebSite or have a look to this video
      I'm currently building a full Client based on AutoIt, with many features like drag-and-drop layers, but sadly I can't share it right now, might come later. Don't hesitate to ask questions if you have any or need a basic example.
      The only requirement for this UDF is the other Event-driven TCP UDF by Kip
       
       

      AMCP_shared.au3
      TCP.au3
    • kingjacob90
      By kingjacob90
      Hi
      I am trying to send a file over TCP from a TCP client to server. If I run the server and client on the same computer the file is send fine, but if the server is on one computer on the network and the client on another the file is sent in what looks like more than one packet. In other words the server receives 4 msg from the client.
      Why is this, how do I make it send in one go, or what is a way around it?
      Side Note: TCP server has a max of 999999999
×