Not able to connect to Photon over Telnet

I am trying to connect to the Photon using telnet. I have port scanned the Photon and it shows that port 23 on the Photon is open.

    // socket parameters
int serverPort = 23;
char inbyte;
int counter = 0;
int simulate_temp = 21;


// start TCP servers
TCPServer server = TCPServer(serverPort);
TCPClient client;

char myIpString[24];
char outmsg[50];


enum tnetState {DISCONNECTED, CONNECTED};
int telnetState = DISCONNECTED;

unsigned long activity_timeout = 0;

void out(const char *s) {server.write( (const uint8_t*)s, strlen(s) );}

int LED = D7;                      // LED is connected to D7

void setup() 
{
    //Serial.begin(serialBaud); // open serial communications
    server.begin(); // begin listening for TCP connections

    IPAddress myIP = WiFi.localIP();
    sprintf(myIpString, "%d.%d.%d.%d", myIP[0], myIP[1], myIP[2], myIP[3]);
    Spark.variable("devIP", myIpString, STRING);
    pinMode(LED, OUTPUT);            // sets LED pin as output
}


void loop() 
{
    
    if (client.connected()) 
    {
        telnetState = CONNECTED;
        // echo all available bytes back to the client
        inbyte = 0;        //set to 0 so can detect when a char is received
        while (client.available()) 
        {    //Read incoming TCP data if available and copy to Serial port
            inbyte = client.read();
            if (inbyte == 'x')
            {
                digitalWrite(LED, 1);          // Flashes the LED
                delay(500);
                digitalWrite(LED, 0);          // Flashes the LED
                IPAddress myIP = WiFi.localIP();
                sprintf(outmsg, "%d.%d.%d.%d,%d,2,%d,3,4/e", myIP[0], myIP[1], myIP[2], myIP[3],counter,simulate_temp);
                counter = counter + 1;
                out(outmsg);
            }
            activity_timeout = millis();
        }

        if ( inbyte == 0 && (millis() - activity_timeout > 60000UL) ) 
        {    
            // No data received so check 1 minute inactivity timeout
            //Serial.println("client.stop");    //Check for new connection on next loop()
            client.stop();
            telnetState = DISCONNECTED;
        }

    }
    else 
    {
        // if no client is yet connected, check for a new connection
        if (telnetState == CONNECTED) 
        {        
            //If client WAS connected before, so stop() to close connection
            //Serial.println("client.stop");    //Check for new connection on next loop()
            client.stop();
            telnetState = DISCONNECTED;
        }
        client = server.available();        //Update TCP client status - "client" is declared globally
    }
}

What are you seeing? Note that telnet is not a bare byte stream - you’re likely to see some extraneous traffic where things like the window size (ie 80x24) is communicated to the server on connection open.

If you want a raw stream, try using netcat (nc).

2 Likes

I tried to connect to it with netcat but it still timed out.

I still can’t get it to work.

What exactly is not working? I ran your code on a Photon and when I telnet to the IP address and hit x then Return I get back the data that I would expect from looking at the code.

telnet 192.168.2.185
Trying 192.168.2.185...
Connected to 192.168.2.185.
Escape character is '^]'.
x
192.168.2.185,0,2,21,3,4/e

Mine just says trying 192.168.1.100 and they says it timed out. I know I have the right ip address.

telnet 192.168.1.100
Trying 192.168.1.100…
telnet: connect to address 192.168.1.100: Operation timed out
telnet: Unable to connect to remote host

What am I doing wrong? :confused: