Android: Telnet using Socket - missing last line
I'm trying to write some MUD Client for android, and I run into problem with output from server. I can't get my app to show last line (Login prompt) in console..
try {
Socket socket = new Socket("studnia.mud.pl", 4004);
OutputStream out = socket.getOutputStream();
PrintWriter output = new PrintWriter(out);
output.println("Siema开发者_如何学JAVA, pisze klient mudowy pod androida wiec nie bijcie że testuje na studni. :(");
BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String line="1";
while(line!=null){
line = input.readLine();
Log.i("Socket", line);
}
socket.close();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
readLine()
does not return the login prompt line because this line is not yet complete - there is no line-termination character until the login name has been entered. In order to get the partial line with the prompt, you cannot use readLine()
; try something like while (input.ready()) { int c = input.read(); ... }
or input.read(cbuf, 0, len)
.
精彩评论