简体   繁体   English

如何使用java使用outputstream(Telnet客户端)编写字符串

[英]how to write string with outputstream(Telnet client) using java

i tried to use this code but when i start the connection it seems that the server doesn't receive the string(the command): 我试图使用此代码,但是当我开始连接时,服务器似乎未接收到字符串(命令):

public static void sendMessage(TelnetClient s, String myMessageString)
       throws IOException {
      OutputStream os = s.getOutputStream();
      ObjectOutputStream oos = new ObjectOutputStream(os);  
      oos.writeObject(myMessageString); 
      }

i tried also to use only the outputstream and to do the following thing: 我也尝试仅使用outputstream并执行以下操作:

os.write(myMessageString.getbytes());

You definitely don't want to use ObjectOutputStream - that will use binary serialization that your telnet server won't be expecting. 您绝对不希望使用ObjectOutputStream它将使用您的telnet服务器不会期望的二进制序列化。

It would be best to create an OutputStreamWriter : 最好创建一个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();

The flush call may well be what was missing before - depending on exactly what TelnetClient does, it may be buffering the data until you flush the stream. flush调用很可能是以前丢失的-取决于TelnetClient所做的工作,它可能会缓冲数据,直到刷新流为止。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM