Hi all
Hope u are ok 
I tried to build two UDP servers from(UDPSendRecieve) example on same IP address but with different Ports, i'll write below declarations:
unsigned int localPort = 8888;
unsigned int localPort2 = 2200;
EthernetUDP Udp;
EthernetUDP Udp2;
void setup()
{
Ethernet.begin(mac,ip);
Udp.begin(localPort);
Udp2.begin(localPort2);
Serial.begin(9600);
}
void loop()
{
Udp2.remoteIP()=Udp.remoteIP();
Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
Udp.write(s);
Serial.println(s);
Udp.endPacket();
delay(500);
Udp2.beginPacket(Udp2.remoteIP(), localPort2);
Udp2.write(s);
Serial.println("@ 2200 port");
Udp2.endPacket();
}
the code was uploaded successfully but the Udp2 doesn't work.
is there any problems with declarations?
or
is this impossible to work?
thank you for your help 
The Arduino processor does not thread, so you can't send to yourself like that. You can test it with a program called Netcat installed on your computer.
edit: You can't use the remoteIP() and remotePort() calls like that. They only work if you have received a packet from a remote device.
Like I said just a second ago on my edit: You cant use remoteIP() and remotePort() calls like that. They only work if you have received a packet from a remote device.
void loop()
{
Udp.beginPacket(ip,localPort2);
Udp.write(s);
Serial.println(s);
Udp.endPacket();
delay(500);
// check if you have received a packet here like the UdpSendReceiveString example
Udp2.beginPacket(Udp2.remoteIP(), localPort2);
Udp2.write(s);
Serial.println("@ 2200 port");
Udp2.endPacket();
}
edit: I presume this is just an exercise to determine if the UDP library works. I see no purpose to this. It is the equivalent to talking to yourself.
SurferTim:
Like I said just a second ago on my edit: You cant use remoteIP() and remotePort() calls like that. They only work if you have received a packet from a remote device.
i made fix IP address and fix variable called (localport2) like below and still the same problem.
** Udp.beginPacket(ip,localPort2);**
void loop()
{
Udp.beginPacket(ip,localPort2);
Udp.write(s);
Serial.println(s);
Udp.endPacket();
delay(500);
// check if you have received a packet here like the UdpSendReceiveString example
Udp2.beginPacket(Udp2.remoteIP(), localPort2);
Udp2.write(s);
Serial.println("@ 2200 port");
Udp2.endPacket();
}
edit: I presume this is just an exercise to determine if the UDP library works. I see no purpose to this. It is the equivalent to talking to yourself.
**No My friend, i don't want to talk my self but, i want to use the second port as an emergence messages that transfer during the system while the other port used for normal data. **
thank you for replying 
Are you just sending or will one or both be receiving UDP packets? Will the Arduino be sending a packet, then receiving a response? Or will it receive a packet and send a response to the sender?
look i didn't try to receive packets on the second port but my test was like following:
receiving UDP packet on port 1 from PC and then send msg back to same port and then send same msg on the second port port2.
on PC side , i received data on port1 but nothing on port2.
I don't have code for that scenario. I do have code that sends packets on two udp sockets to two different computers and gets a response. I prefer that method since it requires no modification to the router to receive packets from the "server". It works like NTP and DNS.
edit: Here it is
#include <SPI.h>
#include <Ethernet.h>
#include <EthernetUdp.h>
// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
unsigned int UDPport = 5005; // local port to listen for UDP packets
unsigned int UDP2port = 5006; // local port to listen for UDP packets
IPAddress UDPServer(192,168,1,249);
IPAddress UDP2Server(192,168,2,250);
const int UDP_PACKET_SIZE= 48;
byte packetBuffer[ UDP_PACKET_SIZE]; //buffer to hold incoming and outgoing packets
byte packet2Buffer[ UDP_PACKET_SIZE]; //buffer to hold incoming and outgoing packets
EthernetUDP Udp;
EthernetUDP Udp2;
unsigned long currentTime;
unsigned long secondTime;
unsigned long msPerSecond = 1000UL;
unsigned int udpCount = 0;
unsigned int udp2Count = 0;
boolean packetRcvd = true;
boolean packet2Rcvd = true;
void setup()
{
Serial.begin(115200);
pinMode(4,OUTPUT);
digitalWrite(4,HIGH);
// start Ethernet and UDP
Serial.print(F("Starting w5100..."));
while(!Ethernet.begin(mac)) {
Serial.println(F("failed. Retrying in 5 seconds."));
delay(5000);
Serial.print(F("Starting w5100..."));
}
Serial.println(Ethernet.localIP());
Udp.begin(UDPport);
Udp2.begin(UDP2port);
delay(2000);
currentTime=millis();
secondTime = currentTime;
Serial.println("Ready");
}
void loop()
{
currentTime = millis();
getUDPpacket();
getUDP2packet();
if(currentTime - secondTime > msPerSecond) {
byte rtnVal = Ethernet.maintain();
switch(rtnVal) {
case 1: Serial.println(F("\r\nDHCP renew fail"));
break;
case 2: Serial.println(F("\r\nDHCP renew ok"));
break;
case 3: Serial.println(F("\r\nDHCP rebind fail"));
break;
case 4: Serial.println(F("\r\nDHCP rebind ok"));
break;
}
Serial.println(F("\r\nUDP send"));
sendUDPpacket(UDPServer); // send an NTP packet to a time server
sendUDP2packet(UDP2Server); // send an NTP packet to a time server
secondTime += msPerSecond;
}
}
// send an NTP request to the time server at the given address
void sendUDPpacket(IPAddress& address)
{
udpCount++;
if(!packetRcvd) Serial.println(F("Last packet receive fail"));
packetRcvd = false;
// set all bytes in the buffer to 0
memset(packetBuffer, 0, UDP_PACKET_SIZE);
sprintf((char*)packetBuffer,"Arduino count %u",udpCount);
Udp.beginPacket(address, UDPport); //NTP requests are to port 123
Udp.write(packetBuffer,UDP_PACKET_SIZE);
Udp.endPacket();
}
void sendUDP2packet(IPAddress& address)
{
udp2Count++;
if(!packet2Rcvd) Serial.println(F("Last packet receive fail"));
packet2Rcvd = false;
// set all bytes in the buffer to 0
memset(packet2Buffer, 0, UDP_PACKET_SIZE);
sprintf((char*)packet2Buffer,"Arduino count %u",udp2Count);
Udp2.beginPacket(address, UDP2port); //NTP requests are to port 123
Udp2.write(packet2Buffer,UDP_PACKET_SIZE);
Udp2.endPacket();
}
void getUDPpacket() {
if ( Udp.parsePacket() ) {
// We've received a packet, read the data from it
if(Udp.remoteIP() == UDPServer) {
Serial.print(F("UDP IP OK "));
}
else {
Serial.println(F("UDP IP Bad"));
return;
}
if(Udp.remotePort() == UDPport) {
Serial.println(F("Port OK"));
}
else {
Serial.println(F("Port Bad"));
return;
}
packetRcvd = true;
Udp.read(packetBuffer,UDP_PACKET_SIZE); // read the packet into the buffer
Serial.print(F("Received: "));
Serial.println((char*)packetBuffer);
}
}
void getUDP2packet() {
if ( Udp2.parsePacket() ) {
// We've received a packet, read the data from it
if(Udp2.remoteIP() == UDP2Server) {
Serial.print(F("UDP2 IP OK "));
}
else {
Serial.println(F("UDP2 IP Bad"));
return;
}
if(Udp2.remotePort() == UDP2port) {
Serial.println(F("Port OK"));
}
else {
Serial.println(F("Port Bad"));
return;
}
packet2Rcvd = true;
Udp2.read(packet2Buffer,UDP_PACKET_SIZE); // read the packet into the buffer
Serial.print(F("Received: "));
Serial.println((char*)packet2Buffer);
}
}
Thank you for the code, it was a great idea to use UDP like DNS 
I will try to put fix IP2 address instead of Remote IP1 for the UDP2 that uses second port and see what happens. 