aboutsummaryrefslogtreecommitdiff
path: root/libraries/Ethernet/examples/echo_server
diff options
context:
space:
mode:
authorDavid A. Mellis <d.mellis@arduino.cc>2008-07-30 14:47:36 +0000
committerDavid A. Mellis <d.mellis@arduino.cc>2008-07-30 14:47:36 +0000
commiteac2983285b4bc40250771fb5141aa63c76a9a90 (patch)
tree9f51fab1b562f589d7ea6ce3dda53037ff2c929c /libraries/Ethernet/examples/echo_server
parentf1a76708088659850b98a92779a2cb1aaf5793d8 (diff)
Adding ethernet library.
Diffstat (limited to 'libraries/Ethernet/examples/echo_server')
-rw-r--r--libraries/Ethernet/examples/echo_server/echo_server.pde53
1 files changed, 53 insertions, 0 deletions
diff --git a/libraries/Ethernet/examples/echo_server/echo_server.pde b/libraries/Ethernet/examples/echo_server/echo_server.pde
new file mode 100644
index 0000000..e16dcb9
--- /dev/null
+++ b/libraries/Ethernet/examples/echo_server/echo_server.pde
@@ -0,0 +1,53 @@
+/*
+ * Echo Server
+ *
+ * Echoes back the headers of the web request. Good for
+ * learning how the HTTP protocol works.
+ */
+
+#include <Ethernet.h>
+
+byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
+byte ip[] = { 10, 0, 0, 177 };
+
+Server server(80);
+
+void setup()
+{
+ Client client(255);
+ Ethernet.begin(mac, ip);
+ Serial.begin(9600);
+ server.begin();
+}
+
+void loop()
+{
+ char buf[512];
+ int i = 0;
+ Client client = server.available();
+ if (client) {
+ boolean previous_is_newline = false;
+ while (client.connected()) {
+ if (client.available()) {
+ char c = client.read();
+ if (c == '\n' && previous_is_newline) {
+ buf[i] = 0;
+ client.println("HTTP/1.1 200 OK");
+ client.println("Content-Type: text/html");
+ client.println();
+ client.println("<pre>");
+ client.println(buf);
+ client.println("</pre>");
+ break;
+ }
+ if (i < 511)
+ buf[i++] = c;
+ if (c == '\n')
+ previous_is_newline = true;
+ else if (c != '\r')
+ previous_is_newline = false;
+ }
+ }
+ client.stop();
+ }
+}