Ethernet Support for Multiple Ports?

I've used Ethernet for doing telnet, UDP, and now web server, all with excellent results. But I now have an application for which it would be nice to be able to have both web server and UDP access simultaenously. Is this even possible, given that the EthernetServer object is created for a specific port number?

Regards,
Ray L.

Yes, it is possible.

I have a single object/class definition for webserver, and a single function that serves the webpage(s) to handle that.

I declared just one webclient, but I use that object for different tasks. To read websites, to upload to ThingSpeak.com, and so on.

I also have a UDP, and use it now and then to get the time. I might use that same udp object for other things, but not yet.

And I can retrieve mail from a mail server and send mail to a mailserver.

Peter_n:
Yes, it is possible.

And I can retrieve mail from a mail server and send mail to a mailserver.

related question and not trying to hijack this thread, but @Peter_n, would you have posted an example? I am looking to get Unread mail count for a project I have been working on.

Peter_n:
Yes, it is possible.

I have a single object/class definition for webserver, and a single function that serves the webpage(s) to handle that.

I declared just one webclient, but I use that object for different tasks. To read websites, to upload to ThingSpeak.com, and so on.

I also have a UDP, and use it now and then to get the time. I might use that same udp object for other things, but not yet.

And I can retrieve mail from a mail server and send mail to a mailserver.

Peter,

That is good news! So, if I understand correctly (a major assumption in this case.......), in the simplest case, the code would look something like this?

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(10, 0, 0, 42);
 
EthernetServer udpserver(6666);
WebServer webserver(80);
 
void setup()
{
    Ethernet.begin(mac, ip);
    udpserver.begin();
    webserver.begin();
}


void loop(void)
{
    EthernetClient udpclient = udpserver.available();
    if (udpclient) {
        // Handle udp traffic
    }
    EthernetClient webclient = webserver.available();
    if (webclient) {
        // Handle web traffic
    }
}

Regards,
Ray L.

Below is an example of dual server code from past forum postings.

#include <SPI.h>
#include <Ethernet.h>
#include <SD.h>

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

// change to your network settings
IPAddress ip( 192,168,2,2 );
IPAddress gateway( 192,168,2,1 );
IPAddress subnet( 255,255,255,0 );

EthernetServer server(80);
EthernetServer server2(8080);

void setup()
{
  Serial.begin(9600);

  // disable w5100 while setting up SD
  pinMode(10,OUTPUT);
  digitalWrite(10,HIGH);

  Serial.print(F("Starting SD.."));
  if(!SD.begin(4)) Serial.println(F("failed"));
  else Serial.println(F("ok"));
  
  Ethernet.begin(mac, ip, gateway, gateway, subnet);
  digitalWrite(10,HIGH);

  delay(2000);
  server.begin();
  server2.begin();
  Serial.println(F("Ready"));
}

void loop()
{
  EthernetClient client = server.available();
  if(client) {
    boolean currentLineIsBlank = true;
    boolean currentLineIsGet = true;
    int tCount = 0;
    char tBuf[64];
    int r,t;
    char *pch;
    IPAddress ipBuf;
    
    Serial.print(F("Client request: "));
    
    while (client.connected()) {
      while(client.available()) {
        char c = client.read();

        if(currentLineIsGet && tCount < 63)
        {
          tBuf[tCount] = c;
          tCount++;
          tBuf[tCount] = 0;          
        }

        if (c == '\n' && currentLineIsBlank) {
          // send a standard http response
          Serial.println(tBuf);
          Serial.print(F("POST data: "));
          while(client.available()) Serial.write(client.read());
          Serial.println();
          
          pch = strtok(tBuf,"?");

          while(pch != NULL)
          {
            if(strncmp(pch,"t=",2) == 0)
            {
              t = atoi(pch+2);
              Serial.print(F("t="));
              Serial.println(t,DEC);             
            }

            if(strncmp(pch,"r=",2) == 0)
            {
              r = atoi(pch+2);
              Serial.print(F("r="));              
              Serial.println(r,DEC);
            }


            pch = strtok(NULL,"& ");
          }
          Serial.println(F("Sending response"));
          client.println(F("HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n<html>"));
          client.println(F("<body><H1>TEST</H1>"));
          client.println(F("<form method=GET>T: <input type=text name=t>
"));
          client.println(F("R: <input type=text name=r>
<input type=submit></form>"));
          client.println(F("</body></html>\r\n"));
          client.stop();
        }
        else if (c == '\n') {
          currentLineIsBlank = true;
          currentLineIsGet = false;
        } 
        else if (c != '\r') {
          currentLineIsBlank = false;
        }
      }
    }
    Serial.println(F("done"));
  }

  client = server2.available();
  if(client) {
    Serial.println(F("Server2"));

    boolean currentLineIsBlank = true;
    boolean currentLineIsGet = true;
    int tCount = 0;
    char tBuf[64];
    int r,t;
    char *pch;
    IPAddress ipBuf;
    
    Serial.print(F("Client request: "));
    
    while (client.connected()) {
      while(client.available()) {
        char c = client.read();

        if(currentLineIsGet && tCount < 63)
        {
          tBuf[tCount] = c;
          tCount++;
          tBuf[tCount] = 0;          
        }

        if (c == '\n' && currentLineIsBlank) {
          // send a standard http response
          Serial.println(tBuf);
          Serial.print(F("POST data: "));
          while(client.available()) Serial.write(client.read());
          Serial.println();
          
          Serial.println(F("Sending response"));
          client.println(F("HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n<html>"));
          client.println(F("<body><H1>TEST Server 2</H1>"));
          
          client.println(F("</body></html>\r\n"));
          client.stop();
        }
        else if (c == '\n') {
          currentLineIsBlank = true;
          currentLineIsGet = false;
        } 
        else if (c != '\r') {
          currentLineIsBlank = false;
        }
      }
    }
    Serial.println(F("done"));
  }
}

Perfect! Thanks!

Regards,
Ray L.

BulldogLowell, this is code for some specific use, but it should not be hard to change it : Arduino Playground - EmailPOP3
The client object that I use for email is actually the same client that I also use for other things.