【问题标题】:how to stop socket to close automatically after sending files如何在发送文件后停止套接字自动关闭
【发布时间】:2020-08-29 06:23:01
【问题描述】:

我正在制作一个应用程序,它需要套接字将文件从一个设备发送到另一个设备我可以成功地将任何文件从一个设备发送和接收到另一个设备,但问题是我的 文件传输后套接字连接会自动关闭强>。我还尝试从两侧删除socket.close(),但它仍然会自行关闭

这是服务器的代码

            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();
                    }
                }
            }

【问题讨论】:

  • 请更新您的帖子。将文字服务器和/或客户端放在你说的地方这是我的发件人代码,这是接收者代码。
  • 关闭流会关闭底层套接字或流。所以你自己关闭了套接字。

标签: java android android-studio sockets


【解决方案1】:

dos.close();

关闭流也会关闭套接字。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-11
    • 2015-05-27
    相关资源
    最近更新 更多