Jump to content
Sign in to follow this  
E1M1

Why my UDP client never receive answer from server?

Recommended Posts

E1M1

Hello.

I am trying to communicate with udp server. Problem is that my script never receives answer from server. At first I thought packet must be invalid but after playing around with Wireshark I figured that server actually responds but my script never shows that. Anyone got suggestions/ideas about what might cause this.

; Start The UDP Services
;==============================================
UDPStartup()

; Register the cleanup function.
OnAutoItExitRegister("Cleanup")

; Open a "SOCKET"
;==============================================
Local $socket = UDPOpen("1.2.3.4", 1234)
If @error <> 0 Then Exit
Local $n = 0
UDPSend($socket,binary("0xAC152D0A"))
While 1
    Local $data = UDPRecv($socket, 4096)
    If @error Then Exit
    If $data <> "" Then
        ConsoleWrite(Binary($data)&@CRLF)
    EndIf

WEnd

Func Cleanup()
    UDPCloseSocket($socket)
    UDPShutdown()
EndFunc   ;==>Cleanup

edited

Share this post


Link to post
Share on other sites
kylomas

E1M1,

I added some error checking to your code and changed the ip addr to 127.0.0.1. If you run this code you will recieve the error message for UPDRecv. I don't know shit about networking so have no idea why the message is being issued.

#include <winapi.au3>

; Start The UDP Services
;==============================================
UDPStartup()

; Register the cleanup function.
OnAutoItExitRegister("Cleanup")

; Open a "SOCKET"
;==============================================
Local $socket = UDPOpen("127.0.0.1", 12345)

if $socket[0] = 0 then     ConsoleWrite('UDPOPEN failed' & @LF)
;If @error <> 0 Then Exit
Local $n = 0, $ret
$ret = UDPSend($socket,binary("0xAC152D0A"))

if $ret <= 0 then
    switch @error
        case -1,-2,-3
            ConsoleWrite('Invalid socket array' & @LF)
        case 1
            ConsoleWrite('IP ADDR incorrect' & @LF)
        case 2
            ConsoleWrite('Port is incorrect' & @LF)
        case Else
            ConsoleWrite('WSA ERROR MSG = ' & _winapi_getlasterrormessage() & @LF)
    endswitch
    cleanup()
    exit
Else
    ConsoleWrite($ret & ' bytes sent' & @LF)
endif

While 1
    Local $data = UDPRecv($socket, 4096)
    ;If @error Then Exit
    If $data = "" Then
        ConsoleWrite('Receive error' & @LF)
        switch @error
            case -1,-2,-3
                ConsoleWrite('Invalid socket array' & @LF)
            case Else
                ConsoleWrite('WSA ERROR MSG = ' &  _winapi_getlasterrormessage() & @LF)
        endswitch
        cleanup()
        exit
    Else
        ConsoleWrite($data & @LF)
    EndIf

WEnd

Func Cleanup()
    UDPCloseSocket($socket)
    UDPShutdown()
EndFunc   ;==>Cleanup

Hope this helps,

kylomas

edit: change winapiex to winapi

Edited by kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites
E1M1

Thanks.

well it said WSA ERROR MSG = The operation completed successfully.


edited

Share this post


Link to post
Share on other sites
guinness

My current setup doesn't allow me to test the scripts, but looks OK for me.


UDF List:

 
_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_ArrayFilter/_ArrayReduce_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 22/04/2018

Share this post


Link to post
Share on other sites
bogQ

for kulomas code i realy dont think it shud call Cleanup() exit as soon as its = ''

cos there inst any point in loop then.

E1M1 identify what func is returning on "If @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.

 

Share this post


Link to post
Share on other sites
kylomas

E1M1,

When I run this it returns

WSA ERROR MSG = An existing connection was forcibly closed by the remote host.

@bogQ - not sure what you are talking about...

kylomas


Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites
kylomas

Thanks.

well it said WSA ERROR MSG = The operation completed successfully.

And no data to console?


Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites
bogQ

yea i edited sry but botom line is if he is getting on your error that The operation completed successfully. than there is something wrong with me cos i newer seen error that confirms that everything is ok :P , so once more sry for that

but still exiting on first loop if = '' is wrong

edit:

so i did lot of editing mess its not my day aperantly for writing, thinking one thing writing something else

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.

 

Share this post


Link to post
Share on other sites
kylomas

so i did lot of editing mess its not my day aperantly for writing, thinking one thing writing something else

No problem, we've all been there...relax and have a beer

kylomas


Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites
kylomas

E1M1,

Got it...needed to bind to the receiving socket (whatever that means to you network types). See comments in code.

#include <winapi.au3>

; Start The UDP Services
;==============================================
UDPStartup()

; Register the cleanup function.
OnAutoItExitRegister("Cleanup")

; Open a "SOCKET"
;==============================================
Local $socket   =   UDPOpen("127.0.0.1", 12345)
local $recv     =   udpbind('127.0.0.1',12345)    ; <-----   added this and it seems to work

if $socket[0] = 0 then  ConsoleWrite('UDPOPEN failed' & @LF)
;If @error <> 0 Then Exit
Local $n = 0, $ret

$ret = UDPSend($socket,binary("test send data"))

if $ret <= 0 then
    switch @error
        case -1,-2,-3
            ConsoleWrite('Invalid socket array' & @LF)
        case 1
            ConsoleWrite('IP ADDR incorrect' & @LF)
        case 2
            ConsoleWrite('Port is incorrect' & @LF)
        case Else
            ConsoleWrite('WSA ERROR MSG = ' & _winapi_getlasterrormessage() & @LF)
    endswitch
    cleanup()
    exit
Else
    ConsoleWrite($ret & ' bytes sent' & @LF)
endif

While 1
    Local $data = UDPRecv($recv, 4096)  ; <------   changed this to receive socket variable
    ;If @error Then Exit
    If $data = "" Then
        switch @error
            case -1,-2,-3
                ConsoleWrite('Invalid socket array' & @LF)
            case Else
                ConsoleWrite('WSA ERROR MSG = ' &  _winapi_getlasterrormessage() & @LF)
        endswitch
        cleanup()
        exit
    Else
        ConsoleWrite($data & @LF)
    EndIf

WEnd

Func Cleanup()
    UDPCloseSocket($socket)
    UDPShutdown()
EndFunc   ;==>Cleanup

kylomas


Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites
kylomas

@bogq - You are right in post #5. After I started playing with something actually useful I came up with the following. I'm not sure what UPD would be used for but here is one way to get input from a gui and write it out to the console. I did this just to try to understand how the functions work.

; *** Start added by AutoIt3Wrapper ***
#include <GUIConstantsEx.au3>
; *** End added by AutoIt3Wrapper ***

#include <winapi.au3>
#AutoIt3Wrapper_Add_Constants=n
Local $ret, $ip = '127.0.0.1', $port = 12345

$ret = UDPStartup()
if $ret = 0 then
    ConsoleWrite('UPD Startup Error...exiting' & @LF)
    Exit
endif

OnAutoItExitRegister("Cleanup")

local $socket   = UDPOpen($ip, $port)
local $recv     = udpbind($ip, $port)

if $socket[0] = 0 or $recv[0] = 0 then
    ConsoleWrite('UDPOPEN or UPDBIND failed' & @LF)
    Exit
endif

local $gui010   =   guicreate('UPD Testing GUI')
                    guictrlcreatelabel('Enter Data To Send',50,20,100,20)
local $inp010   =   guictrlcreateinput('',50,50,200,20)
local $dmy010   =   guictrlcreatedummy()
                    guisetstate()

while 1
    switch guigetmsg()
        case $gui_event_close
            exit
        case $inp010, $dmy010
            _senddata(guictrlread($inp010))
    endswitch
wend

func _senddata($data)

    $ret = UDPSend($socket,$data)

    if $ret <= 0 then
        switch @error
            case -1,-2,-3
                ConsoleWrite('Invalid socket array' & @LF)
            case 1
                ConsoleWrite('IP ADDR incorrect' & @LF)
            case 2
                ConsoleWrite('Port is incorrect' & @LF)
        endswitch
    Else
        ConsoleWrite($ret & ' bytes sent' & @LF)
    endif

    $data = UDPRecv($recv, 4096)
    If $data = "" Then
        switch @error
            case -1,-2,-3
                ConsoleWrite('Invalid socket array' & @LF)
        endswitch
    Else
        ConsoleWrite('Received = ' & $data & @LF)
    EndIf

endfunc

Func Cleanup()
    UDPCloseSocket($socket)
    UDPShutdown()
EndFunc   ;==>Cleanup

kylomas

Edited by kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites
kylomas

Added accelerator key for {ENTER} and some checking for blank transmissions.

; *** Start added by AutoIt3Wrapper ***
#include <GUIConstantsEx.au3>
; *** End added by AutoIt3Wrapper ***

#include <winapi.au3>
#AutoIt3Wrapper_Add_Constants=n
Local $ret, $ip = '127.0.0.1', $port = 12345

$ret = UDPStartup()
if $ret = 0 then
    ConsoleWrite('UPD Startup Error...exiting' & @LF)
    Exit
endif

OnAutoItExitRegister("Cleanup")

local $socket   = UDPOpen($ip, $port)
local $recv     = udpbind($ip, $port)

if $socket[0] = 0 or $recv[0] = 0 then
    ConsoleWrite('UDPOPEN or UPDBIND failed' & @LF)
    Exit
endif

local $gui010   =   guicreate('UPD Testing GUI')
                    guictrlcreatelabel('Enter Data To Send',50,20,100,20)
local $inp010   =   guictrlcreateinput('',50,50,200,20)
local $dmy010   =   guictrlcreatedummy()
                    guisetstate()

Dim $accelKeys[1][2] = [["{ENTER}", $dmy010]]
GUISetAccelerators($accelKeys)

while 1
    switch guigetmsg()
        case $gui_event_close
            exit
        case $inp010, $dmy010
            _senddata(guictrlread($inp010))
    endswitch
wend

func _senddata($data)

    $ret = UDPSend($socket,$data)

    if $ret <= 0 then
        switch @error
            case -1,-2,-3
                ConsoleWrite('Invalid socket array' & @LF)
            case 1
                ConsoleWrite('IP ADDR incorrect' & @LF)
            case 2
                ConsoleWrite('Port is incorrect' & @LF)
        endswitch
    Else
        ConsoleWrite($ret & ' bytes sent' & @LF)
    endif

    $data = UDPRecv($recv, 4096)
    If $data = "" Then
        switch @error
            case -1,-2,-3
                ConsoleWrite('Invalid socket array' & @LF)
        endswitch
        ConsoleWrite('No data sent...blankinput' & @LF)
    Else
        if stringlen(stringstripws($data,3)) = 0 then
            ConsoleWrite('All data blank'& @LF)
        else
            ConsoleWrite('Received = ' & $data & @LF)
        endif
    EndIf

endfunc

Func Cleanup()
    UDPCloseSocket($socket)
    UDPShutdown()
EndFunc   ;==>Cleanup

kylomas


Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites
E1M1

Thank you kylomas.

It looks little funny that you need to bind other socket to recv reply. I haven't used UDP before but with TCP I have always used same socket for bidirectional communication. Just imagine if server you are trying to communicate with is on localhost and has boud 0.0.0.0. Then client's bind would fail.

And no data to console?

no.

But thank you for helping me alot.


edited

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  

×