问题描述
- 大神帮我看一下为什么我buf.readLine()会出错
-
请注意我注释的地方
客户端代码:
public void onClick(View v) {
// TODO Auto-generated method stubnew Thread(){
public void run(){
try {
client=new Socket("192.168.1.02",8080);
PrintStream out=new PrintStream(client.getOutputStream());
BufferedReader buf=new BufferedReader(new InputStreamReader(client.getInputStream()));
String s=ed.getText().toString();
out.println(s);
String s1=buf.readLine();//如果不把这里注释掉要发生10个中文字符服务器才接的到,注释了就没这个问题了,应该是这里出问题吗,我断点了下s1有字符串
buf.close();
out.close();
client.close();
//txt.setText(s1);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}}
}.start();
}
服务器端代码:
public server(Socket client){
this.client=client;
}
@Override
public void run() {
// TODO Auto-generated method stub
boolean f=true;
try {boolean bo=true;
PrintStream p=new PrintStream(client.getOutputStream());
BufferedReader buf=new BufferedReader(new InputStreamReader(client.getInputStream()));
String s=buf.readLine();
System.out.println("客户端打"+new String(s.getBytes("iso-8859-1"),"utf-8"));buf.close();
p.close();
client.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}}
解决方案
if (mReader.ready()) {
String data = mReader.readLine();
}