Use of ports.

Hello,

I am trying hard to run a webserver and upload data to pachube at the same time. The code works for a while and freezes randomly.

I am trying to debug this situation and I came close to this statements:

byte ip[] = { 192, 168, 1, 123 };
byte remoteServer[] = { 209,40,205,190 }; // pachube.com
Server server(80);
Client localClient(remoteServer, 80);

Can a server and a client share the same port ?

many thanks,
Marian

Can a server and a client share the same port ?

Only if the Server and Client reside on the same computer they communicate both through the same port.

However your code states there is a local SERVER on port 80 and your local client want to talk to a remote server on it's port 80. So there is no conflict. Your client will typically use a portnumber above 1023 to communicate with the pachube server.

IIRC pachube does not like too much traffic from one client so maybe your updating frequency is too high. I don't have a reference describing this unfortunately. Maybe you could share the complete code (use the # button to tag code appropiately with [ code] [ /code] tags) so we can see if there is another problem?

If your Arduino is accessible outside your LAN, it might be that it's getting swamped by zombies probing for vulnerable systems.

I usually use higher port numbers to avoid the traffic sent to the standard ones, and always put systems like Arduinos behind a firewall to screen it out.

Also, after reading on the old forum about problems others have had, I started adding a "manual" reset of the Wiznet chip via a digital pin. If retries fail, I reset the chip.

Gentlemen, thank you very much for your support.
A bit about configuration: It's a reef tank controller that is uploading four datastreams to pachube. The webserver picks up four graphs from pachube and displays as webpage. This controller (Arduino Mega/wiznet5100), as a primary function, controls a lot of features of the tank, so stability is a main concern. So far, without latest pachube update it was rock solid for two years. Hope it will stay like this.
I am uploading to pachube once a minute (no reason for hammering) and I can go even slower since in a reef only bad things happen fast.
So far, with the server on port 81 and client on 80 it was stable for the last 12 hours.

Here are some fragments of the code;

#define SHARE_FEED_ID      xxxxx
#define UPDATE_INTERVAL  60000
#define RESET_INTERVAL    30000   
#define PACHUBE_API_KEY   "xxxxx"

....

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192, 168, 1, 123 };
byte remoteServer[] = { 173, 203, 98, 29  };            // pachube.com
Server server(81);
Client localClient(remoteServer, 80);
char pachube_data[128];


...

void pachube_in_out() {
  if (request_pause){
    if ((millis() - last_connect) > interval){
      ready_to_update = true;
      reading_pachube = false;
      request_pause = false;
      found_status_200 = false;
      found_session_id = false;
      found_CSV = false;
    }
  }
  // Serial.println ("Before ready to connect");
  if (ready_to_update){
    if (localClient.connect()) {
         
      int temp_Current1 = (temp_Current - (int)temp_Current)*100;
      int pH_val1 = (pH_val - (int)pH_val)*100;
      
      sprintf(pachube_data,"%0d.%d,%0d.%d,%d,%d,%d,%d",(int)temp_Current,abs(temp_Current1),(int)pH_val,abs(pH_val1),orp_val,orpSD_val, successes + 1, failures);
      
      localClient.print("PUT /api/");
      localClient.print(SHARE_FEED_ID);
      localClient.print(".csv HTTP/1.1\nHost: pachube.com\nX-PachubeApiKey: ");
      localClient.print(PACHUBE_API_KEY);
      localClient.print("\nContent-Type: text/csv\nContent-Length: ");
      localClient.print(strlen(pachube_data));
      localClient.print("\nConnection: close\n\n");
      localClient.print(pachube_data); 
      localClient.print("\n");

      ready_to_update = false;
      reading_pachube = true;
      request_pause = false;
      interval = UPDATE_INTERVAL;

      delay(100);
    } 
    else {   
      Serial.print("connection failed!");
      Serial.print(++failures);
      found_status_200 = false;
      found_session_id = false;
      found_CSV = false;
      ready_to_update = false;
      reading_pachube = false;
      request_pause = true;
      last_connect = millis();
      interval = RESET_INTERVAL;
      setupEthernet(interval);
    }
  }

  while (reading_pachube){
    while (localClient.available()) {
      checkForResponse();
    } 

    if (!localClient.connected()) {
      disconnect_pachube();
    }
  } 
}

I will keep you updated about stability,

cheers,
Marian

Ran,

I have it behind a router with firewall (wrt54g/dd-wart firmware), but using it as webserver I have to port forward a port. Right ? There is no other way around I guess.

cheers,
Marian

You have to forward some port, but the bots focus their efforts on the standard ports, so using a high-numbered one will significantly reduce the number of nuisances.

Ran, absolutely right. I know that the webserver can take any available port without any problem. What about pachube ? Can one use any port to feed ?

cheers,
Marian