简体   繁体   English

WiFi上的套接字Android和Java在发送文件时冻结

[英]Socket Android with Java on WiFifreezing on sending file

i'm doing a server with Java and a client with Android.我正在做一个带有 Java 的服务器和一个带有 Android 的客户端。 Everything works fine, but when i try to receive the file on Android, the IO input freezes.一切正常,但是当我尝试在 Android 上接收文件时,IO 输入冻结。 I tried to do the same with a Java test app, and works fine.我尝试对 Java 测试应用程序执行相同的操作,并且工作正常。

My code on the client side:我在客户端的代码:

                int bytesRead;
                long current = 0;

                byte [] mybytearray = new byte[InfoPrograma.BUFFER_LENGTH];

                //Receive file
                InputStream is = socket.getInputStream();
                BufferedOutputStream bos = new BufferedOutputStream(
                    new FileOutputStream(file)
                );

                while (
                    (bytesRead = is.read(
                        mybytearray, 
                        0, 
                        mybytearray.length
                    )
                ) >= 0) {
                    current += bytesRead;
                    bos.write(mybytearray, 0 , bytesRead);
                }

                bos.flush();
                bos.close();

The server side code:服务器端代码:

                    File file = new File(fileStr);

                    byte[] buffer = new byte[InfoPrograma.BUFFER_LENGTH];

                    BufferedInputStream bis = new BufferedInputStream(
                        new FileInputStream(file)
                    );

                    OutputStream os = socket.getOutputStream();

                    long tmp = 0;

                    while ((count = bis.read(buffer)) > 0) {
                        os.write(buffer, 0, count);
                        tmp += count;

                        System.out.println(tmp);
                    }

                    os.flush();

BUFFER_LENGHT is 2000. If you need anything else, ask for it, please. BUFFER_LENGHT 是 2000。如果您需要其他任何东西,请提出要求。

Thank you.谢谢你。

Done it.完成了。 The only way i found is to send first the lenght of the file to the client, save it, and then do read() while (on my code) current < fileLength.我发现的唯一方法是首先将文件的长度发送到客户端,保存它,然后在(在我的代码上)current < fileLength.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM