TcpClient not connecting to server

Hello,

I have a photon which listens for multicast udp packets and whenever someone sends a message to the multicast address it should retrieve the IPAddress of the endpoint that did it and start a TCP connection with it.

So in my Ubuntu I have a program that using boost opens a TCP server and sends the UDP message to the photon. This steps are don properly, and the photon detects the udp message.

Then in the phothon I have this piece of code:

// At the beggining
UDP multicastUDP;
TCP tcpClient:

// Inside loop function, after receiving a connection
IPAddress multicastSenderIP = multicastUDP.remoteIP();
Serial.println("Discovery message received from " + multicastSenderIP.toString());
while(multicastUDP.available()) multicastUDP.read(); // ignore all data
    
if(tcpClient.connect(multicastSenderIP, tcpPort)) // Connect to the tcp server (at same address as multicast)
{
    Serial.println("Connected to tcp server");
    tcpClient.stop();
}
else
{
    Serial.println("Can't connect to tcp server");
}

I’m receiving the multicast packet and can’t seem to connect to the TCP server. I tried writing a little program with unix sockets and it does work to open a connection with ip/port of my computer.

Also note that when receiving the udp packet it only prints: Discovery message received from and no IP address is printed.

Could someone point out what I’m doing wrong?

Thanks

You should instead use udp.receivePacket().

https://docs.particle.io/reference/firmware/photon/#receivepacket-

Once you get a valid packet, then multicastUDP.remoteIP() will have a valid value. You’re getting the remoteIP address too soon, before the packet arrives, and it’s obviously not known yet.

Also, it’s better to not block the loop like that. Just call receivePacket() each time through loop, and if you get one, then make the TCP calls.

2 Likes

I made some changes to my code to find the root of the problem. Yesterday I saw that I had a problem with the TCP server and made it impossible for the photon to connect to it.

But now I’m having trouble with the UDP multicast packets. I can actually see the packet in wireshark, but the photon is not correctly getting it. Yesterday I used the method UDP::Available() and checked if it was > 0 but now this is not detecting the multicast packet. So I tried using receivePacket instead, with no luck.

static const int multicastPort = 20452;

// Multicast receiver
UDP multicastUDP;
IPAddress multicastAddress(239,255,0,1);

void setup() {
    // serial begin and stuff
    multicastUDP.begin(multicastPort);
    multicastUDP.joinMulticast(multicastAddress); // Subscribe to multicast address
}

void loop() {
    uint8_t message[128];
    if(multicastUDP.receivePacket((uint8_t *) message, 127) > 0)
    {
        logMsg("Hello world");
    }
    else
    {
        logMsg("No message");
    }

    delay(500);
}

This is printing No message to console all the time, although I can see (using wireshark) that the multicast packet is sent. And yesterday I managed to make it work with UDP:Available()! Haven’t blocked multicast or anything on the router, so I don’t really know what this problem can be.

Are you using SYSTEM_THREAD(ENABLED)? If so, you have to wait until WiFi.ready() before calling the udp.begin() or joinMulticast().

Other than that, it looks like it should work.

I’m not using it, and it’s really weird that yesterday it did work fine (the part of detecting the UDP packet) but not today.