Hello Guest

what about UPNP™ for Java ?

  • 5 Replies
  • 11041 Views
what about UPNP™ for Java ?
« on: November 28, 2009, 23:52:49 »
hi,

I'd like to use upnp for port mapping multi-player activity with the game. I found out upnplib and cyberlink for java.
the latter is quite a bit older, but is there any more recent stuff or do you know a better way to connect two players in a peer-to-peer fashion ?

I've got an implemented server-client interface, whose connection depends on a specific port, selected randomly, but which will not get opened unless set up by the players themselves when using a firewall/router beyond the local network. Then, I'd like to make players able to "announce" on a database located on any internet server, which will list matches (as hosts) available... quite a common sense.
e.g. "I host a game on 23.43.33.222:9090, would you like to join it ?" anyway the odd player wouldn't  establish the connection if he ain't up to access its router admin to open port 9090 for UDP/TCP. Hence, almost all routers run UPNP nowadays so that it can solve the technical issue for that average player.
UPNPLib documentation talks about security issues with ACL which were not available at the time this project was developed, then it may not work with newer devices (???).

Re: what about UPNP™ for Java ?
« Reply #1 on: December 01, 2009, 01:18:27 »
I choosed UPNPLib.
Now here's another issue with the local IP address (in my LAN) :                     
Code: [Select]
localHostIP = InetAddress.getLocalHost().getHostAddress();
always return 127.0.0.2, but I'd like to read my LAN IP address.
from javadoc :
Quote
Returns the local host.

If there is a security manager, its checkConnect method is called with the local host name and -1 as its arguments to see if the operation is allowed. If the operation is not allowed, an InetAddress representing the loopback address is returned.
How to do it ?

*

Offline bobjob

  • ****
  • 394
  • LWJGL: WOW SO GOOD
Re: what about UPNP™ for Java ?
« Reply #2 on: December 01, 2009, 17:34:25 »
Im just having a stab at it.

maybe send a network broadcast packet. then when you recieve the packet, check the origin of the IP.

edit: Multiple NIC cards may be a problem, as well as the network each card connects to. So it might be important to first know which network you want the program to run on.
ei. wireless/wired running on the same computer.
« Last Edit: December 01, 2009, 23:10:46 by bobjob »

Re: what about UPNP™ for Java ?
« Reply #3 on: December 09, 2009, 22:40:39 »
Im just having a stab at it.

maybe send a network broadcast packet. then when you recieve the packet, check the origin of the IP.
(...)
could you explain a bit further on that broadcast please ? It should'nt be casted to the WAN, but the LAN.
I'm connected to my router by wire, so I don't see at the moment where to define Java on the external-LAN address of my pc. it should return 168.192.1.3, not the loopback...

I'm using a Socket to establish connection. But I need first to set the UPNP port to my local address.
For that broadcast trick, is this MulticastSocket http://java.sun.com/docs/books/tutorial/networking/datagrams/definition.html the correct object ?
« Last Edit: December 09, 2009, 22:43:11 by broumbroum »

Re: what about UPNP™ for Java ?
« Reply #4 on: December 09, 2009, 23:02:12 »

Re: what about UPNP™ for Java ?
« Reply #5 on: December 19, 2009, 00:49:07 »
yet here's how to get it read from a java 1.5 sdk :
Code: [Select]
Enumeration<NetworkInterface> nic = NetworkInterface.getNetworkInterfaces();
                    while (nic.hasMoreElements()) {
                        NetworkInterface anic = nic.nextElement();
                        Enumeration<InetAddress> addrs = anic.getInetAddresses();
                        while (addrs.hasMoreElements()) {
                            InetAddress addr = addrs.nextElement();
                            if (!addr.isLoopbackAddress() && addr.isSiteLocalAddress()) {
                                localHostIP = addr.getHostAddress();
                                break;
                            }
                        }
                        if (localHostIP != null) {
                            break;
                        }
                    }
                    if (localHostIP == null) {
                        localHostIP = InetAddress.getLocalHost().getHostAddress();
                    }

I have now 192.168.1.5 which is my local area address I was looking for. :D
Now UPNPLib receives the NAT mapping address correctly :
Code: [Select]
if (gateway != null) {
                        // now let's open the port
                        // we assume that localHostIP is something else than 127.0.0.1
                        mapped = gateway.addPortMapping("Some mapping description", null, internalPort, externalPort, localHostIP, 0, protocol);
                        if (mapped) {
                            System.out.println(JXAenvUtils._JXAEnvOutput("UPNP Port " + externalPort + " mapped for " + protocol + " to " + localHostIP + " : " + internalPort, JXAenvUtils.APP_NOTICE));
                        } else {
                            throw new IOException("UPNP Port " + externalPort + " not mapped");
                        }
                    }
« Last Edit: December 19, 2009, 00:51:39 by broumbroum »