aboutsummaryrefslogtreecommitdiff
path: root/libraries/Ethernet/examples
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
parentf1a76708088659850b98a92779a2cb1aaf5793d8 (diff)
Adding ethernet library.
Diffstat (limited to 'libraries/Ethernet/examples')
-rw-r--r--libraries/Ethernet/examples/chat_server/chat_server.pde34
-rw-r--r--libraries/Ethernet/examples/echo_server/echo_server.pde53
-rw-r--r--libraries/Ethernet/examples/web_client/web_client.pde41
3 files changed, 128 insertions, 0 deletions
diff --git a/libraries/Ethernet/examples/chat_server/chat_server.pde b/libraries/Ethernet/examples/chat_server/chat_server.pde
new file mode 100644
index 0000000..825d2f8
--- /dev/null
+++ b/libraries/Ethernet/examples/chat_server/chat_server.pde
@@ -0,0 +1,34 @@
+/*
+ * Chat Server
+ *
+ * A simple server that distributes any incoming messages to all
+ * connected clients. To use telnet to 10.0.0.177 and type!
+ */
+
+#include <Ethernet.h>
+
+// network configuration. gateway and subnet are optional.
+byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
+byte ip[] = { 10, 0, 0, 177 };
+byte gateway[] = { 10, 0, 0, 1 };
+byte subnet[] = { 255, 255, 0, 0 };
+
+// telnet defaults to port 23
+Server server(23);
+
+void setup()
+{
+ // initialize the ethernet device
+ Ethernet.begin(mac, ip, gateway, subnet);
+
+ // start listening for clients
+ server.begin();
+}
+
+void loop()
+{
+ Client client = server.available();
+ if (client) {
+ server.write(client.read());
+ }
+}
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();
+ }
+}
diff --git a/libraries/Ethernet/examples/web_client/web_client.pde b/libraries/Ethernet/examples/web_client/web_client.pde
new file mode 100644
index 0000000..74a6094
--- /dev/null
+++ b/libraries/Ethernet/examples/web_client/web_client.pde
@@ -0,0 +1,41 @@
+#include <Ethernet.h>
+
+byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
+byte ip[] = { 10, 0, 0, 177 };
+byte server[] = { 64, 233, 187, 99 }; // Google
+
+Client client(server, 80);
+
+void setup()
+{
+ Ethernet.begin(mac, ip);
+ Serial.begin(9600);
+
+ delay(1000);
+
+ Serial.println("connecting...");
+
+ if (client.connect()) {
+ Serial.println("connected");
+ client.println("GET /search?q=arduino HTTP/1.0");
+ client.println();
+ } else {
+ Serial.println("connection failed");
+ }
+}
+
+void loop()
+{
+ if (client.available()) {
+ char c = client.read();
+ Serial.print(c);
+ }
+
+ if (!client.connected()) {
+ Serial.println();
+ Serial.println("disconnecting.");
+ client.stop();
+ for(;;)
+ ;
+ }
+}