8 years, 5 months ago.

Binding two UDP Sockets fails

I have an application that needs two multicast UDP sockets. The program can only bind the first one, binding the second one failed. Can you help me what's going wrong?

Here the code

// include mbed libraries
#include "mbed.h"
#include "EthernetInterface.h"

// variables
char sacnBufferA[638]; // packet buffer for sending UDP data
char sacnBufferB[638]; // packet buffer for sending UDP data
int universeReceiveA = 1;
int universeReceiveB = 2;

// ethernet variables
const int SACN_PORT = 5568;

char IP_ADDRESS[] = "10.101.1.200";
char IP_ADDRESS_MASK[] = "255.255.0.0";
char IP_ADDRESS_GATEWAY[] = "0.0.0.0";

int sacnLengthA;
int sacnLengthB;

// ethernet
EthernetInterface eth;
UDPSocket sacnA;
UDPSocket sacnB;
Endpoint sacnMultiA;
Endpoint sacnMultiB;

// main
int main() {

  // ethernet
  eth.init(IP_ADDRESS, IP_ADDRESS_MASK, IP_ADDRESS_GATEWAY);
  eth.connect();

  // debug
  printf("IP Address %s\n", eth.getIPAddress());
  printf("Subnet %s\n", eth.getNetworkMask());
  printf("MAC Address %s\n", eth.getMACAddress());

  // udp non blocking
  sacnA.set_blocking (false);
  sacnB.set_blocking (false);

  // connection to multicast port
  if (sacnA.bind(SACN_PORT) == -1) printf("socket A bind failed \n");
  if (sacnA.join_multicast_group("239.255.0.1") == -1) printf("multicast A failed \n");

  if (sacnB.bind(SACN_PORT) == -1) printf("socket B bind failed \n");
  if (sacnB.join_multicast_group("239.255.0.2") == -1) printf("multicast B failed \n");

  // main loop
  while (true) {
    //printf("\nWait for packet...\n");
    sacnLengthA = sacnA.receiveFrom(sacnMultiA, sacnBufferA, sizeof(sacnBufferA));
     printf("Packet sACN A from %s\": %d\n", sacnMultiA.get_address(), sacnLengthA);
    sacnLengthB = sacnB.receiveFrom(sacnMultiB, sacnBufferB, sizeof(sacnBufferB));
     printf("Packet sACN B from %s\": %d\n", sacnMultiB.get_address(), sacnLengthB);
    }
  }
Be the first to answer this question.