Go Down

Topic: Problem with Hello World EthernetServer (Read 354 times) previous topic - next topic

fire1

Hi

I'm trying to do a Hello World Server but my problem is that I don't know the specific problem but I think it's start after void loop() , I need to figure out the problem .

Code: [Select]
#include <Ethernet.h>
#include <SPI.h>

byte mac[] = { 0x55 , 0x66 , 0x56 , 0x90 , 0xEE , 0x70 };
byte ip[] = { 192 , 168 , 0, 9 };
byte subnet[] = { 255 , 255 , 255 , 0 };
byte gateway[] = { 192 , 168 , 0 , 1 };
EthernetServer server(84);

void setup(){
  Serial.begin(9600);
  Ethernet.begin(mac,ip,gateway,subnet);
  server.begin();
  Serial.println(Ethernet.localIP());
  Serial.println("Server test1");
}
void loop(){
  // listen for incoming clients
  EthernetClient client = server.available();
  if (client) {
    Serial.println("new client");
    // an http request ends with a blank line
    boolean CNT = true;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        Serial.write(c);
      }
        if( c =='\n' && CNT){
          client.println("HTTP/1.1 204 OK");
          client.println("Content-Type:text/html");
          client.println("<!DOCTYPE HTML>");
          client.println("<html>");
          client.println("<body>");
          client.println("<h1>Hello World</h1>");
          client.println("</body>");
          client.println("</html>");
       
        }
        client.stop();
    }
   
    }
  }
}
         
   


best ,

Erdin

Could you try the example webserver, http://arduino.cc/en/Tutorial/WebServer

If that is okay, you could return to your sketch and try to find the differences.
Do you open the 'serial monitor' to see the messages ?
Which Arduino board and which shield are you using ?

PaulS

Quote
I need to figure out the problem .

But, you don't even know what the problem is.

Quote
I don't know the specific problem


How do you expect us to help you, when you are the one with the hardware, the serial monitor data, and the client, but, you haven't shared any output or observations?

Nick Gammon

Quote

Code: [Select]

          client.println("HTTP/1.1 204 OK");
          client.println("Content-Type:text/html");
          client.println("<!DOCTYPE HTML>");
          client.println("<html>");
          client.println("<body>");
          client.println("<h1>Hello World</h1>");
          client.println("</body>");
          client.println("</html>");



There should be a blank line (sent) before the DOCTYPE line. That separates the HTTP header from the sent HTML.

spatula


Go Up