Jump to content

TCPRecv and v3.3.12.0


Recommended Posts

@ripdad

I noticed you are using == for comparison, that's much prettier but it

Tests if two strings are equal. Case sensitive. The left and right values are converted to strings if they are not strings already. This operator should only be used if string comparisons need to be case sensitive.

Edited by FaridAgl
Link to comment
Share on other sites

FaridAgl,

Case sensitivity is not the issue here. It's testing if the return is exact.

It's making sure that blank doesn't equal zero. I have seen too many times

when a return was something different than the expected result, using =

instead of ==. Which the latter is an Exact comparison. And That is what

I use it for, testing for an exact number. I don't use it for anything else.

Take a look at the "recv" call, toward the bottom of this page at MSDN:

http://msdn.microsoft.com/en-us/library/windows/desktop/ms740121(v=vs.85).aspx

"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

It's making sure that blank doesn't equal zero.

You are always comparing a number returned from DllCall() with a number, why should you convert two numbers to strings before comparing them? They are numbers, not stings, there is no blank in a numerical variable.

Case sensitivity is not the issue here.

Of course it is, just if you care about performance.

When comparing two numbers, they will be compared by a single CMP in machine code level, but when you convert them to strings, they have to be compared character by character.

You are also overusing parenthesis, writing more codes to achieve same thing?

ElseIf ($hSock[0] == -1) Or ($hSock[0] == 4294967295) Then

Take a look at the "recv" call, toward the bottom of this page at MSDN:

:) Edited by FaridAgl
Link to comment
Share on other sites

jpm,

There's a quirk about WSAGetLastError, that I have seen several times a day.

It's like the quirk in "recv" -- a blank return every so often.

This is a little different. It will return an error of 5.

When using "FormatMessage", it returns "access denied".

The problem is, there is no such error code in WSA at MSDN:

http://msdn.microsoft.com/en-us/library/windows/desktop/ms740668(v=vs.85).aspx

It seems to happen when I clear an edit control via OnEvent. Really strange.

Maybe that is the same quirk for "recv" also. I'll run some more tests and

find out.

In any case, this should fix that problem:

Func _WSA_GetLastError()
    Local $aResult = DllCall($hWS2_32, 'int', 'WSAGetLastError')
    If @error Then
        Return SetError(-1, 0, -1); dll error
    ElseIf $aResult[0] > 5 Then
        Return SetError(0, 0, $aResult[0])
    Else
        Return SetError(0, 0, 0)
    EndIf
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

Here's a reproducer for Post #86.

Local Const $hWS2_32 = DllOpen(@WindowsDir & '\system32\ws2_32.dll')
Local $ID_GUI = GUICreate('Test', 430, 330, -1, -1, Default, 0x00000008)
Local $ID_EDT = GUICtrlCreateEdit('', 10, 5, 400, 260)
Local $ID_CLR = GUICtrlCreateButton('Clear Window', 320, 280, 82, 25)
GUICtrlSetOnEvent($ID_CLR, '_Clear')
GUISetOnEvent(-3, '_Exit')
Opt('GUIOnEventMode', 1)
GUISetState(@SW_SHOW, $ID_GUI)
;
Do
    Sleep(100)
    _WSA_GetLastError()
Until @error
;
Func _Exit()
    GUIDelete($ID_GUI)
    DllClose($hWS2_32)
    Exit
EndFunc
;
Func _Clear()
    GUICtrlSetData($ID_EDT, '')
EndFunc
;
Func _WSA_GetLastError()
    Local $aResult = DllCall($hWS2_32, 'int', 'WSAGetLastError')
    If @error Then
        Return SetError(-1, 0, -1); dll error
    Else
        GUICtrlSetData($ID_EDT, $aResult[0] & @CRLF, 1)
    EndIf
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

I get the same 5 on CLEAR but no idea why ...

Perhaps a WSAStartup() is needed to have reliable result

Edited : adding TCPStartup does not solve the issue

Edited by jpm
Link to comment
Share on other sites

Evidently, this issue has existed, since at least v3.3.6.0.

I don't know what one has to do with the other. Very strange indeed.

Until the cause is found, the code in Post #86 will suffice for now.

--edit--

Here's another reproducer using GUIGetMsg(), instead of OnEvent.

Global Const $hWS2_32 = DllOpen(@WindowsDir & '\system32\ws2_32.dll')
Global $ID_GUI = GUICreate('Test', 430, 330, -1, -1, Default, 0x00000008)
Global $ID_EDT = GUICtrlCreateEdit('', 10, 5, 400, 260)
Global $ID_BTN = GUICtrlCreateButton('WSAGetLastError', 300, 280, 110, 25)
GUISetState(@SW_SHOW, $ID_GUI)
;
While 1
    Switch GUIGetmsg()
        Case -3
            GUIDelete($ID_GUI)
            DllClose($hWS2_32)
            Exit
        Case $ID_BTN
            GUICtrlSetData($ID_EDT, _WSA_GetLastError() & @CRLF, 1)
    EndSwitch
WEnd
;
Func _WSA_GetLastError()
    Local $aResult = DllCall($hWS2_32, 'int', 'WSAGetLastError')
    If @error Then
        Return SetError(-1, 0, -1); dll error
    Else
        Return SetError(0, 0, $aResult[0])
    EndIf
EndFunc
;
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

note for #86 changing focus from the button to edit (click with mouse) get it back to normal behavior. Setting focus to anything else result in = 5 return from dllcall array (this include setting focus to some other win on desktop).

question, for what are you trying to get last error?

Edited by bogQ

TCP server and client - Learning about TCP servers and clients connection
Au3 oIrrlicht - Irrlicht project
Au3impact - Another 3D DLL game engine for autoit. (3impact 3Drad related)



460px-Thief-4-temp-banner.jpg
There are those that believe that the perfect heist lies in the preparation.
Some say that it’s all in the timing, seizing the right opportunity. Others even say it’s the ability to leave no trace behind, be a ghost.

 
Link to comment
Share on other sites

bogQ,

Good catch. That still doesn't explain what that

has to do with the call to WSAGetLastError.

Here's the reproducer in Post #89, with the exception

of $GUI_Focus in _Clear(). No error (5) occurs when the

button is pressed.

Local Const $hWS2_32 = DllOpen(@WindowsDir & '\system32\ws2_32.dll')
Local $ID_GUI = GUICreate('Test', 430, 330, -1, -1, Default, 0x00000008)
Local $ID_EDT = GUICtrlCreateEdit('', 10, 5, 400, 260)
Local $ID_CLR = GUICtrlCreateButton('Clear Window', 320, 280, 82, 25)
GUICtrlSetOnEvent($ID_CLR, '_Clear')
GUISetOnEvent(-3, '_Exit')
Opt('GUIOnEventMode', 1)
GUISetState(@SW_SHOW, $ID_GUI)
;
Do
    Sleep(100)
    GUICtrlSetData($ID_EDT, _WSA_GetLastError() & @CRLF, 1)
Until @error
;
Func _Exit()
    GUIDelete($ID_GUI)
    DllClose($hWS2_32)
    Exit
EndFunc
;
Func _Clear()
    GUICtrlSetState($ID_EDT, 256); $GUI_Focus
    GUICtrlSetData($ID_EDT, '')
EndFunc
;
Func _WSA_GetLastError()
    Local $aResult = DllCall($hWS2_32, 'int', 'WSAGetLastError')
    If @error Then
        Return SetError(-1, 0, -1); dll error
    Else
        Return SetError(0, 0, $aResult[0])
    EndIf
EndFunc
;

bogQ said: "question, for what are you trying to get last error?"

It was just a reproducer for the issue discussed, for which WSA does

not have errors lower than 6. It was returning 5 (access denied).

"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 noticed that on replicator script 4 of lines are with '5' and then one line is with '0', im getting 5 5 5 5 0 5 5 5 5 0 combination all the time (pretty strange if you ask me :P)

maybe problem can be in next 2 things if i understood correctly when reading:

on this site comment is that WSAGetLastError can return all errors as defined in winerror.h (http://www.carrona.org/winerror.html)

http://www.gamedev.net/topic/399027-wsagetlasterror-returns-5-what-does-this-mean-solved/

so maybe somehow it's error return is overwritten (your getting error from some other place that is displayed inside return value of WSAGetLastError).

http://msdn.microsoft.com/en-us/library/windows/desktop/ms741580(v=vs.85).aspx

if you can, try to replicate behavior with WSAGetLastError under normal circumstances, but if you ask me i whud think we're looking for problem that isn't there :).

Edited by bogQ

TCP server and client - Learning about TCP servers and clients connection
Au3 oIrrlicht - Irrlicht project
Au3impact - Another 3D DLL game engine for autoit. (3impact 3Drad related)



460px-Thief-4-temp-banner.jpg
There are those that believe that the perfect heist lies in the preparation.
Some say that it’s all in the timing, seizing the right opportunity. Others even say it’s the ability to leave no trace behind, be a ghost.

 
Link to comment
Share on other sites

The problem is there when the button clears the edit box.

And that somehow causes WSAGetLastError to return 5.

As far as I know, this is the only way it occurs.

Otherwise, WSAGetLastError doesn't do that.

It's no big deal -- it's an easy fix.

"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'm looking for alternatives that make the fastest script, so I've been following this topic and adapting to my program everything that is posted here.

I saw that the code below is considerably faster:

Local $tCharBuff = DllStructCreate("char sData[1]")
Local $pCharBuff = DllStructGetPtr($tCharBuff)
Local $aRet

; Waits for the response from the remote computer if there is already a bitmap saved in cache...
$aRet = DllCall($hWs2_32, "int", "recv", "int", $iConnection, "ptr", $pCharBuff, "int", 1, "int", 0)
If $aRet[0] < 1 Then
    ConsoleWrite("Error!" & @CRLF)
    Exit
EndIf

If $tCharBuff.sData = "C" Then
    ConsoleWrite("Continue..." & @CRLF)
EndIf
Than this one that uses TCPRecv() function in loop:

Local $aRet, $sRecv

; Waits for the response from the remote computer if there is already a bitmap saved in cache...
Do
    $sRecv = TCPRecv($iConnection, 1)
    If @error > 0 Then
        ConsoleWrite("Error!" & @CRLF)
        Exit
    EndIf
Until $sRecv = "C" Or $sRecv = "S" ;C = Continue, S = Skips

If $sRecv = "C" Then
    ConsoleWrite("Continue..." & @CRLF)
EndIf

A good alternative would be TCPRecv function to accept an external buffer and a pointer, something like:

TCPRecv($iConnection, $pBuffer, $iLen)
or better:

TCPRecv($iConnection, $pBuffer, $iLen, $iBlockMode)

JS

Edited by JScript

http://forum.autoitbrasil.com/ (AutoIt v3 Brazil!!!)

Somewhere Out ThereJames Ingram

somewh10.png

dropbo10.pngDownload Dropbox - Simplify your life!
Your virtual HD wherever you go, anywhere!

Link to comment
Share on other sites

  • 7 months later...

Hi,

Because of the disconnection detection problem of the "new" TCPRecv() function, I searched for a solution and find this topic.

I'm using this UDF _WSA_TCPRecv-20140703.au3 and the _WSA_TCPRecv() function. It detects perfectly the disconnections of clients but I have a problem of performance.

The processor of the server who host the server app is always running at 25% of usage (same on 2 differents servers). If I use the TCPRecv function it works fine (with no disconnection detection).

Any idea ?

Thanks.

Autoit 3.3.12.0

 

EDIT :

some news : I commented the code block "[set blocking mode]" in the _WSA_TCPRecv function and the CPU charge is now OK on one server. The problem persists on the second server (both of them are running Windows Server 2003).

Edited by tatane
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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...