0

我想在两个 android 设备之间使用 Wi-Fi 热点 IP 地址和 MAC 地址在 Socket 连接上传输文件。我能够传输小于 1 KB 大小但无法发送其他扩展文件和更大大小的文本文件插座。以下是发件人端的代码:-

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

这是接收端的代码:-

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

我想传输更大尺寸的文件,如 mp3 文件,但它只在接收器端创建 1Kb 大小的文件,而不是 2.1 MB 的确切大小。请帮助我在这个实现中哪里错了。

4

1 回答 1

0

把这个放在一起,但还没有测试过。这应该给出一些提示

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

您可以选择像我所做的那样使用 try-catch 包装资源,或者您可以选择不这样做。考虑相应地调整缓冲区大小。考虑这个

请尝试一下。

于 2016-12-13T10:48:13.610 回答