TelnetClient example

How is the TelnetClient example different from the WebClient example? I don't see where telnet comes into place in the TelnetClient example. I have all other ethernet examples working. I believe I'm stuck figuring out this line of code:

// Initialize the EthernetClient library with the IP address and port of the server 
// that you want to connect to
// port 23 is default for telnet; if you're using Processing's ChatServer, use  port 10002):

In the comments it appears to be 2 separate statements. We either initialize client to connect to google on port 80 OR

Here is my entire code:

/*
  Telnet client
 
 This sketch connects to a a telnet server (http://www.google.com)
 using an Arduino Wiznet Ethernet shield.  You'll need a telnet server 
 to test this with.
 Processing's ChatServer example (part of the network library) works well, 
 running on port 10002. It can be found as part of the examples
 in the Processing application, available at 
 http://processing.org/
 
 Circuit:
 * Ethernet shield attached to pins 10, 11, 12, 13
 
 created 14 Sep 2010
 modified 9 Apr 2012
 by Tom Igoe
 
 */

#include <SPI.h>
#include <Ethernet.h>

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {0x90, 0xA2, 0xDA, 0x0D, 0x97, 0x39};
IPAddress ip(192,168,1,118);

// Enter the IP address of the server you're connecting to:
//google
IPAddress server(74,125,224,72); 

// Initialize the EthernetClient library with the IP address and port of the server 
// that you want to connect to
// port 23 is default for telnet; if you're using Processing's ChatServer, use  port 10002):
EthernetServer telnetServer(23);   // I ADDED
EthernetClient client;             // I ADDED

void setup() {
  // start the Ethernet connection:
  Ethernet.begin(mac, ip);
  
  // start listening for clients   // I ADDED
  telnetServer.begin();
 
  // Open serial communications and wait for port to open:
  Serial.begin(9600);

  // give the Ethernet shield a second to initialize:
  delay(1000);
  Serial.println("connecting...");

  // if you get a connection, report back via serial:
  if (client.connect(server, 80)) {
    Serial.println("connected");
  } 
  else {
    // if you didn't get a connection to the server:
    Serial.println("connection failed");
  }
}

void loop()
{
  // if there are incoming bytes available 
  // from the server, read them and print them:
  if (client.available()) {
    char c = client.read();
    Serial.print(c);
  }

  // as long as there are bytes in the serial queue,
  // read them and send them out the socket if it's open:
  while (Serial.available() > 0) {
    char inChar = Serial.read();
    if (client.connected()) {
      client.print(inChar); 
    }
  }

  // if the server's disconnected, stop the client:
  if (!client.connected()) {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();
    // do nothing:
    while(true);
  }
}

You want to establish a telnet session with Google? Does that really make sense?

You appear to be mixing code for a telnet server with code for a web server. Why?

What are you really trying to do?

EthernetServer telnetServer(23);

telnetServer.begin();

Wow this not a way to run both client and server on same machine.

I don't see where telnet comes into place in the TelnetClient example.

Actually now Serial Port your cmd line

while (Serial.available() > 0) {
    char inChar = Serial.read();
    if (client.connected()) {
      client.print(inChar); 
    }
  }

PaulS:
You want to establish a telnet session with Google? Does that really make sense?

You appear to be mixing code for a telnet server with code for a web server. Why?

What are you really trying to do?

What is the given TelnetClient example trying to do? I just want to run the example with the least changes, such as providing my mac address, ip, and server ip. Maybe there is something more that needs to be added after EthernetClient client; ??

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {  
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,1,177);

// Enter the IP address of the server you're connecting to:
IPAddress server(1,1,1,1); 

// Initialize the Ethernet client library
// with the IP address and port of the server 
// that you want to connect to (port 23 is default for telnet;
// if you're using Processing's ChatServer, use  port 10002):
EthernetClient client;

Given Example Provided by Arduino 1.0.1

/*
  Telnet client
 
 This sketch connects to a a telnet server (http://www.google.com)
 using an Arduino Wiznet Ethernet shield.  You'll need a telnet server 
 to test this with.
 Processing's ChatServer example (part of the network library) works well, 
 running on port 10002. It can be found as part of the examples
 in the Processing application, available at 
 http://processing.org/
 
 Circuit:
 * Ethernet shield attached to pins 10, 11, 12, 13
 
 created 14 Sep 2010
 modified 9 Apr 2012
 by Tom Igoe
 
 */

#include <SPI.h>
#include <Ethernet.h>

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {  
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,1,177);

// Enter the IP address of the server you're connecting to:
IPAddress server(1,1,1,1); 

// Initialize the Ethernet client library
// with the IP address and port of the server 
// that you want to connect to (port 23 is default for telnet;
// if you're using Processing's ChatServer, use  port 10002):
EthernetClient client;

void setup() {
  // start the Ethernet connection:
  Ethernet.begin(mac, ip);
 // Open serial communications and wait for port to open:
  Serial.begin(9600);
   while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }


  // give the Ethernet shield a second to initialize:
  delay(1000);
  Serial.println("connecting...");

  // if you get a connection, report back via serial:
  if (client.connect(server, 10002)) {
    Serial.println("connected");
  } 
  else {
    // if you didn't get a connection to the server:
    Serial.println("connection failed");
  }
}

void loop()
{
  // if there are incoming bytes available 
  // from the server, read them and print them:
  if (client.available()) {
    char c = client.read();
    Serial.print(c);
  }

  // as long as there are bytes in the serial queue,
  // read them and send them out the socket if it's open:
  while (Serial.available() > 0) {
    char inChar = Serial.read();
    if (client.connected()) {
      client.print(inChar); 
    }
  }

  // if the server's disconnected, stop the client:
  if (!client.connected()) {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();
    // do nothing:
    while(true);
  }
}

What is the given TelnetClient example trying to do?

It's establishing a connection to a telnet server. Google is NOT a telnet server.

Really, if you don't know what a telnet server is, or where one is running, then this example is not of use to you.

I'm trying to educate myself, not just brush things off as not important and overlook such examples. I know what telnet is. What is meant by telnet server? Like I mentioned I got thru all other ethernet examples. Why isn't this example using port 23 anywhere in the code? Why does this example have me specify both ip address and server address? Please provide example for both. :zipper_mouth_face:

I'm trying to educate myself, not just brush things off as not important and overlook such examples. Why does this example have me specify both ip and server?

All Ethernet client examples require that you specify your IP address (who you are) and the server (who you want to connect to).

A telnet server is one that is running a specific daemon, telnetd, that allows two computers to exchange unformatted data. Do you know such a machine? If not, skip this example and move on.

I'm trying to educate myself, not just brush things off as not important and overlook such examples. Why does this example have me specify both ip and server?

That's good

/ Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {  
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,1,177);

firtst two comment lines explaining the purpose of IP and Mac. Actually first you need to understand what is telnet?

Before I move on, please describe a real-world scenario for this TelnetClient example. how does one get into telnet server running daemon? I take it the server(,,,) in the code would be the ip of the telnet server?

Why isn't this example using port 23 anywhere in the code?

Because it's written to talk to the chatserver Processing example sketch, on port 10002. You can change the port to 23, if you have a telnet server.

Before I move on, please describe a real-world scenario for this TelnetClient example

Before I move on, please describe a real-world scenario for this TelnetClient example.

If you have a telnet server that you want to exchange data with, you would know that server name, and probably have access to log in to it, and determine if it had a telnetd daemon running.

If you have a telnet server, you'd use a telnet client to talk to it.

Telnet is insecure. The cases where it is used are rapidly being phased out. Most cases that remain have both the client and the server on the same network, behind a firewall.

No one runs a public telnet server that you can connect to.

encryptor:
Before I move on, please describe a real-world scenario for this TelnetClient example. how would I know whether what is a telnet server running daemon?

If you have a Windows PC, you can enable the telnet server service.

Cybernetician:

Before I move on, please describe a real-world scenario for this TelnetClient example

Ardupower | Arduino Blog

Would I be correct to say that if I want to control devices remotely I would need a telnet server? The PC with ArduPower would be the client and the PC used to control from remote location would be the server?

Would I be correct to say that if I want to control devices remotely I would need a telnet server? The PC with ArduPower would be the client and the PC used to control from remote location would be the server?

No. There are other ways to have two computers talk to each other. telnetd monitors one port. httpd monitors a different port. snmpd and popd monitor other ports. Each of these daemons supports different protocols. Which protocol to use depends on the kind of information you want to exchange.

Telnet's only advantage is that is doesn't impose a protocol. That it doesn't is also it's biggest security risk. If a client can tell the server to do anything, security just flew out the window.

encryptor:
How is the TelnetClient example different from the WebClient example?

There are two differences:

Firstly, you need to connect to the right TCP port on the server. A server could be accepting connections for HTTP, FTP, telnet and so on at the same time, each service provided on a different TCP port. You need to know what port to connect to for the service you want. There are some standard conventions for the ports used by common services, for example telnet is typically on port 23 and HTTP is typically on port 80. You'll see that the telnet and web client examples connect to different TCP ports, and that's why.

Secondly you need to send and receive messages over the connected socket using the appropriate protocol. Telnet and HTTP both happen to be text-based protocols, meaning that the messages consist of printable text that could be entered via a keyboard or displayed on a screen. You'll notice that in the examples you mention, the Arduino doesn't process the messages itself but just passes through anything received from the network to the serial monitor, and vice versa. So the message processing is done by a person reading and typing messages in the serial monitor. You need to enter messages using the correct format for the service you connected to. For example, when you connect to an HTTP service you need to type in a valid HTTP request. Telnet is slightly easier to use in that the telnet protocol just provides a transparent connection between the client (i.e. your Arduino) and a shell processor on the Telnet server, so the commands you need to enter over a Telnet connection are the same ones you would enter to the command shell if you were connected locally.

To be fair to the OP, you can use the telnet client to connect to any server that requires some sort of character-based protocol. A MUD game, for example. In my case I am using a modified version to connect to my "garage door" server. You can fudge up some HTTP in a couple of lines:

    // if you get a connection, report back via serial:
    if (client.connect (garageDoorServer, garageDoorServerPort)) 
      {
      
      // request page
      client.println ("HTTP/1.0");
      client.println ("Host: 10.0.0.241");
      client.println ();

That requests the root page from 10.0.0.241. It then waits for data to become available. The server in question serves up "plain text" about whether or not the door is open.

As for why you would want a telnet server, I could have replaced the HTTP server with a telnet server, which, once something connects to it, replies "door open" or "door closed" to similar effect. However the advantage of an HTTP server in my case was that I could check the garage door status from any Mac/PC/Linux box/smart phone that had a web browser on it.