【问题标题】:How to get IPv4 address from already connected router via wifi如何通过wifi从已经连接的路由器获取IPv4地址
【发布时间】:2015-11-12 16:39:54
【问题描述】:

我想在我的路由器和安卓设备之间建立tcp/ip 连接。我已经连接到特定的 wifi 网络。但我还需要从路由器获取 IPv4 地址以建立tcp/ip 连接。如果我已经连接,是否可以获得 IPv4 地址。这是我的代码。如何连接到我的路由器。

 @Override
    public void onReceive(Context context, Intent intent) {
        List<ScanResult> list = scanner.getScanResults();

        for(ScanResult result : list){
            if(result.SSID.equals("Micro")){
                //Connect to THIS network
                connect(result.SSID);
                break;
            }
        }
    }

    public void connect(String name){
        String password = "myPassword";
        WifiConfiguration conf = new WifiConfiguration();
        conf.SSID = "\"" + name + "\"";
        conf.preSharedKey = "\""+ password +"\"";

        scanner.addNetwork(conf);

        List<WifiConfiguration> list = scanner.getConfiguredNetworks();
        for( WifiConfiguration i : list ) {
            if(i.SSID != null && i.SSID.equals("\"" + name + "\"")) {
                scanner.disconnect();
                scanner.enableNetwork(i.networkId, true);
                scanner.reconnect();
                break;
            }
        }
    }

这是我的客户班 公共类 TCPClient {

private String serverMessage;
public static final String SERVER_IP = "xxx.xxx.x.xx"; // computer IP address
public static final int SERVER_PORT = 4444;
private OnMessageReceived mMessageListener = null;
private boolean run = false;
private PrintWriter out;
private BufferedReader in;

/**
 *  Constructor of the class. OnMessagedReceived listens for the messages received from server
 */
public TCPClient(OnMessageReceived listener) {
    mMessageListener = listener;
}

/**
 * Sends the message entered by client to the server
 * @param message text entered by client
 */
public void sendMessage(String message){
    if (out != null && !out.checkError()) {
        out.println(message);
        out.flush();
    }
}

public void stopClient(){
    run = false;
}

public void run() {

    run = true;

    try {
        // computer's IP address.
        InetAddress serverAddr = InetAddress.getByName(SERVER_IP);

        //create a socket to make the connection with the server
        Socket socket = new Socket(serverAddr, SERVER_PORT);

        try {

            //send the message to the server
            out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true);

            //receive the message which the server sends back
            in = new BufferedReader(new InputStreamReader(socket.getInputStream()));

            //in this while the client listens for the messages sent by the server
            while (run) {
                serverMessage = in.readLine();
                if (serverMessage != null && mMessageListener != null) {
                    //call the method messageReceived from MyActivity class
                    mMessageListener.messageReceived(serverMessage);
                }
                serverMessage = null;
            }

            Log.e("RESPONSE FROM SERVER", "S: Received Message: '" + serverMessage + "'");

        } catch (Exception e) {

            Log.e("TCP", "S: Error", e);

        } finally {
            //the socket must be closed. It is not possible to reconnect to this socket
            // after it is closed, which means a new socket instance has to be created.
            socket.close();
        }

    } catch (Exception e) {

        Log.e("TCP", "C: Error", e);

    }

}

    // Declare the interface. The method messageReceived(String message) will must be implemented in the MyActivity
    // class at on asynckTask doInBackground
    public interface OnMessageReceived {
        void messageReceived(String message);
    }
}

所以现在我已经硬编码了我的 IP 地址,但我如何从连接的设备获取?

【问题讨论】:

    标签: android networking tcp ip ipv4


    【解决方案1】:

    下一步是运行 dhcp 客户端或从您的应用程序发送 DHCP 消息。一个让其快速运行的简单替代方法是分配一个静态 IP 地址,但不建议这样做,因为可能会发生 IP 冲突。

    【讨论】:

    • 好的,但是如何发送 DHCP 消息?
    • @David,似乎没有 API。你可以实现自己的DHCP客户端,然后配置网络接口
    猜你喜欢
    • 2019-12-16
    • 1970-01-01
    • 2015-11-13
    • 2012-08-13
    • 1970-01-01
    • 2011-09-25
    • 2020-05-29
    • 1970-01-01
    相关资源
    最近更新 更多