0

I want to transfer a file on Socket connection using Wi-Fi Hotspot IP address and MAC address between two android devices.I am able to transfer a text file with less than 1 KB size but unable to send other extension files and of bigger size using socket. Below is the code for Sender side:-

            Socket socket = null;
            File file = new File(
                    Environment.getExternalStorageDirectory(),
                    "test.mp3");

            byte[] bytes = new byte[(int) file.length()];
            BufferedInputStream bis;
            try {
                socket = new Socket(dstAddress, dstPort);
                bis = new BufferedInputStream(new FileInputStream(file));
                bis.read(bytes, 0, bytes.length);
                OutputStream os = socket.getOutputStream();
                os.write(bytes, 0, bytes.length);
                os.flush();
                if (socket != null) {
                    socket.close();
                }

                final String sentMsg = "File Sent.....";
                ((Activity)context_con).runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        Toast.makeText(context_con,
                                sentMsg,
                                Toast.LENGTH_LONG).show();
                    }});


            }catch (ConnectException e) {
                e.printStackTrace();
            }
            catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (socket != null) {
                        socket.close();
                    }
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }

This is the code for Receiver End:-

           try {
            File file = new File(
                    Environment.getExternalStorageDirectory(),
                    "test.mp3");

            byte[] bytes = new byte[1024];
            InputStream is = socket.getInputStream();
            FileOutputStream fos = new FileOutputStream(file);
            BufferedOutputStream bos = new BufferedOutputStream(fos);
            int bytesRead = is.read(bytes, 0, bytes.length);
            bos.write(bytes, 0, bytesRead);
            bos.close();
            socket.close();

            final String sentMsg = "File Received...";
            Main.this.runOnUiThread(new Runnable() {

                @Override
                public void run() {
                    Toast.makeText(Main.this,
                            sentMsg,
                            Toast.LENGTH_LONG).show();
                }});

        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            try {
                socket.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

I want to transfer bigger size files like mp3 file but it only creating 1Kb size file on receiver end not with the exact size which is 2.1 MB. Please help me where I am wrong in this implementation.

  • Have you considered compressing them and then send? – Zuko Dec 13 '16 at 10:30
  • Take a look at this [this](http://www.helloandroid.com/tutorials/simple-connection-example-part-ii-tcp-communication) – Zuko Dec 13 '16 at 10:32
  • I am not compressing the files.. – Vikas Panwar Dec 13 '16 at 10:33
  • You did not tell what kind of errors you got. – greenapps Dec 13 '16 at 12:44
  • `bis = new BufferedInputStream(new FileInputStream(file)); bis.read(bytes, 0, bytes.length);`. It is not good practice to load the whole file in memory. Just declare a small buffer and in a loop read chuncks from the inputstream and then write them to the outputstream. Read them just as the server does. – greenapps Dec 13 '16 at 12:47

1 Answers1

0

Put together this but haven't tested it. This should give some hints

 //Server side snippet
 public void server(int port) throws IOException {
    try (ServerSocket serverSocket = new ServerSocket(port); Socket socket = serverSocket.accept()) {
        try (InputStream in = socket.getInputStream(); OutputStream out = new FileOutputStream("test.mp3")) {
            byte[] bytes = new byte[2 * 1024];

            int count;
            while ((count = in.read(bytes)) > 0) {
                out.write(bytes, 0, count);
            }
        }
    }
}

 //client side
 public static void client(String dstAddress, int dstPort) throws IOException { 
    try (Socket socket = new Socket(dstAddress, dstPort)) {
        File file = new File(Environment.getExternalStorageDirectory(), "test.mp3");

        // Get the size of the file
        long length = file.length();
        if (length > 0) {
            byte[] bytes = new byte[2 * 1024];
            InputStream in = new FileInputStream(file);
            OutputStream out = socket.getOutputStream();

            int count;
            while ((count = in.read(bytes)) > 0) {
                out.write(bytes, 0, count);
            }
            out.close();
            in.close();
        }
    }
}

You could choose to wrap the resources with try-catch as i've done or you can choose not to. Consider adjusting the buffer size accordingly. Consider this

Please try that.

Zuko
  • 2,764
  • 30
  • 30
  • Thanks Olu , now I am able to send bigger size files as well :- – Vikas Panwar Dec 13 '16 at 11:05
  • I Used this code socket = new Socket(dstAddress, dstPort); File file = new File( Environment.getExternalStorageDirectory(),"test.mp3"); byte[] bytes = new byte[(int) file.length()]; BufferedInputStream bis; bis = new BufferedInputStream(new FileInputStream(file)); bis.read(bytes, 0, bytes.length); ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream()); oos.writeObject(bytes); oos.flush(); – Vikas Panwar Dec 13 '16 at 11:06
  • Well, you can always adjust the buffer size if you run out of memory – Zuko Dec 13 '16 at 11:10
  • Also if this answer helped you, consider closing the question – Zuko Dec 13 '16 at 11:14