I have this code in the main class:
IOUtil.readWrite(telnet.getInputStream(), telnet.getOutputStream(),
System.in, System.out);
This works very well, since System.in gets the inputs from the user nad System.out prints all the outputs.
I was trying to change this, so instead of System.in could be another InputStream object that reads one line from a file each time asks for an input, and also System.out could be an Object that writes all the output to a file.
the IOUtil class is the following:
package examples;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import org.apache.commons.net.io.Util;
public final class IOUtil
{
public final static void readWrite(final InputStream remoteInput,
final OutputStream remoteOutput,
final InputStream localInput,
final OutputStream localOutput)
{
Thread reader, writer;
reader = new Thread()
{
public void run()
{
int ch;
try
{
while (!interrupted() && (ch = localInput.read()) != -1)
{
remoteOutput.write(ch);
remoteOutput.flush();
}
}
catch (IOException e)
{
//e.printStackTrace();
}
}
}
;
writer = new Thread()
{
public void run()
{
try
{
Util.copyStream(remoteInput, localOutput);
}
catch (IOException e)
{
e.printStackTrace();
System.exit(1);
}
}
};
writer.setPriority(Thread.currentThread().getPriority() + 1);
writer.start();
reader.setDaemon(true);
reader.start();
try
{
writer.join();
reader.interrupt();
}
catch (InterruptedException e)
{
}
}
For the replacement for System.out, simply use a FileOutputStream
.
For the replacement for System.in, you could use a FileInputStream
. This doesn't deliver input a line at a time, but I expect that the remote telnet server can handle type-ahead, so that shouldn't matter.
If type-ahead does matter, then you've got a difficult problem. The input side of your telnet client has to synchronize with the output side, and wait until the remote server / shell is expecting the next line before sending it.
IOUtil.readWrite
method (as written) doesn't do that. I had a brief look at the telnet protocol, and I could see nothing that says that the client has to send data a line at a time.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.