[英]java socket fileoutputstream can't close file
i've write a code for transferring file over sockets, the file is transfers properly but it isn't closed even after calling .close()
method. 我已经编写了用于通过套接字传输文件的代码,文件可以正确传输,但是即使调用
.close()
方法也无法关闭文件。
however the file closes after closing the "sockets", but i want to keep the connection open. 但是文件关闭“套接字”后关闭,但我想保持连接打开。
here the server sends the file to client 服务器将文件发送到客户端
SERVER CODE 服务器代码
public void sendFile(String sfileName) throws IOException{
try{
in = new FileInputStream(sfileName);
out = socket.getOutputStream();
transferData(in,out);
}
finally {
in.close();
in = null;
System.gc();
}
}
private void transferData(InputStream in, OutputStream out) throws IOException {
byte[] buf = new byte[8192];
int len = 0;
while(in.available()==0);
while ((len = in.read(buf)) != -1) {
out.write(buf, 0, len);
}
out.flush();
}
CLIENT CODE : 客户代码:
public void recieveFile(String rfileName) throws IOException{
try{
in = socket.getInputStream();
System.out.println("Reciever file : " + rfileName);
out = new FileOutputStream(rfileName);
transferData(in,out);
}
finally{
out.flush();
out.close();
out = null;
System.gc();
}
}
private void transferData(InputStream in, OutputStream out) throws IOException {
byte[] buf = new byte[8192];
int len = 0;
while(in.available()==0);
while ((len = in.read(buf)) != -1) {
out.write(buf, 0, len);
}
out.flush();
}
what is wrong with the code ? 代码有什么问题?
我认为您应该使用Socket.shutdownOutput()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.