我尝试使用此代码,但当我启动连接时,服务器似乎没有收到字符串(命令):
public static void sendMessage(TelnetClient s, String myMessageString)
throws IOException {
OutputStream os = s.getOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(os);
oos.writeObject(myMessageString);
}
我还尝试仅使用输出流并执行以下操作:
os.write(myMessageString.getbytes());
最佳答案
您绝对不想使用 ObjectOutputStream
- 它将使用您的 telnet 服务器不期望的二进制序列化。
最好创建一个 OutputStreamWriter
:
// Adjust the encoding to whatever you want, but you need to decide...
Writer writer = new OutputStreamWriter(s.getOutputStream(), "UTF-8");
writer.write(myMessageString);
writer.flush();
flush
调用很可能是之前缺失的 - 具体取决于 TelnetClient
的作用,它可能会缓冲数据,直到您刷新流。
关于java - 如何使用java通过输出流(Telnet客户端)编写字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20813204/