Skip to content

Can't create a UDP Socket and Multicast Socket #105

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Makuna opened this issue Apr 24, 2015 · 3 comments
Closed

Can't create a UDP Socket and Multicast Socket #105

Makuna opened this issue Apr 24, 2015 · 3 comments

Comments

@Makuna
Copy link
Collaborator

Makuna commented Apr 24, 2015

I need to have two listening sockets (Contexts in Esp8266/Arduiino speak) for my CoapServer. One for the Multicast and one for a unicast. They both use the same port by CoAp standards.

Which ever one is created first succeeds, but the second one created fails in UdpContext::Listen. The call to udp_bind within UdpContext::Listen returns error code is ERR_USE; meaning the address is in use?

Coap Port is 5684
Coap Multicast Address is 224.0.1.187

By normal socket concepts this should be doable, not so sure by Esp8266 context concepts.

Further, if I just create a multicast socket, while it will receive multicast packets, it will not receive packets sent directly to the ipaddress of the device. This works as I would expect IF I can create the two functional sockets.

here are snippits of the code, just for completeness, they work independently but not when called back to back.

multicast

    multicastSocket = new WiFiUDP();

        if (!multicastSocket ->beginMulticast(WiFi.localIP(),
            IPAddress(CoapMultiCastAddress) ,
            CoapPort))
        {
            // error
        }

unicast

    unicastSocket = new WiFiUDP();

    // unicast
    if (!unicastSocket->begin(CoapPort))
    {
        // error
    }
igrr added a commit that referenced this issue Apr 27, 2015
Might be useful to distinguish between normal and multicast packets arriving at the same port (#105)
@igrr
Copy link
Member

igrr commented Apr 27, 2015

With lwIP network stack you can only bind one PCB to a given port.
However, joining a multicast group is not a property of PCB, it is a property of an interface. This means that when you create a PCB and bind it to a port, it will receive both multicast and unicast packets targeting this port, provided you have joined the multicast group.

I've added WiFiUDP::destinationIP method that you may use to distinguish the packets sent to a multicast and unicast destinations.

Here's my test sketch:

void loop() {
  int cb = Udp.parsePacket();
  if (!cb)
    return;

  Serial.print("got udp packet from ");
  Serial.print(Udp.remoteIP());
  Serial.print(" to ");
  Serial.println(Udp.destinationIP());
  delay(1000);
}

I send some packets using nc:

$ date | nc -u -n -w 1 224.1.1.1 5322
$ date | nc -u -n -w 1 192.168.1.26 5322

And here's the output:

got udp packet from 192.168.1.23 to 224.1.1.1
got udp packet from 192.168.1.23 to 192.168.1.26

@Makuna
Copy link
Collaborator Author

Makuna commented Apr 27, 2015

But I was not receiving a unicast packet to the "context" that was created with the Multicast Join. Could you include your setup for that here so I can confirm I wasn't doing something wrong.

igrr added a commit that referenced this issue Apr 28, 2015
Might be useful to distinguish between normal and multicast packets arriving at the same port (#105)
@Makuna
Copy link
Collaborator Author

Makuna commented Apr 30, 2015

Confirmed I am receiving both types with correct destination addresses.

@Makuna Makuna closed this as completed Apr 30, 2015
igrr added a commit that referenced this issue Oct 29, 2015
Might be useful to distinguish between normal and multicast packets arriving at the same port (#105)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants