Android:使用 Socket 的 Telnet - 缺少最后一行
我正在尝试为 android 编写一些 MUD 客户端,但我遇到了服务器输出的问题。我无法让我的应用程序在控制台中显示最后一行(登录提示)。
try {
Socket socket = new Socket("studnia.mud.pl", 4004);
OutputStream out = socket.getOutputStream();
PrintWriter output = new PrintWriter(out);
output.println("Siema, 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();
}
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, 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();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
readLine()
不会返回登录提示行,因为该行尚未完成 - 输入登录名之前没有行终止字符。为了获取带有提示的部分行,不能使用readLine()
;尝试类似while (input.ready()) { int c = input.read(); ... }
或input.read(cbuf, 0, len)
。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 usereadLine()
; try something likewhile (input.ready()) { int c = input.read(); ... }
orinput.read(cbuf, 0, len)
.