Dual Web Server Ports 80 and 8080

I am trying to get a two web servers running on a mega 2560 with a ethernet shield. One on port 80 andf the other on port 8080. I have searched the forum here and have found a few topics addressing this, especially one by SurferTim at Is it possible to create multiple servers on Arduino Ethernet - #4 by SurferTim - Networking, Protocols, and Devices - Arduino Forum, but I can not get his "example" working. Does anyone have a sketch that successfully has two web servers running?

I am using port 80 to transfer data from my mega 2560 into mysql via php scripts based upon this tutorial, Tweaking4All.com - Arduino Ethernet - Pushing data to a (PHP) server, and now need to use the same 2560 to push realtime sensor data through port 8080 to some python scripts on another machine than the box running the php and mysql.

I am pushing data through port 80 every 60 seconds, so if possible, I could push the data through port 80, then shutdown that interface, open a session with port 8080 and push the data to my python scripts, then close that connection and reestablish the port 80 session. But I can not even get this model working.

Any help woill be greatly appreciated..

Thank you in advance,
Murrah Boswell

Here is my test code. Change the network settings to yours.

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

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
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 SD if one in slot
  pinMode(4,OUTPUT);
  digitalWrite(4,HIGH);

  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];
    
    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 1</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"));
  }

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

    boolean currentLineIsBlank = true;
    boolean currentLineIsGet = true;
    int tCount = 0;
    char tBuf[64];
    
    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"));
  }
}

Hello SurferTim,

Thanks for your quick response. I got your code and built a sketch. It works just like you said. Magical!

Thank you very much!

Now I just need to see if I can integrate it into my system.

Regards,
Murrah Boswell