Telnet
paket "telnet";
The telnet
package allows you to create a telnet client.
TelnetClient constructor
Use the TelnetClient constructor to instantiate a TelnetKlijent
object, that you will use for all telnet
communication.
ƒ TelnetKlijent() : objekat
- takes: no parameters
- returns:
TelnetKlijent
object - special cases: none
var klijent = TelnetKlijent();
TelnetKlijent
The TelnetKlijent
object has the following properties:
- uspostaviKonekciju
- zatvoriKonekciju
- izlazniTok
- ulazniTok
uspostaviKonekciju
The uspostaviKonekciju
function is used to establish a connection with a Telnet server.
ƒ uspostaviKonekciju(hostname: tekst, port: broj) : void (nedefinisano)
- takes:
- hostname - hostname of the server you’re connecting to. Use “localhost” if connecting to a local server. (
tekst
) - port - port number (
broj
)
- hostname - hostname of the server you’re connecting to. Use “localhost” if connecting to a local server. (
- returns: nothing
- special cases:
- Throws exception if connection could not be established
var klijent = TelnetKlijent(); probaj { klijent.uspostaviKonekciju("localhost", 23); } spasi { ispis("Could not connect!"); }
zatvoriKonekciju
The zatvoriKonekciju
function is used to end an established connection with a Telnet server.
ƒ zatvoriKonekciju() : void (nedefinisano)
- takes: no parameters
- returns: nothing
- special cases:
- Throws exception if connection could not be closed
klijent.zatvoriKonekciju();
izlazniTok
The izlazniTok
function returns the output stream of a Telnet client.
ƒ izlazniTok() : objekat
- takes: no parameters
- returns: output stream object
objekat
- special cases:
- Throws exception if output stream cannot be provided (no connection has been established or other problems)
var izlaz = klijent.izlazniTok();
ulazniTok
The ulazniTok
function returns the input stream of a Telnet client.
ƒ ulazniTok() : objekat
- takes: no parameters
- returns: input stream object
objekat
- special cases:
- Throws exception if output stream cannot be provided (no connection has been established or other problems)
var ulaz = klijent.ulazniTok();
Output Stream object
The output stream object, in simple terms, collects output bytes and sends them to some sink. In the context of Telnet, you will use it for sending messages towards the server.
posaljiBajt
The posaljiBajt
function sends a single byte down the output stream.
ƒ posaljiBajt(b: broj) : void (nedefinisano)
- takes: b: byte in number form (
broj
) - returns: nothing
- special cases:
- Throws exception if operation fails
izlaz.posaljiBajt(5);
posalji
The posalji
function sends a byte array down the output stream.
ƒ posalji(bytes: BajtNiz) : void (nedefinisano)
- takes: bytes: byte array to send (
BajtNiz
) - returns: nothing
- special cases:
- Throws exception if operation fails
izlaz.posalji("Hello World".bajti());
isprazni
The isprazni
function flushes the output stream.
ƒ isprazni() : void (nedefinisano)
- takes: no parameters
- returns: nothing
- special cases:
- Throws exception if operation fails
izlaz.posalji("Hello".bajti()); izlaz.posalji("World".bajti()); izlaz.isprazni();
Note that you will need to flush the output stream to actually send its contents.
zatvori
The zatvori
function closes the output stream.
ƒ zatvori() : void (nedefinisano)
- takes: no parameters
- returns: nothing
- special cases:
- Throws exception if operation fails
izlaz.zatvori();
pisac
The pisac
function returns the buffered writer object for this output stream.
ƒ pisac() : objekat
- takes: no parameters
- returns: buffered writer object (
objekat
) - special cases:
- Throws exception if operation fails
var writer = izlaz.pisac();
Input Stream Object
An Input Stream object is the opposite of an Output Stream object. It collects incoming bytes. In the context of Telnet, you will use it to read messages you get from the server.
zatvoriTok
The zatvoriTok
function closes the input stream.
ƒ zatvoriTok() : void (nedefinisano)
- takes: no parameters
- returns: nothing
- special cases:
- Throws exception if operation fails
izlaz.zatvoriTok();
ucitaj
The ucitaj
function reads a given number of bytes from the input stream.
ƒ ucitaj(n: broj) : BajtNiz
- takes:
- n - number of bytes to read
- returns: array of read bytes (
BajtNiz
) - special cases:
- Throws exception if operation fails
// Reads 1024 bytes var odgovor = ulaz.ucitaj(1024); ispis(odgovor.dekodiraj());
ucitajPreostaleBajte
The ucitajPreostaleBajte
function reads all remaining bytes from the input stream.
ƒ ucitajPreostaleBajte() : BajtNiz
- takes: no parameters
- returns: array of read bytes (
BajtNiz
) - special cases:
- Throws exception if operation fails
var odgovor = ulaz.ucitajPreostaleBajte(1024); ispis(odgovor.dekodiraj());
dostupnihBajtova
The dostupnihBajtova
function returns an estimated number of bytes that can be read (or skipped over) from this input stream
without blocking.
ƒ dostupnihBajtova() : broj
- takes: no parameters
- returns: number of bytes that can be read (
broj
) - special cases:
- Throws exception if operation fails
var available = ulaz.dostupnihBajtova();
preskoci
The preskoci
function skips over and discards a specified number of bytes of data from the input stream. It returns the
number of bytes actually skiped, since sometimes the number can be lower than the one specified in the arguments.
ƒ preskoci(n: broj) : broj
- takes:
- n - number of bytes to skip
- returns: actual number of skipped bytes (
broj
) - special cases:
- Throws exception if operation fails
var actuallySkipped = ulaz.preskoci(8);
citac
The citac
function returns the buffered reader object for this input stream.
ƒ citac() : objekat
- takes: no parameters
- returns: buffered reader object (
objekat
) - special cases:
- Throws exception if operation fails
var reader = klijent.ulazniTok.citac();