【发布时间】:2016-05-31 04:06:09
【问题描述】:
我创建了一个向客户端 (Android) 发送文本文件的服务器,并且只有在连接超时时才获取该文件。
为什么首先会发生“连接超时”?而且,接收文件 (1MB) 大约需要 1 分钟。
服务器:
FileInputStream fis = new FileInputStream(
new File("123.txt");
BufferedInputStream bis = new BufferedInputStream(fis);
DataInputStream dis = new DataInputStream(bis);
byte[] mybytearray = new byte[8192];
OutputStream os;
try {
os = clientSocket.getOutputStream();
DataOutputStream dos = new DataOutputStream(os);
int read;
while ((read = dis.read(mybytearray)) > 0) {
dos.write(mybytearray, 0, read);
}
} catch (IOException e) {
e.printStackTrace();
}
客户:
InputStream in;
int Size = 0;
try {
Size = clientSocket.getReceiveBufferSize();
in = clientSocket.getInputStream();
DataInputStream dis = new DataInputStream(in);
byte[] buffer = new byte[Size];
int read;
while ((read = dis.read(buffer)) > 0) {
fos.write(buffer, 0, read);
}
} catch (IOException e) {
e.printStackTrace();
}
知道如何解决这个问题吗?
【问题讨论】:
-
您是否搜索过与您的错误信息匹配的关键字?
-
是的,我做到了,但亚历克斯找到了答案。谢谢你的回复。
-
您的意思是“读取超时”,而不是“连接超时”?
标签: java android file server client