I've had to create a client that takes requests from sockets and send them to our telnet legacy server and return the servers response. It's worked great until we recently realized that some of the commands I send are getting cut off at 1955 characters. The way everything is set up is that I send the server 1 line commands and it sends back 1 line responses. I can't figure out a way to send longer commands via DataOutputStream through the TelnetClient. Am I doing this wrong or is this just a limitation of one of these objects?
public void connect() {
try {
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);
try {
tc.addOptionHandler(ttopt);
tc.addOptionHandler(echoopt);
tc.addOptionHandler(gaopt);
} catch (InvalidTelnetOptionException e) {
ServerConsole.log(e.getMessage());
}
tc.connect("192.168.1.8", 2010);
in = new DataInputStream(tc.getInputStream());
out = new DataOutputStream(tc.getOutputStream());
login();
} catch (IOException ex) {
ServerConsole.log(ex.getMessage());
}
}
//This is a watered down version of my write method just to show the basic idea of whats happening
public String write(String command) {
String finalCommand = command+"\n";
byte[] comm = finalCommand.getBytes();
out.write(comm);
out.flush();
response = in.readLine();
response = in.readLine();
return response;
}
After some tinkering I don't think this is a client-side problem at all. I took your client code and wrote a test client based on it. I then wrote a (very) basic sample server which prints out incoming messages and returns an ACK.
Even pushing the request size to 100k didn't result in a truncated message from the client. You might want to take a closer look at how the server you are contacting handles incoming data.
public class TelnetClientTest {
public static String LOCALHOST = "127.0.0.1";
public static int PORT = 5003;
public static void main(String[] args) throws Exception {
TelnetClient 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);
try {
tc.addOptionHandler(ttopt);
tc.addOptionHandler(echoopt);
tc.addOptionHandler(gaopt);
} catch (InvalidTelnetOptionException e) {
e.printStackTrace();
}
tc.connect(LOCALHOST, PORT);
DataInputStream in = new DataInputStream(tc.getInputStream());
DataOutputStream out = new DataOutputStream(tc.getOutputStream());
StringBuilder sb = new StringBuilder();
for (int i=0; i<1000; i++) {
sb.append("0123456789");
}
sb.append("\n");
out.write(sb.toString().getBytes());
out.flush();
System.out.println(in.readLine());
tc.disconnect();
}
}
public class ServerSocketExample {
private static ServerSocket server;
private static int PORT = 5003;
public static void main(String[] args) throws Exception {
server = new ServerSocket(PORT);
while (true) {
System.out.println("Waiting for clients...");
Socket s = server.accept();
System.out.println("Client connected: " + s.getLocalAddress().getHostName());
BufferedReader reader = new BufferedReader(new InputStreamReader(s.getInputStream()));
String request = reader.readLine();
System.out.println("Client says ("+request.length()+"): " + request);
System.out.println("Sending ACK response\n");
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
writer.write("ACK\n");
writer.flush();
}
}
}