网络是有延迟的,当从网络上读取数据流时,一般都这样写:
int buflen;//需要读取的字节长度
int readed;//已经读取的字节长度
readed = 0;
while (readed < buflen) readed = dataIn.read(readByte,readed,buflen-readed)
:)
good luck!
int buflen;//需要读取的字节长度
int readed;//已经读取的字节长度
readed = 0;
while (readed < buflen) readed = dataIn.read(readByte,readed,buflen-readed)
:)
good luck!
解决方案 »
- 怎么使用Junit测试
- ibatis insert后怎么用selecyKey返回联合主键的值
- 大家有空帮忙看看了...求解,spring配置报的异常
- 关于<rich:modalPanel/>数据提交 各位前辈,师兄师姐们!近来帮忙看下:
- 寻找高手:tomcat启动很慢是什么问题
- 如何獲取tomcat的端口號?
- 求助:SQL 问题
- myeclipes中的两个问题!1、乱码 2、JSP文件头
- 怪怪怪,tomcat的中文问题(在线等)
- 请问jdom如何生成和解析soap格式的XML文件?
- 基础问题,关于持久层的疑惑
- 急切希望各位大哥不吝赐教!小弟初学hibernate 的问题(总报错:No persister for: hibernate.user )
public class ServerTest {
public static void main(String[] args) throws Exception {
ServerSocket ss = new ServerSocket(9919);
Socket sock = ss.accept();
InputStream in = sock.getInputStream();
OutputStream out = sock.getOutputStream();
for (int i = 0; i < 10; i++) {
out.write(1);
Thread.sleep(1000);
}
sock.close();
}
}public class ClientTest {
public static void main(String[] args) throws Exception {
Socket sock = new Socket("localhost", 9919);
InputStream in = sock.getInputStream();
byte[] c = new byte[10];
in.read(c, 0, 10);
for (int i = 0; i < c.length; i++) {
System.out.println(c[i]);
}
sock.close();
}
}
这个例子,你要是把Thread.sleep()去掉就对了,加上sleep来模拟网络延迟的话,客户端就要变成楼上的写法,比如
while (readed < 10) readed = in.read(c, readed, 10 - readed)