android–UDP数据包(通过WiFi Direct)永远不会到达

android–UDP数据包(通过WiFi Direct)永远不会到达,第1张

概述那些阅读我之前的问题的人可能已经知道这一点:我目前正试图抓住整个WiFiDirect的东西.到目前为止我所做的就是创建连接.我想要做的是通过UDP将数据包发送到groupOwner(其IP当然是已知的)–但它似乎每次都迷失方向.这是我的一些代码,我有一个接收文件的服务和一个在后台提交它们

那些阅读我之前的问题的人可能已经知道这一点:我目前正试图抓住整个WiFi Direct的东西.到目前为止我所做的就是创建连接.
我想要做的是通过UDP将数据包发送到groupOwner(其IP当然是已知的) – 但它似乎每次都迷失方向.

这是我的一些代码,我有一个接收文件的服务和一个在后台提交它们的intentservice:

fileReceiverService.java

    @OverrIDe    public voID run() {        Log.d(TAG, "Thread starting...");        try {            app.log("opening UDP socket to receive files.");            DatagramSocket socket = new DatagramSocket(PORT);            app.log("Socket open!");            socket.setSoTimeout(5000);            app.log("Waiting for packet..");            while (isRunning && !isInterrupted()) {                DatagramPacket packet = new DatagramPacket(                        new byte[WiFiPacket.PACKET_SIZE],                        WiFiPacket.PACKET_SIZE);                try {                    socket.receive(packet);                    app.log("Received " + packet.getLength()                            + " bytes, trying to parse!");                    parsePacket(packet);                } catch (SocketTimeoutException e) {                } catch (Exception e) {                    app.log("Something went wrong: " + e.getMessage());                    e.printstacktrace();                }            }            socket.close();            app.log("Closing UDP socket");        } catch (Exception e) {            e.printstacktrace();        }    }

常量WiFiPacket.PACKET_SIZE设置为1024 * 32(32 KB,因为我收到“ERRBLABLA MSG太长”错误,值较高).

fileTransferService.java

@OverrIDeprotected voID onHandleIntent(Intent intent) {    App app = (App) getApplication();    Context context = getApplicationContext();    boolean rightIntent = false;    WiFifile file = null;    if (intent.getAction().equals(ACTION_SEND_TEXT)) {        rightIntent = true;        file = WiFifile.fromText(intent.getExtras().getString(EXTRAS_TEXT));    } else if (intent.getAction().equals(ACTION_SEND_file)) {        rightIntent = true;        file = WiFifile.fromfile(intent.getExtras().getString(                EXTRAS_file_PATH));    }    if (rightIntent && file != null) {        app.getonWiFiTransmissionChangedListener().onNewOutgoingTransfer(                file);        String text = intent.getExtras().getString(EXTRAS_TEXT);        String host = intent.getExtras().getString(                EXTRAS_GROUP_OWNER_ADDRESS);        DatagramSocket socket = null;        int port = intent.getExtras().getInt(EXTRAS_GROUP_OWNER_PORT);        Log.d(TAG, "Sending packets to " + host + ":" + port);        try {            socket = new DatagramSocket();            int bytesSent = 0;            for (WiFiPacket p : file) {                Log.d(TAG, "Preparing another packet..");                byte[] payload = p.getBytes();                DatagramPacket packet = new DatagramPacket(payload,                        payload.length, InetAddress.getByname(host), port);                Log.d(TAG, "Sending packet..");                socket.send(packet);                bytesSent += payload.length;                Log.d(TAG, "Packet send! Contained " + payload.length                        + " bytes! All over we've sent about " + bytesSent                        + " bytes!");                List<WiFifile> List = new ArrayList<WiFifile>();                List.add(file);                app.getonWiFiTransmissionChangedListener()                        .onTransferProgressChanged(List);            }            app.getonWiFiTransmissionChangedListener()                    .onfileTransferSuccessful(file);            Log.d(TAG, "ClIEnt: Data written");        } catch (IOException e) {            e.printstacktrace();            Log.e(TAG, e.getMessage());        } finally {            if (socket != null) {                if (socket.isConnected()) {                    socket.close();                }            }        }    }}

代码可能不是最好的,但至于现在我唯一关心的是接收该死的数据包. 总结

以上是内存溢出为你收集整理的android – UDP数据包(通过WiFi Direct)永远不会到达全部内容,希望文章能够帮你解决android – UDP数据包(通过WiFi Direct)永远不会到达所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1109899.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存