如何阻止套接字在发送文件后自动关闭

huangapple go评论69阅读模式
英文:

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 &lt; files.size();i++){
dos.writeUTF(files.get(i).getName());
dos.flush();
}
int n = 0;
byte[]buf = new byte[4092];
for(int i =0; i &lt; files.size(); i++){
if (name == null){
dos.writeUTF(files.get(i).getName());
}else {
dos.writeUTF(filesName.get(i) + &quot;.apk&quot;);
}
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&lt;File&gt;files = new ArrayList&lt;File&gt;(number);
for(int i = 0; i&lt; number;i++){
File file = new File(dis.readUTF());
files.add(file);
}
int n = 0;
byte[]buf = new byte[4092];
for(int i = 0; i &lt; 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()+&quot;/SharePlus/.data/&quot;+filename);
FileOutputStream fos = new FileOutputStream(f);
while (fileSize &gt; 0 &amp;&amp; (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(&quot;&quot;+(int)((fileeesizee * 100) / fileSize));
}
}
fos.close();
}
} catch (IOException e) {
e.printStackTrace();
Log.d(&quot;d&quot;, &quot;1 catch = &quot; + e.getMessage());
final String eMsg = &quot;Something wrong: &quot; + 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(&quot;d&quot;, &quot;2 catch = &quot; + e.getMessage());
e.printStackTrace();
}
}
}

答案1

得分: 0

dos.close();

关闭流也会关闭套接字。

英文:

> dos.close();

Closing a stream closes the socket too.

huangapple
  • 本文由 发表于 2020年8月29日 14:23:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/63644088.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定