Question

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:

os.write(myMessageString.getbytes());
Was it helpful?

Solution

You definitely don't want to use ObjectOutputStream - that will use binary serialization that your telnet server won't be expecting.

It would be best to create an 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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top