send UDP without recieving

Hi all,

I’ve a question about the Arduino EthernetUDP lib. I want to send data without first recieving data. I need to set Udp.remoteIP() manualy. The UDPSendReceiveString example ist working so I assume that my network, laptop, firewall and all the rest ist properly setup.

Any ideas whats wrong with my code?

#include <Arduino.h>
#include <Ethernet.h>
#include <EthernetUdp.h>

byte mac[] = {
 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};

unsigned int localPort = 8888;      // local port to listen on

EthernetUDP Udp;

IPAddress ip(192, 168, 178, 200);
IPAddress rip(192, 168, 178, 43);

void setup() {
 Serial.begin(9600);
 
 Ethernet.init(10);  // Most Arduino shields

 Ethernet.begin(mac, ip);


 //Small delay for link status
 delay(1000);
 if (Ethernet.linkStatus() == LinkOFF) {
   Serial.println("Ethernet cable is not connected.");
 }
 else{
   Serial.println("Ethernet cable connected.");
 }

 Udp.begin(localPort);
}

void loop() {
 delay(1000); 

 //Nothing of the two version below are working
 Udp.beginPacket(rip, Udp.remotePort());
 //Udp.beginPacket("192.168.178.43", Udp.remotePort());
 Udp.write("Hallo");
 Udp.endPacket();
}

Udp.remotePort() tells you the remote port of a packet that has been sent to you.

If you want to send a message then in beginPacket you need to specify the remote port that is listening on the remote machine (not the remote port that sends a packet). Since the remote machine has not sent you a packet Udp.remotePort() won't be able to help you.

You know the remote machines ip address, you should also be able to determine the remote machiens listening port for your test, it should be an integer so if your remotePort is 1234 you can either do
#define remotePort 1234
or
const unsigned int remotePort = 1234;

then

Udp.beginPacket(rip, remotePort);

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.