我必须创建一个客户端,从套接字接收请求并将它们发送到我们的telnet旧服务器并返回服务器响应。在我们最近意识到我发送的一些命令被截止到1955个字符之前,它工作得很好。设置所有内容的方式是我发送服务器1行命令,然后发回1行响应。我无法找到通过TelnetClient通过DataOutputStream发送更长命令的方法。我这样做错了还是这只是其中一个对象的限制?
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;
}
答案 0 :(得分:2)
我不知道这些对象,但为什么不在消息的开头添加一个caracter,这表示这是否是消息的结尾。因为发送一个巨大的字符串看起来不太安全。
例子:字符“E”表示END,字符“N”表示NotEnded。
因此,您的客户端可以继续接收消息并将其连接在一个大字符串中,而不会收到带有字符“E”的消息。
例: 收到......“NTheWorld ......” 创建一个字符串“strMessage”; 收到......“NBurnY昨天......” 将receivedMessage与strMessage连接起来; 收到......“NTodayApple ......” 将receivedMessage与strMessage连接起来; 收到......“ETheEnd” 字符串完成后,我执行下一步操作。
(抱歉拼写错误)
答案 1 :(得分:1)
经过一些修修补补后,我认为这根本不是客户端问题。我拿了你的客户端代码并根据它编写了一个测试客户端。然后我编写了一个(非常)基本的示例服务器,它打印出传入的消息并返回ACK。
即使将请求大小推送到100k也不会导致客户端发送截断的消息。您可能希望仔细查看您正在联系的服务器如何处理传入数据。
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();
}
}
}