Sending data

Top  Previous  Next

Once a connection has been established and option negotiation completed you can then proceed to send data to the telnet server. Sending data to the telnet server is accomplished using the TelnetOutputStream. The TelnetOutputStream is a filtered java.io.OutputStream that is capable of sending both low-level commands (e.g. option negotiation) and non filtered data such as commands that you would enter at a telnet shell prompt.

 

To send data you must first obtain the TelnetOutputStream that is associated with your telnet session. This can be accomplished by invoking the Telnet#getOutputStream method.

 

Example

 

// get OutputStream

OutputStream out = telnet.getOutputStream();

 

// command to perform directory listing

String command = "ls –al";

 

// send command to Telnet server

out.write(command.getBytes());

 

// write CRLF indicate end of command

out.write("\n".getBytes());

 

 

Note

 

In the last line of the example above a \n was sent after the ls –al command. This is required to let the server know that the client has finished sending data. If you do not send a \n then the server will continue blocking I/O and will not respond until a \n has been received. Depending on the server you are communicating with a \r\n may be required.

 

An alternative to manually sending the \r\n is to use the TelnetOutputStream#println method. The object returned when invoking the Telnet#getOutputStream method is of type java.io.OutputStream but may be cast to a TelnetOutputStream.





Home | Company | Products | Solutions | Purchase | Support | Services | Blog

© 2021 JSCAPE LLC