Sunday, March 15, 2009

Telnet Client part 2 - using Apache net implementation...

I have shown Telnet client using socket programming which is my own logic.But Apache has already done this for us in their Net package which is freely available to download.So we can use that org.apache.commons.net.telnet.TelnetClient class for using telnet connection.

Its a very simple class.You can download its source and can figure out what they have done. Though i will show simple usage for it.
TelnetClient tc = new TelnetClient();
tc.connect("localhost", 23);
Then you can register a listener thread to get response from the telnet connection shown above. For full examples, check out java2s

Its nothing new, but a good implementation what we understood so far.
If you want totally command prompt like session for telnet, then you must wait for response from execution of the given command.Then only the next command will be sent over, as we do in DOS/Terminal.So for that we must have a logic which check the input stream and receives response.