Take the 2-minute tour ×

i am new to Clojure, need a simple example to create connection to a IP on a given port(actually the ready-made system connect to this port via TELNET). Send some character to this connection, receive the response and display(interpret) the result.

Can someone show via a code, this telnetting stuff with the above connection. Thanks in advance.

share|improve this question
add comment

1 Answer

Your best bet is to use clojure's java interop to wrap an existing telnet client library

Apache commons-net would be a good first stop. There's an example (in java) of a telnet client which should be straightforward to port

e.g. Often you'll find that it's easier to use clojure java interop that code it in java!.

// from TelnetClientExample

tc = new TelnetClient();

TerminalTypeOptionHandler ttopt = new TerminalTypeOptionHandler("VT100", false, false, true, false);
EchoOptionHandler echoopt = new EchoOptionHandler(true, false, true, false);
SuppressGAOptionHandler gaopt = new SuppressGAOptionHandler(true, true, true, true);

Looks like this in clojure:

(ns telnet.core
  (:import [org.apache.commons.net.telnet TelnetClient TelnetNotificationHandler SimpleOptionHandler EchoOptionHandler TerminalTypeOptionHandler SuppressGAOptionHandler InvalidTelnetOptionException]))

(def tc 
  (doto (TelnetClient.)
    (.addOptionHandler (TerminalTypeOptionHandler. 
                           "VT100" false false true false))
    (.addOptionHandler (EchoOptionHandler. 
                           true false true false))
    (.addOptionHandler (SuppressGAOptionHandler. 
                           true true true))))
share|improve this answer
Can you elaborate it a bit more with connect(), send() and receive function. –  Avi May 4 '12 at 11:20
I've given you lots to go on, yes I could write the whole thing for you, but that's not the Stackoverflow way. If you write some code and have specific problems, create a new question and we'll take it from there. –  sw1nn May 4 '12 at 11:23
add comment

Your Answer


