Problems With ServerSocket

Ok, ive tried just about everything i can involving this and for some wierd reason its not working.
I created a servers socket like so

ServerSocker s1 = new ServerSocket(9999);

but when i print out the toString method of s1, it shows that the Adress of it is 0.0.0.0.0 which strikes me odd… so then i try using the other ServerSocket Constructors to assign it an internet adress.
I did this like so

ServerSocket s1 = new ServerSocket(9999,10,InetAddress.getLocalHost());

Ok, this seemed logical, but It turned out the getLocalHost() method referred to my IP adress on my network, rather than my Internet IPaddress… I even tried using the getByName() method passing my Internet IP adress as a parameter and of course i got an exepton on this Java.Net.BindExeption… Im pretty stumped here, im just trying to run a simple program where an applet connects to the Server and the server returns the number of people connected to it, ive got everything else working except the fact that its not working over the internet because of these problems… if anyone could help i would really appreciate it…

-me-

I noticed the problem too. The socket sometimes binds to “127.0.0.1” or “localhost”. This
prevents clients to connect over the Internet! To use the game over the internet, you have
to find the real (not local) address of the computer!

That’s what I used for my game:


        // Finds the "real" ip address and NOT "localhost" or "127.0.0.1"
        // The old method might just return the loopback:
        //    String address = InetAddress.getLocalHost().getHostAddress();
        String address = "localhost";
        Enumeration networkInterfaces = NetworkInterface.getNetworkInterfaces();
        while(networkInterfaces.hasMoreElements()) {
          NetworkInterface netface = (NetworkInterface)networkInterfaces.nextElement();
          Enumeration inetAddresses = netface.getInetAddresses();
          while (inetAddresses.hasMoreElements()) {
            InetAddress ip = (InetAddress)inetAddresses.nextElement();
            if((!ip.isLoopbackAddress()) && (ip.getHostAddress().indexOf(":") == -1)) {
              address = ip.getHostAddress();   
            }
          }
        }      
        fire("Opening multiverse server on " + address + ":" + getPort() + ".");
        
        // open a non-blocking server socket channel
        serverSocketChannel = ServerSocketChannel.open();
        serverSocketChannel.configureBlocking(false);
  
        // bind to localhost on designated port
        serverSocketChannel.socket().bind(new InetSocketAddress(address, getPort()));

The example is for NIO, but it is the same as for ServerSocket.

This is a multi-homed computer?

If so then yes Java isnt going to figure out what interface is the “right” one because, really, it can’t. It varies dependign on what you are trying to do. Its up to you to tell it.

If something else is going on then I don’t udnerstand your problem yet. You should never see 127.0.0.1 as your address unless you have no live ethernet connection at all. If Java is returning this as the default then it sounds like a configuration problem on your OS level…

Are you behind a NAT/router/firewall? In wich case you can not connect to the computer from the internet. Unless you configure firewall or use udp punch through.

This is a good point. If you are trying to find oput your external NAT address from your server through the Java APIs and ship it out to players this will never work. Your compuer does not know its NAT address, only the router knows that.

The right answer in this case is to register a DNS name. I use DynDns.org for all my DNS names (both static and dynamic.)

Well, i registered the dns name and everything like that, but im still kinda confused as to where to take it from there. Just to make sure everything worked out allright, i did a system.out.println() statement of InetAddress.getByName(“barron.gameserve.org”); and that worked, it spit out barron.gameserve.org/[my IP address]
But Im still getting the whole thing where my Serversocket doesnt want to bind to that address… im sorry for being an ultra Networking n00b, but any idea what im doin wrong?

If youa re under NAT then your server socket wil lNEVER bind to that ip.

That ip is on the N AT firewall, not on your computer.

First you go to your NAT fire wall and map the socket on it to the ip of your server. You do this in the firewall’s configuration menus.

Your program will open a server socket on your server at its own ip.

When a user connects they will conenct to the NAT firewall and the firewall will foward the connection to your server.

Does that make more sense?

Haha, yes, that makes alot more sense, thank you!!! ;D

Im so close… but its still not working, I managed to connect to my server via Telnet, so I know my Port Forwarding works & theres nothing wrong with my server program, so its something wrong with my client. When I try to connect to my server via my Applet its giving me an java.security.AccessControlException… Does anyone know what my problem could be???

Yes.

Unsigned Applets are forbidden from accessing any server other then the one they were served from.

Its a security issue.

Hmmm… well, in that case, how do I make it not an unsigned applet then? btw, i appreciate all the help

google “signing applet java”. The second link:
How to Sign Applets Using RSA-Signed Certificates

You need a digital signiture, and then you need to runa program tht uses it to sign the applet.

When the applet is run by the use they will get a warning that the applet is requesting greater permissions and will have to click a doialog to approve it. If you use “self-signing” (as I ssuem you will becaue any other option costs some seriosu money) then it will wanr the user that your signiture is unverified.

I actually havent done this myself but there are folks around here who have. They can tel lyou how to generate a signiture for “self-signing.”

(Actually that might be a good one for the WIKI… )

JK

The one thing this article doesnt appear to say is how to “self-sign.”,

so does self-signing the applet cost money??? because that would definitely suck…

It does not cost money.

Googeling “signing generating key”, fifth link (looks like a good tutorial):
Generate Keys

There is also a guide in the SDK documentation. You know, along with the javadoc.

No. Thats why you self-sign.

The flip side is that the user has absolutely no assurance that your key is legitimate and thus its a bigger elap of faith for them to approve it to mess with their machine.

Edit: a quick google came up with this – http://java.sun.com/developer/technicalArticles/Security/Signed/