Pages: [1] 2   Go Down
Author Topic: TelnetClient example  (Read 961 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Sr. Member
****
Karma: 1
Posts: 273
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:

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:
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);
  }
}
« Last Edit: December 03, 2012, 06:19:27 am by encryptor » Logged

peace*&^

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48569
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Offline Offline
Full Member
***
Karma: 4
Posts: 187
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
EthernetServer telnetServer(23);
 
Code:
telnetServer.begin();

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


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

Actually now Serial Port your cmd line

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

From Idea To Invention

Offline Offline
Sr. Member
****
Karma: 1
Posts: 273
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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; ??

Code:
// 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
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[] = { 
  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);
  }
}
Logged

peace*&^

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48569
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.

Logged

Offline Offline
Sr. Member
****
Karma: 1
Posts: 273
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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. smiley-zipper
« Last Edit: December 03, 2012, 08:29:44 am by encryptor » Logged

peace*&^

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48569
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.
Logged

Offline Offline
Full Member
***
Karma: 4
Posts: 187
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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

Code:
/ 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?
Logged

From Idea To Invention

Offline Offline
Sr. Member
****
Karma: 1
Posts: 273
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
« Last Edit: December 03, 2012, 08:44:09 am by encryptor » Logged

peace*&^

Gosport, UK
Offline Offline
Faraday Member
**
Karma: 21
Posts: 3113
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.
Logged

Offline Offline
Full Member
***
Karma: 4
Posts: 187
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Before I move on, please describe a real-world scenario for this TelnetClient example
http://arduino.cc/blog/2012/11/16/ardupower/
Logged

From Idea To Invention

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48569
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.
Logged

Gosport, UK
Offline Offline
Faraday Member
**
Karma: 21
Posts: 3113
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Sr. Member
****
Karma: 1
Posts: 273
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Before I move on, please describe a real-world scenario for this TelnetClient example
http://arduino.cc/blog/2012/11/16/ardupower/

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?

 
Logged

peace*&^

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48569
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.
Logged

Pages: [1] 2   Go Up
Jump to: