Jump to content

UDPRecv fails?


dexto
 Share

Recommended Posts

Hello guys,

Idea is to monitor UDP broadcast in the same subnet (private LAN) on port 67. In other words monitor DHCP requests.

Here is the code that logically should work (moded UDPRecv example):

;;This is the UDP Server
;;Start this first

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

; Bind to a SOCKET
;==============================================
$socket = UDPBind("0.0.0.0", 67)
If @error <> 0 Then
    ConsoleWrite(1)
    Exit
EndIf

While 1
    ConsoleWrite(UDPRecv($socket, 255, 1))
WEnd

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

Packets are there, as confirmed by packet monitor (pic attached (first 3 packets)).

What am I missing?

post-39800-12654189907844_thumb.png

Edited by dexto
Link to comment
Share on other sites

Hello guys,

Idea is to monitor UDP broadcast in the same subnet (private LAN) on port 67. In other words monitor DHCP requests.

Here is the code that logically should work (moded UDPRecv example):

;;This is the UDP Server
;;Start this first

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

; Bind to a SOCKET
;==============================================
$socket = UDPBind("0.0.0.0", 67)
If @error <> 0 Then
    ConsoleWrite(1)
    Exit
EndIf

While 1
    ConsoleWrite(UDPRecv($socket, 255, 1))
WEnd

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

Packets are there, as confirmed by packet monitor (pic attached (first 3 packets)).

What am I missing?

What version of AutoIt are you using?
Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

Using 3.3.4.0 outcome is the same.

Tried binding to adapter separately, using binary and auto mode - no difference.

Sorry, I see you're missing UDPOpen which will need the flag at 1.

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

It reminds me of a fix that had to do with UDP broadcast _sent_ to 255.255.255.255, but I may be off my head. No I still have a bit of brain: this was ticket #938, closed with version 3.3.1.1 so it's pretty old story. It may be related to your issue, or not at all.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

Sorry, I see you're missing UDPOpen which will need the flag at 1.

Like so?

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

; Bind to a SOCKET
;==============================================
#cs
    $socket = UDPBind("0.0.0.0", 67)
    If @error <> 0 Then
    ConsoleWrite(@error)
    Exit
    EndIf
#ce

$socket = UDPOpen("0.0.0.0", 67, 1)
If @error <> 0 Then
    ConsoleWrite(@error)
    Exit
EndIf

While 1
    $out = UDPRecv($socket, 255, 0)
    If $out <> "" Then
        ConsoleWrite($out & @CRLF)
    EndIf
WEnd

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

I do not know if its right to use $socket from UDPOpen with UDPRecv

but its acting the same.

Link to comment
Share on other sites

Ok, I'm paying proper attention now!

I tried between 2 PCs like this:

PC 1 had this code

;;Start the other script first

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

; Open a "SOCKET"
;==============================================

$ThisPCIP = "192.168.1.104";running this script1
$socket2 = UDPBind($ThisPCIP, 65532)

If @error <> 0 Then
 ConsoleWrite("error opening $socket2 , @error = " & @error & @CRLF)
 Exit
 EndIf

$ThatPCIP = "255.255.255.255";
 $socket = UDPOpen($ThatPCIP, 65532,1)
If @error <> 0 Then
 ConsoleWrite("error opening $socket" & @CRLF)
 Exit
 EndIf


$n=0
While 1
    $data = UDPRecv($socket2, 50)
    If $data <> "" Then
    MsgBox(0, "UDP DATA", $data, 1)
    EndIf
    sleep(100)
    $n = $n + 1
    $status = UDPSend($socket, "Message #" & $n)
    If $status = 0 then
    MsgBox(0, "ERROR", "Error while sending UDP message: " & @error)
    Exit
    EndIf
WEnd

Func OnAutoItExit()
    UDPCloseSocket($socket)
    UDPCloseSocket($socket2)
    UDPShutdown()
EndFunc

PC2 had this code which I started running first.

;Script2

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

; Bind to a SOCKET
;==============================================
HotKeySet("{ESC}","xit")
$ThisPCIP = "192.168.1.17";running script2
$socket = UDPBind($ThisPCIP, 65532)
If @error <> 0 Then Exit

While 1
    $data = UDPRecv($socket, 50)
    If $data <> "" Then
    MsgBox(0, "UDP DATA", $data, 1)
        ;UDPSend($socket2,"Got number " & $data)
    EndIf
    sleep(100)
WEnd

Func OnAutoItExit()
    UDPCloseSocket($socket)
    UDPShutdown()
EndFunc

func xit()
    Exit
EndFunc

Both PCs received the messages as they should.

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

The only difference comes when computer that sends it and has no IP (Requesting DHCP services) (as in Source IP is 0.0.0.0 target IP is 255.255.255.255) its not showing anything.

This might not be important at all because for the most part IP has to be present. In fact DHCP request is the only case where source computer broadcasting UDP from 0.0.0.0 IP.

Link to comment
Share on other sites

  • 1 year later...

Hi All,

I hope it's not too frowned up on when old topics such as this are revived with answers months/years later. It's just that, people (like me) may end up hitting them through web-searches, and I though the following addition to this thread may be useful.

The only difference comes when computer that sends it and has no IP (Requesting DHCP services) (as in Source IP is 0.0.0.0 target IP is 255.255.255.255) its not showing anything.

This might not be important at all because for the most part IP has to be present. In fact DHCP request is the only case where source computer broadcasting UDP from 0.0.0.0 IP.

Dexto,

You may have figured this out long time ago, but in case you haven't; another difference is the packet size. DHCP packets have usually been >300 bytes in the networks I've monitored. When you put that 255 limit, UDPRecv() just ignores them. It doesn't do "partial" loads. You need to make sure that the "maxlen" parameter you use is bigger than your expected packet size.

Ozgur

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