DataInputStream read() 总是 return -1
DataInputStream read() always return -1
我在 java 中有一个客户端-服务器程序,其中客户端向服务器发送请求,服务器通过发送文件进行响应。
与服务器的连接正确,客户端发送的第一个请求被服务器正确解释。
此时服务器读入一个特定的文件,该文件似乎读得很好,因为写入发送了正确的字节数和字符串(通过打印字节数和字符串进行了测试)。
发送第一条消息后,客户端等待接收文件,但是一旦收到消息,读取 returns -1 就好像读取了 EOF 一样,我无法理解原因,如何解决?
发送文件时服务器上的方法
private void sendFile(Socket client, String path, DataOutputStream writer) {
File file = new File(path);
byte[] bytes = new byte[1024];
InputStream in = null;
try {
in = new FileInputStream(file);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
int count;
try {
while ((count = in.read(bytes)) > 0) {
System.out.println("nBytes " + count);
String tmp = new String(bytes);
System.out.println(tmp);
//i'm reading from a json file this string
//[{"username":"user"},{"score":0},{"friendList":["us1","us2"]},{"password":"password"}]
//and it is printed correctly
writer.write(bytes, 0, count);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
客户端方法
public void getFile (String username) {
// first of all connects correctly with the server and sends a message, the message is received correctly by the server, after it waits for a reply message
Socket socket = connect2server();
DataInputStream reader = null;
BufferedWriter writer = null;
try {
reader = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//send message to the server using writer
OutputStream out = null;
String file = username+".json";
try {
out = new FileOutputStream(file);
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
byte[] bytes = new byte[1024];
int count;
try {
while ((count = reader.read(bytes)) >= 0) {
System.out.println("nByte " + count);
out.write(bytes, 0, count);
}
// the client is waiting correctly on the read but as soon as it receives a message, the read returns -1 as if it had only read an EOF and not the whole message
System.out.println(count);
} catch (IOException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
try {
out.close();
reader.close();
writer.close();
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
在你的sendFile()
writer没有关闭。您需要 flush()
然后 close()
作者以保证创建输出。也许这会解决您的问题
关于 write
,您不是 closing/flushing 输出流,输出流又不会释放与该流关联的任何资源。您有两个选择:
- 关闭调用其 flush 方法的输出流,并
然后调用其底层输出流的关闭方法。
- 刷新输出流缓冲区。缓冲主要是为了提高
I/O
性能。
来自doc,read()
的总合同
Reads the next byte of data from the input stream. The value byte is returned as an int in the range 0 to 255. If no byte is available because the end of the stream has been reached, the value -1 is returned. This method blocks until input data is available, the end of the stream is detected, or an exception is thrown.
A subclass must provide an implementation of this method.
Returns:
下一个数据字节,如果到达流的末尾则为 -1。
我在 java 中有一个客户端-服务器程序,其中客户端向服务器发送请求,服务器通过发送文件进行响应。 与服务器的连接正确,客户端发送的第一个请求被服务器正确解释。
此时服务器读入一个特定的文件,该文件似乎读得很好,因为写入发送了正确的字节数和字符串(通过打印字节数和字符串进行了测试)。
发送第一条消息后,客户端等待接收文件,但是一旦收到消息,读取 returns -1 就好像读取了 EOF 一样,我无法理解原因,如何解决?
发送文件时服务器上的方法
private void sendFile(Socket client, String path, DataOutputStream writer) {
File file = new File(path);
byte[] bytes = new byte[1024];
InputStream in = null;
try {
in = new FileInputStream(file);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
int count;
try {
while ((count = in.read(bytes)) > 0) {
System.out.println("nBytes " + count);
String tmp = new String(bytes);
System.out.println(tmp);
//i'm reading from a json file this string
//[{"username":"user"},{"score":0},{"friendList":["us1","us2"]},{"password":"password"}]
//and it is printed correctly
writer.write(bytes, 0, count);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
客户端方法
public void getFile (String username) {
// first of all connects correctly with the server and sends a message, the message is received correctly by the server, after it waits for a reply message
Socket socket = connect2server();
DataInputStream reader = null;
BufferedWriter writer = null;
try {
reader = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//send message to the server using writer
OutputStream out = null;
String file = username+".json";
try {
out = new FileOutputStream(file);
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
byte[] bytes = new byte[1024];
int count;
try {
while ((count = reader.read(bytes)) >= 0) {
System.out.println("nByte " + count);
out.write(bytes, 0, count);
}
// the client is waiting correctly on the read but as soon as it receives a message, the read returns -1 as if it had only read an EOF and not the whole message
System.out.println(count);
} catch (IOException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
try {
out.close();
reader.close();
writer.close();
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
在你的sendFile()
writer没有关闭。您需要 flush()
然后 close()
作者以保证创建输出。也许这会解决您的问题
关于 write
,您不是 closing/flushing 输出流,输出流又不会释放与该流关联的任何资源。您有两个选择:
- 关闭调用其 flush 方法的输出流,并 然后调用其底层输出流的关闭方法。
- 刷新输出流缓冲区。缓冲主要是为了提高
I/O
性能。
来自doc,read()
Reads the next byte of data from the input stream. The value byte is returned as an int in the range 0 to 255. If no byte is available because the end of the stream has been reached, the value -1 is returned. This method blocks until input data is available, the end of the stream is detected, or an exception is thrown. A subclass must provide an implementation of this method.
Returns: 下一个数据字节,如果到达流的末尾则为 -1。