0

I'm trying to exchange files between two devices using android wifi direct. Any of the device can send and receive files. After discovering and connecting the two devices,I can only successfully send and receive a file/files upon the first time calling send. I'm unable to send any file afterwards. In addition,transfer speed is very slow(it takes about three minutes to transfer a 10mb file)

    void startServer(){
    
        String intRoot = new FilesDisplayFragment().internalDirRoot;
        ExecutorService serverExecutor = newSingleThreadExecutor();
        serverExecutor.submit(new Runnable() {
            @Override
            public void run() {

                ServerSocket serverSocket = null;
                try {
                    serverSocket = new ServerSocket(8888);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                try {
                    socket = serverSocket.accept();
                } catch (IOException e) {
                    e.printStackTrace();
                }

                BufferedInputStream bis = null;
                try {
                    bis = new BufferedInputStream(socket.getInputStream());
                } catch (IOException e) {
                    e.printStackTrace();
                }
                DataInputStream dis = new DataInputStream(bis);

                int filesCount = 0;
                try {
                    filesCount = dis.readInt();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                File[] files = new File[filesCount];

                for(int i = 0; i < filesCount; i++)
                {
                    long fileLength = 0;
                    try {
                        fileLength = dis.readLong();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    String fileType = null;
                    try {
                        fileType = dis.readUTF();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    String fileName = null;
                    try {
                        fileName = dis.readUTF();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }

                    if (fileType == "apk"){
                        File dirs = new File(intRoot + "RemoteView/" + "app");
                        if(!dirs.exists()) dirs.mkdirs();
                        files[i] = new File(dirs.toString() + "/" + fileName);
                    }


                    FileOutputStream fos = null;
                    try {
                        fos = new FileOutputStream(files[i]);
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    }
                    BufferedOutputStream bos = new BufferedOutputStream(fos);

                    for(int j = 0; j < fileLength; j++) {
                        try {
                            bos.write(bis.read());
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        });

    }

    void startClient(Intent intent){
        socket = new Socket();
        String intRoot = new FilesDisplayFragment().internalDirRoot;
        Parcelable parcelable = intent.getParcelableExtra("hostAdd");
        Parceler parceler = Parcels.unwrap(parcelable);
        String hostAdd = parceler.getGroupOwnerAddress().getHostAddress();
        ExecutorService clientExecutor = newSingleThreadExecutor();
        clientExecutor.submit(new Runnable() {
            @Override
            public void run() {
                try {
                    socket.connect(new InetSocketAddress(hostAdd, 8888), 5000);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                BufferedInputStream bis = null;
                try {
                    bis = new BufferedInputStream(socket.getInputStream());
                } catch (IOException e) {
                    e.printStackTrace();
                }
                DataInputStream dis = new DataInputStream(bis);

                int filesCount = 0;
                try {
                    filesCount = dis.readInt();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                File[] files = new File[filesCount];

                for(int i = 0; i < filesCount; i++)
                {
                    long fileLength = 0;
                    try {
                        fileLength = dis.readLong();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    String fileType = null;
                    try {
                        fileType = dis.readUTF();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    String fileName = null;
                    try {
                        fileName = dis.readUTF();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }

                    if (fileType == "apk"){
                        File dirs = new File(intRoot + "RemoteView/" + "app");
                        if(!dirs.exists()) dirs.mkdirs();
                        files[i] = new File(dirs.toString() + "/" + fileName);
                    }


                    FileOutputStream fos = null;
                    try {
                        fos = new FileOutputStream(files[i]);
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    }
                    BufferedOutputStream bos = new BufferedOutputStream(fos);

                    for(int j = 0; j < fileLength; j++) {
                        try {
                            bos.write(bis.read());
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }

            }
        });
    }

    void send(Intent intent){
        Parcelable parcelable = intent.getParcelableExtra("selected_wifi_files");
        Parceler parceler = Parcels.unwrap(parcelable);
        File[] files = parceler.getSelectedFilesWifi();
        ExecutorService sendExecutor = newSingleThreadExecutor();
        sendExecutor.submit(new Runnable() {
            @Override
            public void run() {
                If(bos == null){
                BufferedOutputStream bos = null;
                try {
                    bos = new BufferedOutputStream(socket.getOutputStream());
                } catch (IOException e) {
                    e.printStackTrace();
                }
                DataOutputStream dos = new DataOutputStream(bos);
BufferedOutputStream bos = null;
                try {
                    bos = new BufferedOutputStream(socket.getOutputStream());
                } catch (IOException e) {
                    e.printStackTrace();
                }
                DataOutputStream dos = new DataOutputStream(bos);
 
                }
               
                try {
                    dos.writeInt(files.length);
                } catch (IOException e) {
                    e.printStackTrace();
                }

                for(File file : files)
                {
                    long length = file.length();
                    try {
                        dos.writeLong(length);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }

                    String fileType = FilenameUtils.getExtension(String.valueOf(file));
                    try {
                        dos.writeUTF(fileType);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }

                    String name = file.getName();
                    try {
                        dos.writeUTF(name);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }

                    FileInputStream fis = null;
                    try {
                        fis = new FileInputStream(file);
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    }
                    BufferedInputStream bis = new BufferedInputStream(fis);

                    int theByte = 0;
                    while(true) {
                        try {
                            if (!((theByte = bis.read()) != -1)) break;
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                        try {
                            bos.write(theByte);
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        });
    }
8
  • All those catch blocks. Very good. But if there is a cath you continue as if nothing has happened. Instead you should stop and inform the user about the catch. Commented Mar 10, 2022 at 4:42
  • bos.write(bis.read()) Indeed that makes the transfer slow reading and writing one byte at the time. Commented Mar 10, 2022 at 4:46
  • It is inclear when you call send() and it is even unclear for what you would use that function. And if it was used by server or client Commented Mar 10, 2022 at 4:49
  • @blackapps the function can be used by both server and clients to send files Commented Mar 10, 2022 at 5:31
  • And when? You are not using it. Nor are you telling which code you use the second time. We hardly know what you do. You should exactly tell your scenario that leads to a problem. Commented Mar 10, 2022 at 5:35

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.