客户端与服务器之间通信收不到信息——readLine()
2024-10-11 18:39:14
写服务器端和客户端之间通信,结果一直读取不到信息,在https://blog.csdn.net/yiluxiangqian7715/article/details/50173573
上找到了原因:使用BufferedReader的readLine()方法来读取信息,但是没有遇见行结束符“\n”,“\r”或“\r\n”还是会继续读取(即阻塞了),所以要想读取到信息,只需要在写入的时候加上行结束符。
BufferedReader的readLine方法源码如下:
/**
* Reads a line of text. A line is considered to be terminated by any one
* of a line feed ('\n'), a carriage return ('\r'), or a carriage return
* followed immediately by a linefeed.
*
* @return A String containing the contents of the line, not including
* any line-termination characters, or null if the end of the
* stream has been reached
*
* @exception IOException If an I/O error occurs
*
* @see java.nio.file.Files#readAllLines
*/
public String readLine() throws IOException {
return readLine(false);
}
类似的还有RandomAccessFile的readLine():
/**
* Reads the next line of text from this file. This method successively
* reads bytes from the file, starting at the current file pointer,
* until it reaches a line terminator or the end
* of the file. Each byte is converted into a character by taking the
* byte's value for the lower eight bits of the character and setting the
* high eight bits of the character to zero. This method does not,
* therefore, support the full Unicode character set.
*
* <p> A line of text is terminated by a carriage-return character
* ({@code '\u005Cr'}), a newline character ({@code '\u005Cn'}), a
* carriage-return character immediately followed by a newline character,
* or the end of the file. Line-terminating characters are discarded and
* are not included as part of the string returned.
*
* <p> This method blocks until a newline character is read, a carriage
* return and the byte following it are read (to see if it is a newline),
* the end of the file is reached, or an exception is thrown.
*
* @return the next line of text from this file, or null if end
* of file is encountered before even one byte is read.
* @exception IOException if an I/O error occurs.
*/ public final String readLine() throws IOException {
StringBuffer input = new StringBuffer();
int c = -1;
boolean eol = false; while (!eol) {
switch (c = read()) {
case -1:
case '\n':
eol = true;
break;
case '\r':
eol = true;
long cur = getFilePointer();
if ((read()) != '\n') {
seek(cur);
}
break;
default:
input.append((char)c);
break;
}
} if ((c == -1) && (input.length() == 0)) {
return null;
}
return input.toString();
}
最新文章
- 常见css垂直自适应布局(css解决方法)
- 【原创】Matlab.NET混合编程技巧之找出Matlab内置函数
- 粒子群优化算法-python实现
- 发现磁盘的shell
- toString&;&;equals方法
- 【转】Usage of sendBroadcast()
- 16. Linux 文件目录权限
- Java 内省机制
- 第一百零一节,JavaScript流程控制语句
- Oracle 数据库常用操作语句大全
- libevent中evmap实现(哈希表)
- Hive使用必知必会系列
- json 解析错误的问题
- PYTHON基础入门(内置函数、推导式)学习
- windows server 修改远程桌面连接端口号
- ARC下野指针 EXC_BAD_ACCESS错误
- strerror函数的总结【转】
- Selenium之ActionChains(一)
- Spring的AsyncHandlerInterceptor
- nginx location 配置详解 【转载,整理】
热门文章
- 论文速读(Yongchao Xu——【2018】TextField_Learning A Deep Direction Field for Irregular Scene Text)
- 大数据处理框架之Strom:DRPC
- ansible-2.1.0.0_module
- Linux开局配置注意事项
- linux执行jmeter脚本解决响应数据为空
- GOQTTemplate简单介绍
- Scrum Meeting 合集
- 【spotlight安装监控】
- Windows环境下利用anaconda3安装python版本的Xgboost
- 机器学习总结(二)bagging与随机森林