source: arduino-1-6-7/trunk/fuentes/arduino-ide-amd64/libraries/Ethernet/examples/TelnetClient/TelnetClient.ino @ 46

Last change on this file since 46 was 46, checked in by jrpelegrina, 4 years ago

First release to Xenial

File size: 2.3 KB
Line 
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 <Ethernet.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:
26byte mac[] = {
27  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
28};
29IPAddress ip(192, 168, 1, 177);
30
31// Enter the IP address of the server you're connecting to:
32IPAddress 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):
38EthernetClient client;
39
40void 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 native USB port 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  } else {
58    // if you didn't get a connection to the server:
59    Serial.println("connection failed");
60  }
61}
62
63void loop() {
64  // if there are incoming bytes available
65  // from the server, read them and print them:
66  if (client.available()) {
67    char c = client.read();
68    Serial.print(c);
69  }
70
71  // as long as there are bytes in the serial queue,
72  // read them and send them out the socket if it's open:
73  while (Serial.available() > 0) {
74    char inChar = Serial.read();
75    if (client.connected()) {
76      client.print(inChar);
77    }
78  }
79
80  // if the server's disconnected, stop the client:
81  if (!client.connected()) {
82    Serial.println();
83    Serial.println("disconnecting.");
84    client.stop();
85    // do nothing:
86    while (true);
87  }
88}
89
90
91
92
Note: See TracBrowser for help on using the repository browser.