BufferedReader/BufferedInputStream.readLine()
2016-05-22 13:13
357 查看
最新在学习socket写一个简单聊天室时,发现服务端能够输出,但是客户端总是收不到东西。很郁闷,debug后发现无法跳转出readline。于是百度之:
readLine()的实质(下面是从JDK源码摘出来的):
从上面看出,readLine()是调用了read(char[] cbuf, int off, int len) 来读取数据,后面再根据”/r”或”/n”来进行数据处理。
在Java I/O书上也说了:
public String readLine() throws IOException
This method returns a string that contains a line of text from a text file. /r, /n, and /r/n are assumed to be line breaks and are not included in the returned string. This method is often used when reading user input from System.in, since most platforms only send the user’s input to the running program after the user has typed a full line (that is, hit the Return key).
readLine() has the same problem with line ends that DataInputStream’s readLine() method has; that is, the potential to hang on a lone carriage return that ends the stream . This problem is especially acute on networked connections, where readLine() should never be used.
小结,使用readLine()一定要注意:
**1.读入的数据要注意有/r或/n或/r/n
2.没有数据时会阻塞,在数据流异常或断开时才会返回null
3.使用socket之类的数据流时,要避免使用readLine(),以免为了等待一个换行/回车符而一直阻塞**
readLine()的实质(下面是从JDK源码摘出来的):
String readLine(boolean ignoreLF) throws IOException { StringBuffer s = null; int startChar; synchronized (lock) { ensureOpen(); boolean omitLF = ignoreLF || skipLF; bufferLoop: for (;;) { if (nextChar >= nChars) fill(); //在此读数据 if (nextChar >= nChars) { /* EOF */ if (s != null && s.length() > 0) return s.toString(); else return null; } ......//more } private void fill() throws IOException { ....//more int n; do { n = in.read(cb, dst, cb.length - dst); //实质 } while (n == 0); if (n > 0) { nChars = dst + n; nextChar = dst; } }
从上面看出,readLine()是调用了read(char[] cbuf, int off, int len) 来读取数据,后面再根据”/r”或”/n”来进行数据处理。
在Java I/O书上也说了:
public String readLine() throws IOException
This method returns a string that contains a line of text from a text file. /r, /n, and /r/n are assumed to be line breaks and are not included in the returned string. This method is often used when reading user input from System.in, since most platforms only send the user’s input to the running program after the user has typed a full line (that is, hit the Return key).
readLine() has the same problem with line ends that DataInputStream’s readLine() method has; that is, the potential to hang on a lone carriage return that ends the stream . This problem is especially acute on networked connections, where readLine() should never be used.
小结,使用readLine()一定要注意:
**1.读入的数据要注意有/r或/n或/r/n
2.没有数据时会阻塞,在数据流异常或断开时才会返回null
3.使用socket之类的数据流时,要避免使用readLine(),以免为了等待一个换行/回车符而一直阻塞**
相关文章推荐
- java-模拟tomcat服务器
- Linux socket 初步
- java socket 注意的地方
- java socket 注意的地方
- C#基于socket模拟http请求的方法
- 简单的Ruby中的Socket编程教程
- Socket不能选择本地IP连接问题如何解决
- C#之Socket操作类实例解析
- 使用C#来编写一个异步的Socket服务器
- C#使用Socket快速判断数据库连接是否正常的方法
- 科学知识:理解socket
- Android聊天工具基于socket实现
- php与flash as3 socket通信传送文件实现代码
- 解决time_wait强制关闭socket
- C#使用Socket上传并保存图片的方法
- 深入php socket的讲解与实例分析
- Linux网络编程之UDP Socket程序示例
- Linux网络编程之socket文件传输示例
- filezilla Failed to create listen socket on port 21 for IPv4 解决办法
- php socket方式提交的post详解