数据传输Socket,获取IP地址的实现方法

Android 获取IP地址

首先,描述下功能,两个手机,其中一个手机开热点,另外一个手机连接热点,2个手机间数据通信。简述一下原理:开热点的手机相当于路由器,连接的手机作为客户端,获取路由器的IP建立Socket
连接,开始双方通信。

最近做项目,有一个需求是Android设备获取当前IP的功能,经过一番查询资料解决了,记录下实现方法。

图片 1

1.使用WIFI

开热点的作为服务器端

首先设置用户权限

初始化数据:

<uses-permission Android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>

<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>

<uses-permission android:name="android.permission.WAKE_LOCK"></uses-permission>
 public static final int DEVICE_CONNECTING = 1;//有设备正在连接热点 public static final int DEVICE_CONNECTED = 2;//有设备连上热点 public static final int SEND_MSG_SUCCSEE = 3;//发送消息成功 public static final int SEND_MSG_ERROR = 4;//发送消息失败 public static final int GET_MSG = 6;//获取新消息 private TextView text_state; private WifiManager wifiManager; /** * 连接线程 */ private ConnectThread connectThread; /** * 监听线程 */ private ListenerThread listenerThread; /** * 热点名称 */ private static final String WIFI_HOTSPOT_SSID = "TEST"; /** * 端口号 */ private static final int PORT = 54321; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); findViewById(R.id.create_wifi).setOnClickListener; findViewById(R.id.close_wifi).setOnClickListener; findViewById(R.id.send).setOnClickListener; text_state =  findViewById(R.id.receive); wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE); /** * 先开启监听线程,在开启连接 */ listenerThread = new ListenerThread(PORT, handler); listenerThread.start(); try { Thread.sleep; } catch (InterruptedException e) { e.printStackTrace(); } // 开启连接线程 new Thread(new Runnable() { @Override public void run() { try { Log.i("ip", "getWifiApIpAddress()" + getWifiApIpAddress; //本地路由开启通信 String ip = getWifiApIpAddress(); if (ip != null) { } else { ip = "192.168.43.1"; } Socket socket = new Socket; connectThread = new ConnectThread(MainActivity.this, socket, handler); connectThread.start(); } catch (IOException e) { e.printStackTrace(); runOnUiThread(new Runnable() { @Override public void run() { text_state.setText; } }); } } }).start(); }

其次,代码如下

创建wifi 热点,之后开启Socket 监听线程

public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);



    //获取wifi服务

    WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);

    //判断wifi是否开启

    if (!wifiManager.isWifiEnabled()) {

    wifiManager.setWifiEnabled(true); 

    }

    WifiInfo wifiInfo = wifiManager.getConnectionInfo();   

    int ipAddress = wifiInfo.getIpAddress(); 

    String ip = intToIp(ipAddress); 

    EditText et = (EditText)findViewById(R.id.EditText01);

    et.setText(ip);

  }  

  private String intToIp(int i) {   



     return (i & 0xFF ) + "." +   

    ((i >> 8 ) & 0xFF) + "." +   

    ((i >> 16 ) & 0xFF) + "." +   

    ( i >> 24 & 0xFF) ;

   } 
 /** * 创建Wifi热点 */ private void createWifiHotspot() { if (wifiManager.isWifiEnabled { //如果wifi处于打开状态,则关闭wifi, wifiManager.setWifiEnabled; } final WifiConfiguration config = new WifiConfiguration(); config.SSID = WIFI_HOTSPOT_SSID; config.preSharedKey = "123456789"; config.hiddenSSID = false; config.allowedAuthAlgorithms .set(WifiConfiguration.AuthAlgorithm.OPEN);//开放系统认证 config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP); config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK); config.allowedPairwiseCiphers .set(WifiConfiguration.PairwiseCipher.TKIP); config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP); config.allowedPairwiseCiphers .set(WifiConfiguration.PairwiseCipher.CCMP); config.status = WifiConfiguration.Status.ENABLED; //通过反射调用设置热点 //192.168.43.59 // Log.i("ip", "getWifiApIpAddress()" + getWifiApIpAddress() + // "\n"); try { Method method = wifiManager.getClass().getMethod( "setWifiApEnabled", WifiConfiguration.class, Boolean.TYPE); boolean enable =  method.invoke(wifiManager, config, true); if  { text_state.setText("热点已开启 SSID:" + WIFI_HOTSPOT_SSID + " password:123456789"); // 开启连接线程 new Thread(new Runnable() { @Override public void run() { try { Log.i("ip", "getWifiApIpAddress()" + getWifiApIpAddress; String ip = getWifiApIpAddress(); if (ip != null) { } else { //一般Android手机默认路由是 ip = "192.168.43.1"; } //本地路由开启通信 Socket socket = new Socket; connectThread = new ConnectThread(MainActivity.this, socket, handler); connectThread.start(); } catch (IOException e) { e.printStackTrace(); runOnUiThread(new Runnable() { @Override public void run() { text_state.setText; } }); } } }).start(); Thread.sleep; // listenerThread = new ListenerThread(PORT, handler); // listenerThread.start(); } else { text_state.setText; } } catch (Exception e) { e.printStackTrace(); text_state.setText; } }

2.使用GPRS

服务器端获取本地路由:

首先,设置用户上网权限

 public String getWifiApIpAddress() { try { for (Enumeration<NetworkInterface> en = NetworkInterface .getNetworkInterfaces(); en.hasMoreElements { NetworkInterface intf = en.nextElement(); if (intf.getName().contains { for (Enumeration<InetAddress> enumIpAddr = intf .getInetAddresses(); enumIpAddr.hasMoreElements { InetAddress inetAddress = enumIpAddr.nextElement(); if (!inetAddress.isLoopbackAddress() && (inetAddress.getAddress().length == 4)) { Log.d("Main", inetAddress.getHostAddress; return inetAddress.getHostAddress(); } } } } } catch (SocketException ex) { Log.e("Main", ex.toString; } return null; }

<uses-permission
android:name=”android.permission.INTERNET”></uses-permission>

关闭热点

其次,代码如下

 /** * 关闭WiFi热点 */ public void closeWifiHotspot() { try { Method method = wifiManager.getClass().getMethod("getWifiApConfiguration"); method.setAccessible; WifiConfiguration config = (WifiConfiguration) method.invoke(wifiManager); Method method2 = wifiManager.getClass().getMethod("setWifiApEnabled", WifiConfiguration.class, boolean.class); method2.invoke(wifiManager, config, false); } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } text_state.setText; }
public String getLocalIpAddress()

  {

    try

    {

      for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();)

      {

        NetworkInterface intf = en.nextElement();

        for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();)

        {

          InetAddress inetAddress = enumIpAddr.nextElement();

          if (!inetAddress.isLoopbackAddress())

          {

            return inetAddress.getHostAddress().toString();

          }

        }

      }

    }

    catch (SocketException ex)

    {

      Log.e("WifiPreference IpAddress", ex.toString());

    }

    return null;

  }

ListenerThread 监听线程

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

import android.os.Handler;import android.os.Message;import android.util.Log;import com.example.syhuang.wifiserver.MainActivity;import java.io.IOException;import java.net.ServerSocket;import java.net.Socket;/** * 监听线程 * Created by syh on 2018/4/3 */public class ListenerThread extends Thread { private ServerSocket serverSocket = null; private Handler handler; private int port; private Socket socket; public ListenerThread(int port, Handler handler) { setName("ListenerThread"); this.port = port; this.handler = handler; try { serverSocket = new ServerSocket;//监听本机的12345端口 } catch (IOException e) { e.printStackTrace(); } } @Override public void run() { while  { try { Log.i("ListennerThread", "阻塞"); //阻塞,等待设备连接 if (serverSocket != null) socket = serverSocket.accept(); Message message = Message.obtain(); message.what = MainActivity.DEVICE_CONNECTING; handler.sendMessage; } catch (IOException e) { Log.i("ListennerThread", "error:" + e.getMessage; e.printStackTrace(); } } } public Socket getSocket() { return socket; }}

您可能感兴趣的文章:

  • Android
    判断ip地址合法实现代码
  • android
    获取本机的IP地址和mac物理地址的实现方法
  • android实现获取有线和无线Ip地址的方法
  • Android手机获取IP地址的两种方法
  • android开发教程之wifi开发示例
  • android编程实现设置、打开wifi热点共享供他人连接的方法
  • android连接wifi时获取广播地址代码
  • Android开发之Wifi基础教程
  • Android开发实现在Wifi下获取本地IP地址的方法

ConnectThread 连接线程

package com.example.syhuang.wifiserver.thread;import android.content.Context;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.util.Log;import com.example.syhuang.wifiserver.MainActivity;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.net.Socket;import java.text.DecimalFormat;/** * 连接线程 * Created by syhuang on 2018/4/3 */public class ConnectThread extends Thread { private final Socket socket; private Handler handler; private InputStream inputStream; private OutputStream outputStream; Context context; public ConnectThread(Context context, Socket socket, Handler handler) { setName("ConnectThread"); Log.i("ConnectThread", "ConnectThread"); this.socket = socket; this.handler = handler; this.context = context; } @Override public void run() {/* if(activeConnect){// socket.c }*/ if (socket == null) { return; } handler.sendEmptyMessage(MainActivity.DEVICE_CONNECTED); try { //获取数据流 inputStream = socket.getInputStream(); outputStream = socket.getOutputStream(); byte[] buffer = new byte[1024]; int bytes; while  { //读取数据 bytes = inputStream.read; if (bytes > 0) { final byte[] data = new byte[bytes]; System.arraycopy(buffer, 0, data, 0, bytes); Message message = Message.obtain(); message.what = MainActivity.GET_MSG; Bundle bundle = new Bundle(); bundle.putString("MSG", new String; message.setData; handler.sendMessage; } // DataInputStream dis = null; // FileOutputStream fos = null; // try { // dis = new DataInputStream(inputStream); // // // 文件名和长度 // String fileName = dis.readUTF(); // if (!fileName.equals { // long fileLength = dis.readLong(); // Log.i("ConnectThread", "======== 文件接收 [File Name:" + fileName + "] " + // "[Size:" + getFormatFileSize(fileLength) + "] ========"); // File directory = new File(Environment.getExternalStorageDirectory; // if (!directory.exists { // directory.mkdir(); // } else { // } // File file = new File(directory.getAbsolutePath() + File.separatorChar + fileName); // fos = new FileOutputStream; // // // 开始接收文件 // byte[] bytesA = new byte[1024]; // int length = 0; // int progress = 0; // while ((length = dis.read(bytesA, 0, bytesA.length)) != -1) { // Log.i("ConnectThread", length + "..."); // fos.write(bytesA, 0, length); // fos.flush(); // progress += length; // Log.i("ConnectThread", "| " + (100 * progress / file.length + "% |"); // } // Log.i("ConnectThread", "文件传输完成"); // // Message message = Message.obtain(); // message.what = MainActivity.GET_MSG; // Bundle bundle = new Bundle(); // bundle.putString("MSG", new String("接收到文件:" + file.getAbsolutePath; // message.setData; // handler.sendMessage; // } else { // //读取数据 // bytes = inputStream.read; // if (bytes > 0) { // final byte[] data = new byte[bytes]; // System.arraycopy(buffer, 0, data, 0, bytes); // // // Message message = Message.obtain(); // message.what = MainActivity.GET_MSG; // Bundle bundle = new Bundle(); // bundle.putString("MSG", new String; // message.setData; // handler.sendMessage; // // Log.i("ConnectThread", "读取到数据:" + new String; // } // } } } catch (IOException e) { e.printStackTrace(); } } /** * 格式化文件大小 * * @param length * @return */ private String getFormatFileSize(long length) { DecimalFormat df = new DecimalFormat; double size =  length) / (1 << 30); if (size >= 1) { return df.format + "GB"; } size =  length) / (1 << 20); if (size >= 1) { return df.format + "MB"; } size =  length) / (1 << 10); if (size >= 1) { return df.format + "KB"; } return length + "B"; } public static boolean copyFile(InputStream inputStream, OutputStream out) { byte buf[] = new byte[1024]; int len; try { while ((len = inputStream.read != -1) { out.write(buf, 0, len); } out.close(); // inputStream.close(); } catch (IOException e) { return false; } return true; } /** * 发送数据 */ public void sendData(String msg) { Log.i("ConnectThread", "发送数据:" + (outputStream == null)); if (outputStream != null) { try { outputStream.write(msg.getBytes; Log.i("ConnectThread", "发送消息:" + msg); Message message = Message.obtain(); message.what = MainActivity.SEND_MSG_SUCCSEE; Bundle bundle = new Bundle(); bundle.putString("MSG", new String; message.setData; handler.sendMessage; } catch (IOException e) { e.printStackTrace(); Message message = Message.obtain(); message.what = MainActivity.SEND_MSG_ERROR; Bundle bundle = new Bundle(); bundle.putString("MSG", new String; message.setData; handler.sendMessage; } } }}

客户端连接到热点,代码差不多,初始化数据部分顺序不一样,先开启连接线程,在开启监听线程,获取路由器IP
地址有差别。其他基本一样。

wifi获取 已连接网络路由 路由ip地址

 /** * wifi获取 已连接网络路由 路由ip地址 * @param context * @return */ private static String getWifiRouteIPAddress(Context context) { WifiManager wifi_service = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); DhcpInfo dhcpInfo = wifi_service.getDhcpInfo(); // WifiInfo wifiinfo = wifi_service.getConnectionInfo(); // System.out.println("Wifi info----->" + wifiinfo.getIpAddress; // System.out.println("DHCP info gateway----->" + Formatter.formatIpAddress(dhcpInfo.gateway)); // System.out.println("DHCP info netmask----->" + Formatter.formatIpAddress(dhcpInfo.netmask)); //DhcpInfo中的ipAddress是一个int型的变量,通过Formatter将其转化为字符串IP地址 String routeIp = Formatter.formatIpAddress(dhcpInfo.gateway); Log.i("route ip", "wifi route ip:" + routeIp); return routeIp; }

初始化

 public static final int DEVICE_CONNECTING = 1;//有设备正在连接热点 public static final int DEVICE_CONNECTED = 2;//有设备连上热点 public static final int SEND_MSG_SUCCSEE = 3;//发送消息成功 public static final int SEND_MSG_ERROR = 4;//发送消息失败 public static final int GET_MSG = 6;//获取新消息 private TextView text_state; /** * 连接线程 */ private ConnectThread connectThread; /** * 监听线程 */ private ListenerThread listenerThread; /** * 热点名称 */ private static final String WIFI_HOTSPOT_SSID = "TEST"; /** * 端口号 */ private static final int PORT = 54321; private WifiManager wifiManager; private TextView status_init; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); findViewById(R.id.send).setOnClickListener; findViewById(R.id.connect).setOnClickListener; findViewById(R.id.fileButton).setOnClickListener; wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE); //检查Wifi状态 if (!wifiManager.isWifiEnabled wifiManager.setWifiEnabled; text_state =  findViewById(R.id.status_info); status_init =  findViewById(R.id.status_init); status_init.setText("已连接到:" + wifiManager.getConnectionInfo().getSSID() + "\nIP:" + getIp() + "\n路由:" + getWifiRouteIPAddress(MainActivity.this)); // initBroadcastReceiver(); // 开启连接线程 new Thread(new Runnable() { @Override public void run() { try { Socket socket = new Socket(getWifiRouteIPAddress(MainActivity.this), PORT); connectThread = new ConnectThread(socket, handler); connectThread.start(); } catch (IOException e) { e.printStackTrace(); runOnUiThread(new Runnable() { @Override public void run() { text_state.setText; } }); } } }).start(); listenerThread = new ListenerThread(PORT, handler); listenerThread.start(); }

服务器和客户端Handler处理一样

 private Handler handler = new Handler() { @Override public void handleMessage(Message msg) { switch  { case DEVICE_CONNECTING: connectThread = new ConnectThread(listenerThread.getSocket(), handler); connectThread.start(); break; case DEVICE_CONNECTED: text_state.setText; break; case SEND_MSG_SUCCSEE: text_state.setText("发送消息成功:" + msg.getData().getString; break; case SEND_MSG_ERROR: text_state.setText("发送消息失败:" + msg.getData().getString; break; case GET_MSG: text_state.setText("收到消息:" + msg.getData().getString; break; } } };

源码传送门

Android 连接Wifi和创建Wifi热点 demo – CSDN博客Android WiFi开发
扫描、连接、信息 – CSDN博客Android/安卓开发之WIFI的基本应用 –
CSDN博客Android设备之间通过Wifi通信 – sowhat4999 – 博客园Android
WiFi开发教程——WiFi热点数据传输 – CSDN博客Android手机通过wifi进行数据传输

  • CSDN博客android wifi 点对点传输 – 简书android WIFI Socket 文件传输 –
    Android移动开发技术文章_手机开发 – 红黑联盟Android
    实现无网络传输文件用户5207283611新浪博客wifi direct—深入理解Wi-Fi P2P
  • CSDN博客Android手机间使用socket进行文件互传实例 –
    CSDN博客Android仿茄子快传-实现面对面快传功能 –
    CSDN博客安卓手机wifi面对面快传的实现 – CSDN博客

Post Author: admin

发表评论

电子邮件地址不会被公开。 必填项已用*标注