Android上DatagramSocket的send()中的奇怪块

时间:2017-09-26 02:05:55

标签: java android sockets udp blocking

我试图将Android手机上的UDP数据包发送到家庭网络中的每个可能主机。发送UDP数据包的AFAIK很少会阻止,但是当我在我的两部Android M手机上运行此程序时,它总是阻止一次约2秒钟。有时,它会在第183 #include <boost/preprocessor/control/iif.hpp> #include <boost/preprocessor/seq/for_each.hpp> #define MY_SEQUENCE ((1,int,z))((0,float,y)) #define APPLY_EXTRACT(r,data,elem) EXTRACT elem #define EXTRACT(FLAG_,TYPE_,PNAME_) BOOST_PP_IIF(FLAG_, TYPE_, "narp") #define ALL_ARGS(args) BOOST_PP_SEQ_FOR_EACH(APPLY_EXTRACT, _, MY_SEQUENCE) ALL_ARGS(MY_SEQUENCE) 处阻塞,有时在第237 send()处,它随路由器一起变化,我用Netgear路由器,谷歌wifi等测试它。我知道我可以发送UDP数据包广播地址,如send(),这不是我想要的。这是代码:

MainActivity.java

192.168.1.255

UDPSender.java

package com.example.udp;

// import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

import java.math.BigInteger;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

//    private class MyTask extends AsyncTask<UDPSender, Void, Void> {
//        @Override
//        protected Void doInBackground(UDPSender... params) {
//            // change it to your CIDR
//            for (InetAddress addr: ipList("10.1.1.0/24")) {
//                params[0].send(addr);
//            }
//            return null;
//        }
//    }

    public void send(View view) {
        final UDPSender sender = new UDPSender();
//        MyTask task = new MyTask(); // AsyncTask doesn't work too.
//        task.execute(sender);
        ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
        executor.scheduleWithFixedDelay(new Runnable() {
            int i = 0;
            // change it to your CIDR
            ArrayList<InetAddress> ips = ipList("10.0.0.0/24");
            @Override
            public void run() {
                if (i < ips.size()) {
                    sender.send(ips.get(i));
                    i++;
                }
            }
        }, 0, 10, TimeUnit.MILLISECONDS);
    }

    private ArrayList<InetAddress> ipList(String cidr) {
        ArrayList<InetAddress> ipList = new ArrayList<>();
        String[] ipnet = cidr.split("/");
        int hostBits = 32 - Integer.parseInt(ipnet[1]);
        // If a literal IP address is supplied, only the
        // validity of the address format is checked.
        try {
            InetAddress addr = InetAddress.getByName(ipnet[0]);
            int ip = new BigInteger(addr.getAddress()).intValue();

            int start = (ip >> hostBits) << hostBits;
            int end = ip | ((1 << hostBits) - 1);

            for (int i = start + 1; i < end; i++) {
                if (i != ip) { // exclude itself
                    InetAddress inetAddress = InetAddress.getByAddress(BigInteger.valueOf(i).toByteArray());
                    ipList.add(inetAddress);
                }
            }
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }

        return ipList;
    }
}

布局只包含一个触发发送的按钮,这与此行为无关。流量太大会导致这种情况吗?有什么办法可以避免这个吗我应该转向NIO吗?

更新
我在Android N手机上测试这个应用程序,它不会阻止。

0 个答案:

没有答案