Sign in to follow this  
Followers 0
thomaatje

WinAPI Send()

3 posts in this topic

#1 ·  Posted (edited)

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!

Client:

#include <WinAPI.au3>

Global $Ws2_32 = "Ws2_32.dll"

Global Const $MSG_OOB = 0x0001

TCPStartup()
$Sock = TCPConnect("127.0.0.1", 9100)

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

TCPCloseSocket($Sock)


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())
EndFunc

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)
GUISetState()

GUISetOnEvent($GUI_EVENT_CLOSE, "SpecialEvents")

TCPStartup()
$MainSocket = TCPListen("127.0.0.1", 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)
            EndIf
        WEnd
        Sleep(5)
    WEnd
    Sleep(5)
WEnd

Func SpecialEvents()
    Select
        Case @GUI_CTRLID = $GUI_EVENT_CLOSE
           Exit
    EndSelect
EndFunc

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.


♡♡♡

.

eMyvnE

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  
Followers 0