connecting to a switch network

I'm trying to connect to localhost xampp server on a computer connected to a switch router, I do IPconfig and see there isnt any default network IP and I remember hearing that switch's use mac address to communicate between other computers.
Im getting failed to connnect from my serialconnect.
PS I also cant seem to map my analog signal, I tried a x = map(x 0, 1023, 0, 255) but the variable when called from serial and the server does return anything, Ive been just using the raw inout value for testing purposes.

#include <WString.h>
#include <Ethernet.h>
#include <SPI.h>
byte mac[] = { 0x00, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,1,9); // ip in lan
IPAddress gateway(192,168,1,1); // internet access via router
IPAddress subnet(255,255,255,0); //subnet mask
IPAddress myserver(192,168,1,4); // zoomkat web page
EthernetServer server(80); //server port
EthernetClient client;
String readString = String(30);
int chlorinelvl = A0;
long previousMillis = 0;        // will store last time LED was updated

// the follow variables is a long because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long interval = 100000;           // interval at which to blink (milliseconds)
boolean data= LOW;
void setup(){
  Serial.begin(9600);
  Ethernet.begin(mac, ip, subnet, gateway); 
  pinMode(3, OUTPUT); digitalWrite(3, LOW); // led 1
  pinMode(4, OUTPUT); digitalWrite(4, LOW); // used as a ground
  pinMode(8, OUTPUT); digitalWrite(8, LOW); // led 2
  pinMode(9, OUTPUT); digitalWrite(9, LOW); // used as a ground
  pinMode(chlorinelvl, INPUT);
  

}

void loop(){
  
EthernetClient client = server.available();
int chlorinedata = 0;
chlorinedata = analogRead(chlorinelvl);
  
  
                 
unsigned long currentMillis = millis();
 
  if(currentMillis - previousMillis > interval) {
    // save the last time you blinked the LED 
    previousMillis = currentMillis;   

    // if the LED is off turn it on and vice-versa:
Serial.print("hi");
data= !data;
Serial.print(digitalRead(data));
    // set the LED with the ledState of the variable:
    if (data==HIGH){
          Serial.print("data working") ;
          sendGET();
          Serial.print(analogRead(chlorinelvl));   
             if(chlorinedata>=0){
                 sendHELP();  }  
        }
  }
  if (client) {
    while (client.connected()) {
	if (client.available()) {
	  char c = client.read();
	  

          if (readString.length() < 100){
	    readString.concat(c);
	  }
	  if (c == '\n') {
	  
		 if(readString.indexOf("led8") >=0)  {
		   toggle(8);
                  }
		 if(readString.indexOf("led3") >=0) {
		   toggle(3);
		 }
             
		 client.println("<html><center>");
		 client.println("Light 3
");
		 if (digitalRead(3) == LOW){
		   
                   client.println("Light 3 OFF");
		 } else if (digitalRead(3) == HIGH){
		   
                    client.println("Light 3 ON");
		 }
		 client.println("<form method='get'><input type=submit name=led3 value=toggle></form>");

		 client.println("Light 8
");
		 if (digitalRead(8) == LOW){
		  Serial.println("off LED"); 
                  client.println("Light off 8
");
                  
		 } 
                  else if (digitalRead(8) == HIGH){
                    client.println("Light ON 8 
");
                 
                  
		 }
		 client.println("<form method='get'><input type=submit name=led8 value=toggle></form>");
		 client.println("</center></html>");
	    readString="";
	    client.stop();
	  }
	}
    }
  }
}

int toggle(int pin){
  if(digitalRead(pin) == LOW){
    digitalWrite(pin, HIGH);
  } else {
    digitalWrite(pin, LOW);
  }
}




int chlorinedata=analogRead(chlorinelvl);

void sendGET() //client function to send/receie GET request data.
{
  if (client.connect(myserver, 80)) {
    Serial.println("connected");
   client.print("GET http://localhost/handler.php?watt=");
client.print(analogRead(chlorinelvl));
client.println(" HTTP/1.1");
 client.println("Host: 192.168.0.126");
client.println("Connection: close");
client.println();

 Serial.print(chlorinedata);
  } 
  else {
    Serial.println("connection failed");
    Serial.println();
  }


  Serial.println();
  Serial.println("disconnecting.");
  Serial.println("==================");
  Serial.println();
  client.stop();

}
void sendHELP() //client function to send/receie GET request data.
{
  if (client.connect(myserver, 80)) {
    Serial.println("connected");
   client.print("GET http://localhost/email.php?watt=");
client.print(chlorinedata);
client.println(" HTTP/1.1");
 client.println("Host: 192.168.0.126");
client.println("Connection: close");
client.println();

 Serial.print(chlorinedata);
  } 
  else {
    Serial.println("connection failed");
    Serial.println();
  }


  Serial.println();
  Serial.println("disconnecting.");
  Serial.println("==================");
  Serial.println();
  client.stop();

}

Your problem starts at this line:

  Ethernet.begin(mac, ip, subnet, gateway);

The current versions of the IDE define the begin() method of the Ethernet class this way:

  void begin(uint8_t *mac_address, IPAddress local_ip, IPAddress dns_server, IPAddress gateway, IPAddress subnet);

Note the dns_server parameter between local_ip and gateway.

So i'm using older version of code, but still switch doesn't have dns or subnet, how do i rearrange things when switch doesn't use those parts? I tried removing subnet and running the code, but still doesn't connect.

It seems to me that you want to use your Arduino as a server and you want to access it from a PC in your local network. If this is correct, use:

Ethernet.begin(mac, ip);

Otherwise: describe your network setup.

Try to eliminate the String class (it's buggy), replace it with character arrays (C strings).

This is the main part of the code I'm trying to get working, I have mil sec timer triggering the get request that sends analog signal to the php handler that sends it to mysql.

void sendGET() //client function to send/receie GET request data.
{
  if (client.connect(myserver, 80)) {
    Serial.println("connected");
   client.print("GET http://localhost/handler.php?watt=");
client.print(analogRead(chlorinelvl));
client.println(" HTTP/1.1");
 client.println("Host: 192.168.0.126");
client.println("Connection: close");
client.println();

 Serial.print(chlorinedata);
  } 
  else {
    Serial.println("connection failed");
    Serial.println();
  }

Have you read my post?

Sorry, change to c string?

What I want the ardiuno to do is take analog reading every so many mil sec's then send get request with the analog readings(preferably map into scale, which doesnt seem to work.). Alot of the code was isnt being used mostly the string code.

#include <WString.h>
#include <Ethernet.h>
#include <SPI.h>
byte mac[] = { 0xb8, 0x88, 0xE3, 0xB7, 0x95, 0xF3 };
IPAddress ip(169,254,131,179); // ip in lan
IPAddress gateway(192,168,1,1); // internet access via router
IPAddress subnet(255,255,0,0); //subnet mask
IPAddress myserver(100,100,100,6); // zoomkat web page
EthernetServer server(3333); //server port
EthernetClient client;
String readString = String(30);
int chlorinelvl = A0;
long previousMillis = 0;        // will store last time LED was updated

// the follow variables is a long because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long interval = 10000;           // interval at which to blink (milliseconds)
boolean data= LOW;
void setup(){
  Serial.begin(9600);
  Ethernet.begin(mac, ip); 
  pinMode(chlorinelvl, INPUT);
  

}

void loop(){
  
EthernetClient client = server.available();                 
unsigned long currentMillis = millis();
 
  if(currentMillis - previousMillis > interval) {
    // save the last time you blinked the LED 
    previousMillis = currentMillis;   

    // if the LED is off turn it on and vice-versa:
Serial.print("hi");
data= !data;
Serial.print(digitalRead(data));
    // set the LED with the ledState of the variable:
    if (data==HIGH){
          Serial.print("data working") ;
          sendGET();
          Serial.print(.027*analogRead(chlorinelvl));   
             if(chlorinedata>=0){
                 sendHELP();  }  
        }
  }
}


void sendGET() //client function to send/receie GET request data.
{
  if (client.connect(myserver, 3333)) {
    Serial.println("connected");
   client.print("GET http://localhost:3333/handler.php?watt=");
client.print(analogRead(chlorinelvl));
client.println(" HTTP/1.1");
 client.println("Host: 100.100.100.6");
client.println("Connection: close");
client.println();


  } 
  else {
    Serial.println("connection failed");
    Serial.println();
  }


  Serial.println();
  Serial.println("disconnecting.");
  Serial.println("==================");
  Serial.println();
  client.stop();

}
void sendHELP() //client function to send/receie GET request data.
{
  if (client.connect(myserver, 3333)) {
    Serial.println("connected");
   client.print("GET http://localhost/email.php?watt=");
client.print(.0195*analogRead(chlorinelvl));
client.println(" HTTP/1.1");
 client.println("Host: 100.100.100.6");
client.println("Connection: close");
client.println();


  } 
  else {
    Serial.println("connection failed");
    Serial.println();
  }


  Serial.println();
  Serial.println("disconnecting.");
  Serial.println("==================");
  Serial.println();
  client.stop();

}

I cut out things im not worried about right now.

   client.print("GET http://localhost:3333/handler.php?watt=");

The protocol type and server name are NOT part of the GET request. Nor is the port number.

j4cm:
Sorry, change to c string?

That is just a distractor when they don't know the real answer. Below is some simple client code you might tinker with to see if you can get a response from your server.

//zoomkat 4-04-12
//simple client test
//for use with IDE 1.0
//open serial monitor and send an e to test
//for use with W5100 based ethernet shields
//note that the below bug fix may be required
// http://code.google.com/p/arduino/issues/detail?id=605 
//the arduino lan IP address { 192, 168, 1, 102 } may need 
//to be modified modified to work with your router.

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

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
byte ip[] = { 192, 168, 1, 102 }; // ip in lan assigned to arduino
//byte gateway[] = { 192, 168, 1, 1 }; // internet access via router
//byte subnet[] = { 255, 255, 255, 0 }; //subnet mask
byte myserver[] = { 208, 104, 2, 86 }; // zoomkat web page server IP address
EthernetClient client;
//////////////////////

void setup(){

  Ethernet.begin(mac, ip);
  //Ethernet.begin(mac, ip, gateway, gateway, subnet);
  Serial.begin(9600); 
  Serial.println("Better client test 4/04/12"); // so I can keep track of what is loaded
  Serial.println("Send an e in serial monitor to test"); // what to do to test
}

void loop(){
  // check for serial input
  if (Serial.available() > 0) //if something in serial buffer
  {
    byte inChar; // sets inChar as a byte
    inChar = Serial.read(); //gets byte from buffer
    if(inChar == 'e') // checks to see byte is an e
    {
      sendGET(); // call sendGET function below when byte is an e
    }
  }  
} 

//////////////////////////

void sendGET() //client function to send/receive GET request data.
{
  if (client.connect(myserver, 80)) {  //starts client connection, checks for connection
    Serial.println("connected");
    client.println("GET /~shb/arduino.txt HTTP/1.0"); //download text
    client.println(); //end of get request
  } 
  else {
    Serial.println("connection failed"); //error message if no client connect
    Serial.println();
  }

  while(client.connected() && !client.available()) delay(1); //waits for data
  while (client.connected() || client.available()) { //connected or data available
    char c = client.read(); //gets byte from ethernet buffer
    Serial.print(c); //prints byte to serial monitor 
  }

  Serial.println();
  Serial.println("disconnecting.");
  Serial.println("==================");
  Serial.println();
  client.stop(); //stop client

}

Don't know if that's gonna help me with my failure to connect to server in my "If client" statement.

Are you certain your network settings are correct? Something seems strange here.

IPAddress ip(192,168,1,9); // ip in lan
IPAddress gateway(192,168,1,1); // internet access via router
IPAddress subnet(255,255,255,0); //subnet mask

// this is not reachable on the Arduino's localnet.
client.println("Host: 192.168.0.126");

edit: This is not correct:

  Ethernet.begin(mac, ip, subnet, gateway); 
  // it should be
  Ethernet.begin(mac, ip, gateway, gateway, subnet);

What version of the IDE are you using?

Sorry, change to c string?

No, changing the Ethernet.begin() call.

That is just a distractor when they don't know the real answer.

You're the only one of the frequent posters being ignorant to that problem. I didn't tell the OP that the String class is causing it's described problems but I think I'm allowed to point him to general problems caused by poor implementations (of the String class) and bugs in the IDE (in the de-allocation of dynamic memory).

well, im connecting to a switching hub and it doesnt have a default gateway, it only has mac and subnet. I'm trying to connect to a PC with IP of 100.100.100.6 on port 3333 to apache(I changed the ports on apache to listen to 3333 and 3444).

j4cm:
well, im connecting to a switching hub and it doesnt have a default gateway, it only has mac and subnet. I'm trying to connect to a PC with IP of 100.100.100.6 on port 3333 to apache(I changed the ports on apache to listen to 3333 and 3444).

Then that is the problem. If you have no gateway, you are restricted to your localnet. You will only be able to access devices with 192.168.1.x ip addresses. Any ip address outside that range (like 100.100.100.6) will be unreachable.

You don't have a router between the switch and the internet?

There isnt any internet and I only want to use localhost on the PC to collect data, so ardiuno cant only communicate with anything lower then 192. well that probably my main problem then. The PC is a scada for PLC and the PLC are on 100.100.100.x ip.

Then all your ip addresses must be within your localnet. According to the settings you posted, the localnet is 192.168.1.0/24. That means only ip addresses from 192.168.1.1 to 192.168.1.254 will be accessible. Keep all your devices within that range.

edit: or change your ethernet shield to a 100.100.100.x ip address. Then you can reach anything within the 100.100.100.x subnet.

IP addresses in the 100.100.100.x network are not in a private range of IPs but in the shared address space reserved for ISP's internal usage (for carrier grade NAT). Was that IP just a wildly guessed example or does your PC really have such an address? If yes, have you configured it or where does it originate from?