root / arduino-1.0 / libraries / Ethernet / examples / TelnetClient / TelnetClient.ino @ 58d82c77
History | View | Annotate | Download (2.14 KB)
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 |
by Tom Igoe |
17 |
|
18 |
*/ |
19 |
|
20 |
#include <SPI.h> |
21 |
#include <Ethernet.h> |
22 |
|
23 |
// Enter a MAC address and IP address for your controller below. |
24 |
// The IP address will be dependent on your local network: |
25 |
byte mac[] = { |
26 |
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; |
27 |
IPAddress ip(192,168,1,177); |
28 |
|
29 |
// Enter the IP address of the server you're connecting to: |
30 |
IPAddress server(1,1,1,1); |
31 |
|
32 |
// Initialize the Ethernet client library |
33 |
// with the IP address and port of the server |
34 |
// that you want to connect to (port 23 is default for telnet; |
35 |
// if you're using Processing's ChatServer, use port 10002): |
36 |
EthernetClient client; |
37 |
|
38 |
void setup() { |
39 |
// start the Ethernet connection: |
40 |
Ethernet.begin(mac, ip); |
41 |
// start the serial library: |
42 |
Serial.begin(9600); |
43 |
// give the Ethernet shield a second to initialize: |
44 |
delay(1000); |
45 |
Serial.println("connecting..."); |
46 |
|
47 |
// if you get a connection, report back via serial: |
48 |
if (client.connect(server, 10002)) { |
49 |
Serial.println("connected"); |
50 |
} |
51 |
else { |
52 |
// if you didn't get a connection to the server: |
53 |
Serial.println("connection failed"); |
54 |
} |
55 |
} |
56 |
|
57 |
void loop() |
58 |
{ |
59 |
// if there are incoming bytes available |
60 |
// from the server, read them and print them: |
61 |
if (client.available()) { |
62 |
char c = client.read(); |
63 |
Serial.print(c); |
64 |
} |
65 |
|
66 |
// as long as there are bytes in the serial queue, |
67 |
// read them and send them out the socket if it's open: |
68 |
while (Serial.available() > 0) { |
69 |
char inChar = Serial.read(); |
70 |
if (client.connected()) { |
71 |
client.print(inChar); |
72 |
} |
73 |
} |
74 |
|
75 |
// if the server's disconnected, stop the client: |
76 |
if (!client.connected()) { |
77 |
Serial.println(); |
78 |
Serial.println("disconnecting."); |
79 |
client.stop(); |
80 |
// do nothing: |
81 |
while(true); |
82 |
} |
83 |
} |
84 |
|
85 |
|
86 |
|
87 |
|