如果它们位于不同的子网上,是否可以连接两个无线设备?

时间:2015-02-25 20:45:15

标签: android networking multicast access-point

我正在使用Android应用。让我们假设我有4个设备(智能手机),其中两个是接入点,并且它们都连接了一个设备,显然它们通过无线连接。

所以事实是我需要一种方法将数据从一个子网发送到另一个子网,我尝试使用多播(使用jmdns库),但没有成功。

有人能帮帮我吗?感谢。

2 个答案:

答案 0 :(得分:1)

我使用多播而没有任何库在PC和智能手机之间做了类似的事情,只是Android和Java后端的MulticastSocket类,你试过吗?

答案 1 :(得分:0)

    import java.io.BufferedReader;
    import java.io.ByteArrayInputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.net.DatagramPacket;
    import java.net.InetAddress;
    import java.net.MulticastSocket;
    import java.util.concurrent.Executors;
    import java.util.concurrent.ScheduledExecutorService;
    import java.util.concurrent.TimeUnit;

    import com.classes.domain.NetworkProperties;
    import com.classes.domain.Settings;

    public class MulticastSender {

        private static ScheduledExecutorService scheduler = null;

        public static final String MC_ADDRESS = "226.2.2.3";
        public static final int MC_PORT = 8888;
        public static final int MAX_LEN = 1024;

        public static void startExecutor() {

            scheduler = Executors.newScheduledThreadPool(1);

            // remove final and uncomment line above if you wanna force automatic
            // start
            // final Runnable multicastServer = new Runnable() {
            Runnable multicastServer = new Runnable() {
                @Override
                public void run() {
                    multicastSender();
                }
            };

            scheduler.scheduleAtFixedRate(multicastServer, 0, 10, TimeUnit.SECONDS);
        }

        public static void multicastSender() {
            MulticastSocket socket = null;
            InetAddress address = null;
            InputStream is = null;
            BufferedReader br = null;
            int ttl = 1;
            String sendData = null;
            byte[] sendBytes = null;

            try {
                socket = new MulticastSocket();
                socket.setTimeToLive(ttl);

                address = InetAddress.getByName(MC_ADDRESS);

                NetworkProperties.retrieveNetworkProperties();
                String ip = NetworkProperties.localIPv4Address;
                int port = NetworkProperties.applicationPort;

                sendData = ip + "-" + port;
                is = new ByteArrayInputStream(sendData.getBytes());
                br = new BufferedReader(new InputStreamReader(is));

                while ((sendData = br.readLine()) != null) {

                    sendBytes = sendData.getBytes();

                    DatagramPacket packet = new DatagramPacket(sendBytes, sendBytes.length, address, MC_PORT);
                    socket.send(packet);

                }

                socket.close();

            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }

    }



package com.classes.domain;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;
import java.sql.SQLException;

import android.content.Context;
import android.net.wifi.WifiManager;

public class MulticastReceiver {

    public static final String MC_ADDRESS = "226.2.2.3";
    public static final int MC_PORT = 8888;
    public static final int MAX_LEN = 1024;

    public static void multicastReceiver(Context context) {

        MulticastSocket socket = null;
        DatagramPacket packet = null;
        InetAddress address = null;
        WifiManager.MulticastLock lock = null;

        WifiManager wifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        if (wifi != null) {
            lock = wifi.createMulticastLock("AllowMulticast");
            lock.acquire();
        }

        try {
            socket = new MulticastSocket(MC_PORT);
            socket.setReuseAddress(true);

            address = InetAddress.getByName(MC_ADDRESS);
            socket.joinGroup(address);

            while (true) {

                byte[] buf = new byte[MAX_LEN];
                packet = new DatagramPacket(buf, buf.length);

                socket.receive(packet);

                String msg = new String(buf, 0, packet.getLength());
//              System.out.println("From " + packet.getAddress() + " Msg : "
//                      + msg);
//              System.out.println("Received " + packet.getLength()
//                      + " bytes from " + packet.getAddress() + ": "
//                      + new String(packet.getData(), 0, packet.getLength()));

                String temp = msg;

/*
REPLACE THIS PART WITH RECEIVED DATA DO WHAT YOU NEED
                if (temp.indexOf("-") != -1) {
                    String[] result = temp.split("-");
                    String ip = result[0];
                    String port = result[1];


                    Settings settings = Settings.loadSettings(context);
                    settings.setIP(ip);
                    settings.setPort(Integer.parseInt(port));
                    try {
                        settings.Save(context);
                    } catch (SQLException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

                }
*/

                break;
            }

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

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

        if (lock.isHeld()) {
            lock.release();
            lock = null;
        }

    }

}