Telnet and Raw Sockets

Three questions the main text raises in a single sentence and then moves past. Each one is worth unpacking properly, because each is a place where a plausible mental model is subtly wrong.

What is telnet?

Two things share the name: a protocol (1969, RFC 15/854, designed for remote terminal login) and a command-line tool that speaks it. For our purposes only one property matters:

Telnet opens a raw, bidirectional TCP connection to a host:port. Bytes you type go straight down the socket. Bytes the server sends come straight back to your terminal. No encryption, no message framing, no client-side logic.

   You type  ────────────► [ TCP socket ] ────────────►  Server
   Terminal  ◄────────────                ◄────────────  Server reply
             (raw bytes, both directions, no interpretation)

Why it works on Redis

$ telnet localhost 6379
Trying 127.0.0.1...
Connected to localhost.
SET foo bar
+OK
GET foo
$3
bar
INCR counter
:1
INCR counter
:2

You are literally typing the wire protocol by hand and reading raw server responses:

ReplyMeaning
+OK+ = simple string → the string OK
$3 then bar$ = bulk string of length 3 → the bytes bar
:1: = integer → 1

Contrast: why this doesn't work for most servers

ServerWire formatTelnet-able by hand?
RedisRESP (plaintext)✓ yes
Memcachedplaintext✓ yes
HTTP/1.1plaintext✓ yes (GET / HTTP/1.1)
SMTPplaintext✓ yes
Postgresbinary framed protocol✗ no
gRPCHTTP/2 + protobuf (binary)✗ no
HTTP/2, TLS anythingbinary / encrypted✗ no

Important caveats

  • Telnet is insecure. Everything is cleartext, including passwords. As a login protocol it has been dead for decades — SSH replaced it entirely.
  • Its only surviving use is exactly the Redis case: a quick "is this TCP port open, and what does the server say back?" debugging probe for plaintext protocols.
  • It's often not installed on modern machines. The go-to replacement is nc (netcat).
$ nc localhost 6379
PING
+PONG

Doing it in Go — "telnet" from scratch

Telnet is just a raw TCP socket. This reimplements exactly what it does: open a socket to Redis, write bytes, read bytes back — no Redis library, speaking RESP by hand.

func main() {
    // 1. Open a raw TCP connection — this is all `telnet localhost 6379` does.
    conn, err := net.DialTimeout("tcp", "localhost:6379", 5*time.Second)
    if err != nil {
        log.Fatalf("dial: %v", err)
    }
    defer conn.Close()

    reader := bufio.NewReader(conn)

    // 2. Send commands as raw bytes, using the "inline" form (space-separated
    //    words + CRLF) — the same thing you'd type into telnet.
    commands := []string{"SET foo bar", "GET foo", "INCR counter", "PING"}

    for _, cmd := range commands {
        if _, err := fmt.Fprintf(conn, "%s\r\n", cmd); err != nil {
            log.Fatalf("write: %v", err)
        }
        reply, err := readReply(reader) // 3. decode the raw RESP reply
        if err != nil {
            log.Fatalf("read: %v", err)
        }
        fmt.Printf("%-15s -> %s\n", cmd, reply)
    }
}

// readReply decodes the handful of RESP2 reply types we care about.
// Teaching-sized decoder, not a production one.
func readReply(r *bufio.Reader) (string, error) {
    prefix, err := r.ReadByte() // the first byte is the TYPE marker
    if err != nil {
        return "", err
    }
    line, err := r.ReadString('\n') // rest of the line, then trim CRLF
    if err != nil {
        return "", err
    }
    line = strings.TrimRight(line, "\r\n")

    switch prefix {
    case '+': // simple string:  +OK
        return "(simple) " + line, nil
    case '-': // error:          -ERR something
        return "(error) " + line, nil
    case ':': // integer:        :42
        return "(int) " + line, nil
    case '$': // bulk string:    $3\r\nbar\r\n
        if line == "-1" {
            return "(nil)", nil
        }
        data, err := r.ReadString('\n') // read the payload line
        if err != nil {
            return "", err
        }
        return "(bulk) " + strings.TrimRight(data, "\r\n"), nil
    default:
        return fmt.Sprintf("(unhandled type %q) %s", prefix, line), nil
    }
}
$ go run main.go
SET foo bar     -> (simple) OK
GET foo         -> (bulk) bar
INCR counter    -> (int) 1
INCR counter    -> (int) 2
PING            -> (simple) PONG