Jump to content
Sign in to follow this  

WinAPI Send()

Recommended Posts


Hello all,

Recently i've been trying to send some data over a TCP connection using the Send() function from Ws2_32.dll instead of the Autoit build-in TCPSend() function. The reason I want to do this this is because I want execute the Send() function in a different thread.

The code below works perfectly for sending 1 packet. But if I try to send more than 16 packets the connection is closed. I found out that if I set the flag to 0, the function sends more than 16 packets but the data is in some kind of binary form instead of a string. Since the reciever can't decode that binary, I need to send it as a string. And the only way it seems to sends a string is to change the flag to $MSG_OOB. But with that flag it won't send more than 16 packets...

I hope someone here knows a solution for this problem.

Thanks in advanced!


#include <WinAPI.au3>

Global $Ws2_32 = "Ws2_32.dll"

Global Const $MSG_OOB = 0x0001

$Sock = TCPConnect("", 9100)

For $i = 1 To 20
    _TCPSend($Sock, "Data" & $i, $MSG_OOB)


Func _TCPSend($Sock, $sData, $iFlag)
    Local $DataBuffer = DllStructCreate("char[" & StringLen($sData)+2 & "]")
    DllStructSetData($DataBuffer, 1, $sData)

    $Ret = DllCall($Ws2_32, "int", "send", "ptr", $Sock, "ptr", DllStructGetPtr($DataBuffer), "int", DllStructGetSize($DataBuffer)-1, "int", $iFlag)
    If $Ret[0] = -1 Then Exit MsgBox(16,"Error",_WinAPI_GetLastErrorMessage())

Server. I just use it to test the client. It's not the server i really want to send the data to.

#include <GUIConstants.au3>
#include <Constants.au3>
#include <StaticConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

Global $Main
Global $DefMsg = ""
Global $MainSocket 
Global $Connection = -1
Global $Count = 0

Opt("GUIOnEventMode", 1)

$Main = GUICreate("Server", 290, 50, 100, 512, -1, $WS_EX_TOPMOST)
$Display = GUICtrlCreateInput($DefMsg, 5, 5, 280, 40)
    GUICtrlSetFont(-1, 22)
    GUICtrlSetState(-1, $GUI_DISABLE)

GUISetOnEvent($GUI_EVENT_CLOSE, "SpecialEvents")

$MainSocket = TCPListen("", 9100)

While 1
    While $Connection = -1
        $Connection = TCPAccept($MainSocket)
        While $Connection >= 0
            $Recv = TCPRecv($Connection, 4098)
            If $Recv <> "" Then
                GUICtrlSetData($Display, $Recv)
                $Count += 1
                WinSetTitle($Main, "", "Server - " & $Count)

Func SpecialEvents()

Edit: used wrong flag in the client code

Edited by thomaatje

Share this post

Link to post
Share on other sites

You make buffer to send double-null terminated. Then you specify that the size of buffer is size-1.

All that confuses receiver and it just returns binary.

Since there is parameter that specifies data length, your buffer should be in size of data (no null at the end).

This is even easier, since you send stings only:

DllCall($Ws2_32, "int", "send", "ptr", $Sock, "str", $sData, "int", StringLen($sData), "int", $iFlag)

Cool idea btw.




Share this post

Link to post
Share on other sites

Thx very much!

Works perfect!

Learned a lot from your inline assembly examples but it still confuses me sometimes :(

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  


Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.