String myIp =getIpAddress();int port =55555;//creates a port mapping configuration with the external/internal port, an internal host IP, the protocol and an optional descriptionPortMapping[] desiredMapping =newPortMapping[2];
desiredMapping[0]=newPortMapping(port,myIp,PortMapping.Protocol.TCP);
desiredMapping[1]=newPortMapping(port,myIp,PortMapping.Protocol.UDP);//starting the UPnP serviceUpnpService upnpService =newUpnpServiceImpl(newAndroidUpnpServiceConfiguration());RegistryListener registryListener =newPortMappingListener(desiredMapping);
upnpService.getRegistry().addListener(registryListener);
upnpService.getControlPoint().search();//method for getting local ipprivateStringgetIpAddress(){String ip ="";try{Enumeration<NetworkInterface> enumNetworkInterfaces =NetworkInterface.getNetworkInterfaces();while(enumNetworkInterfaces.hasMoreElements()){NetworkInterface networkInterface = enumNetworkInterfaces
.nextElement();Enumeration<InetAddress> enumInetAddress = networkInterface
.getInetAddresses();while(enumInetAddress.hasMoreElements()){InetAddress inetAddress = enumInetAddress.nextElement();if(inetAddress.isSiteLocalAddress()){
ip +=inetAddress.getHostAddress();}}}}catch(SocketException e){// TODO Auto-generated catch block
e.printStackTrace();
ip +="Something Wrong! "+ e.toString()+"\n";}return ip;}