日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

UPNP端口映射Android实现

發(fā)布時間:2024/3/24 Android 43 豆豆
生活随笔 收集整理的這篇文章主要介紹了 UPNP端口映射Android实现 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

這一段一直研究UPNP協(xié)議,搞得頭都快炸了,找到一個upnp的jar包,感覺對發(fā)現(xiàn)InternetGatewayDevice非常方便。下面寫了一個小程序,是發(fā)現(xiàn)路由器并進行端口映射的。


package com.example.laozhou.upnptest;import android.content.Context; import android.net.wifi.WifiInfo; import android.net.wifi.WifiManager; import android.os.Bundle; import android.os.Message; import android.support.v7.app.AppCompatActivity; import android.util.Log;import net.sbbi.upnp.impls.InternetGatewayDevice; import net.sbbi.upnp.messages.ActionResponse; import net.sbbi.upnp.messages.UPNPResponseException;import java.io.IOException; import java.net.InetAddress; import java.net.NetworkInterface; import java.util.Enumeration; import java.util.Locale;public class Main2Activity extends AppCompatActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main2);new Thread(discoverUPNP).start();}Runnable discoverUPNP = new Runnable() {@Overridepublic void run() {int discoveryTiemout = 5000; // 5 secstry {System.out.println("Looking for Internet Gateway Device...");InternetGatewayDevice[] IGDs = InternetGatewayDevice.getDevices(discoveryTiemout);if (IGDs != null) {for (int i = 0; i < IGDs.length; i++) {InternetGatewayDevice testIGD = IGDs[i];System.out.println("\t\nFound device " + testIGD.getIGDRootDevice().getModelName());System.out.println("External IP address: " + testIGD.getExternalIPAddress());// now let's open the portint portNum = 12345;System.out.println("\nTrying to map dummy port " + portNum + "..."); // String localHostIP = getIpAddress();String localHostIP = "192.168.2.201";System.out.println("localHostIP " + localHostIP);boolean mapped = testIGD.addPortMapping("chinavideo mapping", null, portNum, portNum, localHostIP, 0, "TCP");System.out.println("AddPortState: " + mapped);if (mapped) {System.out.println("Port " + portNum + " mapped to " + localHostIP);System.out.println("Current mappings count is " + testIGD.getNatMappingsCount());// checking on the deviceActionResponse resp = testIGD.getSpecificPortMappingEntry(null, portNum, "TCP");if (resp != null) {System.out.println("Port " + portNum + " mapping confirmation received from device");}}}System.out.println("\nDone.");} else {System.out.println("Unable to find IGD on your network");}} catch (IOException ex) {System.err.println("IOException occured during discovery or ports mapping " + ex.getMessage());} catch (UPNPResponseException respEx) {System.err.println("UPNP device unhappy " + respEx.getDetailErrorCode() + " " + respEx.getDetailErrorDescription());}}};private String getIpAddress() {WifiManager wifiManager = (WifiManager) Main2Activity.this.getSystemService(WIFI_SERVICE);WifiInfo wifiInfo = wifiManager.getConnectionInfo();int ipAddress = wifiInfo.getIpAddress();String ipaddress = String.format("%d.%d.%d.%d",(ipAddress & 0xff), (ipAddress >> 8 & 0xff), (ipAddress >> 16 & 0xff),(ipAddress >> 24 & 0xff));return ipaddress;}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()) {Log.d("ipAddress:",inetAddress.getHostAddress().toString());return inetAddress.getHostAddress().toString();}}}} catch (Exception ex) {Log.e("IP Address", ex.toString());}return null;} }
其中端口號可以自己定義,因為我本機沒有可測試接口,所以映射了局域網(wǎng)內(nèi)一臺機器的的接口到路由器。

所用包為?upnplib-mobile.jar

總結

以上是生活随笔為你收集整理的UPNP端口映射Android实现的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。