Jump to content

Recommended Posts

Posted

I have this code, where I listen using one socket and send a packet using another one, both in same program.

Receive doesn't seem to work, I checked with Wireshark that send happens.

Am I missing something?
 

#include <AutoItConstants.au3>
#include <GUIConstantsEx.au3>

Opt("GUIOnEventMode", 1)

; Create GUI
Local $w = 400
Local $h = 200
Local $b = 3
GUICreate("Local Discovery", $w, $h)
Local $idListView = GUICtrlCreateListView("UUID|IP|", $b, $b, $w - 2 * $b, $h - 2 * $b)
;GUICtrlSetTip(-1, '#Region LIST VIEW')

; Start the UDP service.
UDPStartup()
OnAutoItExitRegister("OnAutoItExit")

; Start listener
Local $msg = ""
Local $recv = UDPBind("239.255.250.250", 9130)

; Query widgets
Local $send = UDPOpen("239.255.250.250", 9130, $UDP_OPEN_BROADCAST )
UDPSend($send, "Hello")
UDPCloseSocket($send)

GUISetOnEvent($GUI_EVENT_CLOSE, "CLOSEButton")
GUISetState(@SW_SHOW)

While 1
   $msg = UDPRecv($recv, 512)
   If $msg <> "" Then
      ConsoleWrite($msg)
      GUICtrlCreateListViewItem("A|One", $idListView)
   EndIf
   Sleep(100)
WEnd

Func CLOSEButton()
   Exit
EndFunc

Func OnAutoItExit()
   ; Close the UDP service.
   UDPShutdown()
EndFunc

 

Posted (edited)

I understand the OP that he already uses two different ports. 

Edit: But the code shows that he doesn't. 

Edited by water

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Posted

Hi @dmob, I started with different ports.

In the code that I shared, even if I disable the send part, I do not seem to receive any UDP packets sent by other senders.

I added send part with same port just to confirm that multicast sending is working - and try to do a loop-back.

Posted

This is interesting.

I changed the port to my local IP, and on running the code, Windows asked me for some network related permission for AutoIt. After approval, I could receive packet sent to my local IP (over UDP).

So, all these time when I was using multicast IP, AutoIt never tried binding to it. Even after changing IP back to multicast IP, it is not binding.

Does this give any hint?

Posted

According to a comment here: http://stackoverflow.com/questions/20289986/upnp-envelopes-from-scratch-not-really-working
 

Quote

as far as I remember multicast code generally, you normally first bind to your local IP address and then add the multicast address afterwards

I thought AutoIt does that for me, but possibly it doesn't.

So, I need to bind to my local port and join the multicast group.

But, I do not see nay function to join to a multicast group?

Posted

Oh, I looked more and found that UDPBind() returns error 10049 - invalid address.

That makes me think, AutoIt doesn't have support for listening for UDP multicast packets???

Posted (edited)

I made a UDP multicast to retrieve the IP of the server so that I can use it with TCP without knowing the ip of the server.

Server:

UDPStartup()

$socket_send = UDPOpen(StringRegExpReplace(@IPAddress1, "(\d+)$", "") & "255", 7777)
$socket_recieve = UDPBind(@IPAddress1, 6666)

While 1
    $data = UDPRecv($socket_recieve, 50)
    If $data <> "" Then
        If $data = "Find_server" Then
            $status = UDPSend($socket_send, @IPAddress1)
        EndIf
    EndIf
    sleep(100)
WEnd

UDPCloseSocket($socket_send)
UDPCloseSocket($socket_recieve)
UDPShutdown()

Client:

Local $ip = _get_client_ip("Find_server")
If Not @error Then
    ConsoleWrite($ip & @CRLF)
EndIf


Func _get_client_ip($sendstring)
    Local $data, $count, $count_total
    UDPStartup()

    Local $socket_send = UDPOpen(StringRegExpReplace(@IPAddress1, "(\d+)$", "") & "255", 6666)
    Local $socket_recieve = UDPBind(@IPAddress1, 7777)

    If UDPSend($socket_send, $sendstring) = 0 then
        SetError(1);
    Else
        While 1
            If $count_total > 20 Then
                SetError(2)
                ExitLoop
            EndIf
            If $count > 5 Then
                If UDPSend($socket_send, $sendstring) = 0 then
                    SetError(1);
                    ExitLoop
                EndIf
                $count = 0
            EndIf
            $data = UDPRecv($socket_recieve, 50)
            If $data <> "" Then
                ExitLoop
            EndIf
            $count = $count + 1
            $count_total = $count_total + 1
            Sleep(100)
        WEnd
    EndIf

    UDPCloseSocket($socket_send)
    UDPCloseSocket($socket_recieve)
    UDPShutdown()

    Return $data
EndFunc

Mabey you can get what you need from this code?

Edited by nend
Posted

Hi @nend, your example listens to the broadcast group of your subnet (IP x.y.z.255, if I'm correct).

For the devices I'm working with, they emit a discovery beacon on fixed multicast IP address and port.

I've a working code for node.js that binds to port 9131 and then joins to multicast group:

// Create UDP socket
const dgram = require('dgram');
var sock = dgram.createSocket('udp4');

// Bind to port
sock.bind(9131, function() {
    // Join multicast group
    sock.addMembership('239.255.250.250');
});

// Wait for messages
sock.on('message', function(msg, rinfo) {
    msg = msg.toString();
    console.log(msg);
});

I'm able to receive messages with this code.

I hope something similar is available in AutoIt that I can make in to a simple GUI.

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
  • Recently Browsing   0 members

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