Receiving UDP multicast packets?

Just starting out? Need help? Post your questions and find answers here.
Khorus
User
User
Posts: 25
Joined: Sat Nov 13, 2010 11:22 pm

Receiving UDP multicast packets?

Post by Khorus »

Hello all,

I'm writing a small program that communicates with MIDI over LAN devices. I can send UDP packets no problems... But it's the receiving part that is not working. The packets are "connection-less" and are broadcasted for everyone to "capture" or receive if you will...

Here's my code, looks legit:

CreateNetworkServer(0, 21929, #PB_Network_UDP)

...
If NetworkServerEvent() = #PB_NetworkEvent_Data
ReceiveNetworkData(EventClient(), *DataBuffer, 2048)
Debug "Received UDP"
EndIf
...

CloseNetworkServer(0)

My "NetworkServerEvent()" never gets triggered by my multicast UDP packets. Now, how does one "listen" to the network and read those precious packets? Thanks in advance,

Marc Girard aka Khorus
jassing
Addict
Addict
Posts: 1885
Joined: Wed Feb 17, 2010 12:00 am

Re: Receiving UDP multicast packets?

Post by jassing »

Are you sure that your pc is getting the data passed any firewalls?

I just ran your code; slightly modified, and it worked as expected...

Code: Select all

If InitNetwork() And CreateNetworkServer(0, 21929, #PB_Network_UDP)
  *DataBuffer = AllocateMemory(2048)
  Repeat 
    If NetworkServerEvent() = #PB_NetworkEvent_Data 
      ReceiveNetworkData(EventClient(), *DataBuffer, 2048)
      Debug "Received UDP: "+PeekS(*DataBuffer,30)
    EndIf
    Delay(10)
  ForEver
EndIf
CloseNetworkServer(0)
Image
Khorus
User
User
Posts: 25
Joined: Sat Nov 13, 2010 11:22 pm

Re: Receiving UDP multicast packets?

Post by Khorus »

Thanks Jassing for checking my code! I slept on it yesterday and woke up this morning thinking that the problem might be in the UDP Multicasts. They are *NOT* addressed to my IP. The MIDI device sends it on "225.0.0.37" (some sort of a broadcast address I believe). Wireshark receives them OK, I completely disabled my firewall.

The funny part is that the other way around works well, here's what I used:

OpenNetworkConnection("225.0.0.37", 21928, #PB_Network_UDP) and the usual SendNetworkString...

Looks like PureBasic needs to be triggered by the machine's local IP address (or 127.0.0.1).

Let me know if you have any ideas! Thanks again!

Marc Girard aka Khorus
auser
Enthusiast
Enthusiast
Posts: 195
Joined: Wed Sep 06, 2006 6:59 am

Re: Receiving UDP multicast packets?

Post by auser »

I've written something like that (listening to Multicast stuff received over an address that is not the address of my NW-Adapter) via C++ a few days ago. But I'm not sure if you can do this with PB-Network functions. Anyway you can use sockets in PB as well :) ...otherwise I'm not sure how you could add something like "setsockopt (socket, IPPROTO_IP, IP_MULTICAST_IF, &interface_addr, sizeof(interface_addr));" or "setsockopt (socket, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq));" to your socket created in background via CreateNetworkServer. And otherwise if you miss that why should your server ever listen on an address that does not really belong to him (even 0.0.0.0 is "too less" here). So I would recommend use some socket_(), setsockopt_(), bind_(), setsockopt_(), recvfrom_() instead the CreateNetworkServer - and probably take a look here: http://tldp.org/HOWTO/Multicast-HOWTO-6.html

Greetings,
auser
Birdman
New User
New User
Posts: 1
Joined: Wed Dec 31, 2014 3:21 pm

Re: Receiving UDP multicast packets?

Post by Birdman »

I wasn't able to receive Multicast Sockets using Purebasic in C# i used an option like

Code: Select all

s.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(ip, IPAddress.Any));
to make this work.

Anyone out there had made this work for purebasic?
infratec
Always Here
Always Here
Posts: 7666
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Receiving UDP multicast packets?

Post by infratec »

Hi,

with pure PureBasic it is not possible.
I did it with window socket stuff.

I wrote a small program to receive the MicroChip network broadcasts.

Maybe I'll have already written about this. I'll check.

Found it:

http://www.purebasic.fr/english/viewtop ... 18#p451018

Bernd
Post Reply