英文:
how to stop socket to close automatically after sending files
问题
以下是服务器端的代码:
try {
OutputStream os = socket.getOutputStream();
DataOutputStream dos = new DataOutputStream(os);
dos.writeInt(files.size());
dos.flush();
for(int i = 0; i < files.size();i++){
dos.writeUTF(files.get(i).getName());
dos.flush();
}
int n = 0;
byte[] buf = new byte[4092];
for(int i = 0; i < files.size(); i++){
if (name == null){
dos.writeUTF(files.get(i).getName());
}else {
dos.writeUTF(filesName.get(i) + ".apk");
}
dos.writeLong(files.get(i).length());
FileInputStream fis = new FileInputStream(files.get(i));
while((n = fis.read(buf)) != -1){
dos.write(buf,0,n);
dos.flush();
}
}
dos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
以下是客户端的代码:
try {
socket = new Socket(dstAddress, dstPort);
bufferSize = socket.getReceiveBufferSize();
in = socket.getInputStream();
DataInputStream dis = new DataInputStream(in);
DataOutputStream dos = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream()));
int number = dis.readInt();
ArrayList<File> files = new ArrayList<File>(number);
for(int i = 0; i < number; i++){
File file = new File(dis.readUTF());
files.add(file);
}
int n = 0;
byte[] buf = new byte[4092];
for(int i = 0; i < files.size(); i++){
int fileeesizee = 0;
publishProgress(String.valueOf(0));
String filename = dis.readUTF();
long fileSize = dis.readLong();
recyclername.add(filename);
recyclersize.add(android.text.format.Formatter.formatFileSize(test2.this, fileSize));
recyclerimg.add(filename);
final File f = new File(Environment.getExternalStorageDirectory()+"/SharePlus/.data/"+filename);
FileOutputStream fos = new FileOutputStream(f);
while (fileSize > 0 && (n = dis.read(buf, 0, (int)Math.min(buf.length, fileSize))) != -1)
{
fos.write(buf,0,n);
fileSize -= n;
fileeesizee += n;
if (fileSize == 0){
}else{
publishProgress(""+(int)((fileeesizee * 100) / fileSize));
}
}
fos.close();
}
} catch (IOException e) {
e.printStackTrace();
Log.d("d", "1 catch = " + e.getMessage());
final String eMsg = "Something wrong: " + e.getMessage();
test2.this.runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(test2.this, eMsg, Toast.LENGTH_LONG).show();
}
});
} finally {
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
Log.d("d", "2 catch = " + e.getMessage());
e.printStackTrace();
}
}
}
请注意,代码中可能存在一些格式问题,如缺少导入语句等。
英文:
I am making an app which requires socket to send files from one to another Well i can successfully send and receive any file from one device to another but the problem is my socket connection closes itself automatically after file transfer. I also tried removing socket.close()
from both sides but it still closes itself
Here is the server's code
try {
OutputStream os = socket.getOutputStream();
DataOutputStream dos = new DataOutputStream(os);
dos.writeInt(files.size());
dos.flush();
for(int i = 0 ; i < files.size();i++){
dos.writeUTF(files.get(i).getName());
dos.flush();
}
int n = 0;
byte[]buf = new byte[4092];
for(int i =0; i < files.size(); i++){
if (name == null){
dos.writeUTF(files.get(i).getName());
}else {
dos.writeUTF(filesName.get(i) + ".apk");
}
dos.writeLong(files.get(i).length());
FileInputStream fis = new FileInputStream(files.get(i));
while((n =fis.read(buf)) != -1){
dos.write(buf,0,n);
dos.flush();
}
}
dos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
And Here is the Client's code
try {
socket = new Socket(dstAddress, dstPort);
bufferSize = socket.getReceiveBufferSize();
in = socket.getInputStream();
DataInputStream dis = new DataInputStream(in);
DataOutputStream dos = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream()));
int number = dis.readInt();
ArrayList<File>files = new ArrayList<File>(number);
for(int i = 0; i< number;i++){
File file = new File(dis.readUTF());
files.add(file);
}
int n = 0;
byte[]buf = new byte[4092];
for(int i = 0; i < files.size();i++){
int fileeesizee = 0;
publishProgress(String.valueOf(0));
String filename = dis.readUTF();
long fileSize = dis.readLong();
recyclername.add(filename);
recyclersize.add(android.text.format.Formatter.formatFileSize(test2.this, fileSize));
recyclerimg.add(filename);
final File f = new File(Environment.getExternalStorageDirectory()+"/SharePlus/.data/"+filename);
FileOutputStream fos = new FileOutputStream(f);
while (fileSize > 0 && (n = dis.read(buf, 0, (int)Math.min(buf.length, fileSize))) != -1)
{
fos.write(buf,0,n);
fileSize -= n;
fileeesizee+=n;
if (fileSize==0){
}else{
publishProgress(""+(int)((fileeesizee * 100) / fileSize));
}
}
fos.close();
}
} catch (IOException e) {
e.printStackTrace();
Log.d("d", "1 catch = " + e.getMessage());
final String eMsg = "Something wrong: " + e.getMessage();
test2.this.runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(test2.this, eMsg, Toast.LENGTH_LONG).show();
}
});
} finally {
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
Log.d("d", "2 catch = " + e.getMessage());
e.printStackTrace();
}
}
}
答案1
得分: 0
dos.close();
关闭流也会关闭套接字。
英文:
> dos.close();
Closing a stream closes the socket too.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论