[136] | 1 | /*
|
---|
| 2 | Telnet client
|
---|
| 3 |
|
---|
| 4 | This sketch connects to a a telnet server (http://www.google.com)
|
---|
| 5 | using an Arduino Wiznet Ethernet shield. You'll need a telnet server
|
---|
| 6 | to test this with.
|
---|
| 7 | Processing's ChatServer example (part of the network library) works well,
|
---|
| 8 | running on port 10002. It can be found as part of the examples
|
---|
| 9 | in the Processing application, available at
|
---|
| 10 | http://processing.org/
|
---|
| 11 |
|
---|
| 12 | Circuit:
|
---|
| 13 | * Ethernet shield attached to pins 10, 11, 12, 13
|
---|
| 14 |
|
---|
| 15 | created 14 Sep 2010
|
---|
| 16 | modified 9 Apr 2012
|
---|
| 17 | by Tom Igoe
|
---|
| 18 |
|
---|
| 19 | */
|
---|
| 20 |
|
---|
| 21 | #include <SPI.h>
|
---|
| 22 | #include <Ethernet2.h>
|
---|
| 23 |
|
---|
| 24 | // Enter a MAC address and IP address for your controller below.
|
---|
| 25 | // The IP address will be dependent on your local network:
|
---|
| 26 | byte mac[] = {
|
---|
| 27 | 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
|
---|
| 28 | };
|
---|
| 29 | IPAddress ip(192, 168, 1, 177);
|
---|
| 30 |
|
---|
| 31 | // Enter the IP address of the server you're connecting to:
|
---|
| 32 | IPAddress server(1, 1, 1, 1);
|
---|
| 33 |
|
---|
| 34 | // Initialize the Ethernet client library
|
---|
| 35 | // with the IP address and port of the server
|
---|
| 36 | // that you want to connect to (port 23 is default for telnet;
|
---|
| 37 | // if you're using Processing's ChatServer, use port 10002):
|
---|
| 38 | EthernetClient client;
|
---|
| 39 |
|
---|
| 40 | void setup() {
|
---|
| 41 | // start the Ethernet connection:
|
---|
| 42 | Ethernet.begin(mac, ip);
|
---|
| 43 | // Open serial communications and wait for port to open:
|
---|
| 44 | Serial.begin(9600);
|
---|
| 45 | while (!Serial) {
|
---|
| 46 | ; // wait for serial port to connect. Needed for Leonardo only
|
---|
| 47 | }
|
---|
| 48 |
|
---|
| 49 |
|
---|
| 50 | // give the Ethernet shield a second to initialize:
|
---|
| 51 | delay(1000);
|
---|
| 52 | Serial.println("connecting...");
|
---|
| 53 |
|
---|
| 54 | // if you get a connection, report back via serial:
|
---|
| 55 | if (client.connect(server, 10002)) {
|
---|
| 56 | Serial.println("connected");
|
---|
| 57 | }
|
---|
| 58 | else {
|
---|
| 59 | // if you didn't get a connection to the server:
|
---|
| 60 | Serial.println("connection failed");
|
---|
| 61 | }
|
---|
| 62 | }
|
---|
| 63 |
|
---|
| 64 | void loop()
|
---|
| 65 | {
|
---|
| 66 | // if there are incoming bytes available
|
---|
| 67 | // from the server, read them and print them:
|
---|
| 68 | if (client.available()) {
|
---|
| 69 | char c = client.read();
|
---|
| 70 | Serial.print(c);
|
---|
| 71 | }
|
---|
| 72 |
|
---|
| 73 | // as long as there are bytes in the serial queue,
|
---|
| 74 | // read them and send them out the socket if it's open:
|
---|
| 75 | while (Serial.available() > 0) {
|
---|
| 76 | char inChar = Serial.read();
|
---|
| 77 | if (client.connected()) {
|
---|
| 78 | client.print(inChar);
|
---|
| 79 | }
|
---|
| 80 | }
|
---|
| 81 |
|
---|
| 82 | // if the server's disconnected, stop the client:
|
---|
| 83 | if (!client.connected()) {
|
---|
| 84 | Serial.println();
|
---|
| 85 | Serial.println("disconnecting.");
|
---|
| 86 | client.stop();
|
---|
| 87 | // do nothing:
|
---|
| 88 | while (true);
|
---|
| 89 | }
|
---|
| 90 | }
|
---|
| 91 |
|
---|
| 92 |
|
---|
| 93 |
|
---|
| 94 |
|
---|