BufferedInputStream bis = new BufferedInputStream(socket.getInputStream());
BufferedReader in = new BufferedReader(new InputStreamReader(bis));
String line = in.readLine();
public String readLine(InputStream inputStream) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int b;
while ((b = inputStream.read()) != -1) {
if (b == '\r') {
continue;
}
if (b == '\n') {
break;
}
baos.write(b);
}
return baos.toString("UTF-8");
}
注意,如果使用第二种方法,需要注意数据包的长度和分段读取的问题。