SpiderMonkey keeps changing ports upon a client connection?

Hi all, I have a question about SpiderMonkey. Is it normal that even if I declared a TCP/UDP port to use by the server and client, it switches back to another port? I have also noticed that the port increases on each application execution, probably meaning it’s not closed gracefully upon exit.

I’m asking because as it sits now, it’s nearly impossible to predict what port SpikerMonkey will use upon a client connection and so how am I supposed to forward the port on my router if it keeps changing randomly like this?

I’ve set up my SpikerMonkey server app like this:

[java]
this.server = Network.createServer(51010);
this.server.addConnectionListener(sl);
this.server.addMessageListener(sl);
this.server.start();
[/java]

… and my client app is launching like this where server_ip is like 123.123.123.123:

[java]
this.client = Network.connectToServer(server_ip, 51010);
this.client.addClientStateListener(cl);
this.client.addMessageListener(cl);
this.client.start();
[/java]

Everything works well on localhost, because I do not have any ports to forward on my router, so even if it changes port randomly, they’re all available and open locally… but on external IP, that’s where I’m kind of lost.

If I println() new client connections on the server using this:

[java]
System.out.println("SERVER connection ESTABLISHED: " + conn.getAddress());
[/java]

I’m getting this: SERVER connection ESTABLISHED: /127.0.0.1:49303
…and then the second time I launch my app, I’m getting this: SERVER connection ESTABLISHED: /127.0.0.1:49426
…and then the third time I launch my app, I’m getting this: SERVER connection ESTABLISHED: /127.0.0.1:49430

As you can see, it’s incrementing over time, even like when my app is not launched, just letting my computer idle for a couple minutes, the increment will be bigger, but it’s always changing ports. I’ve never actually seen my SpiderMonkey app use port 51010 like I defined it. I tried defining it to a different port like 59873 just to be sure it wasn’t randomly in use, but it’s still going with 49xxx and incrementing.

Anybody has any ideas of what could cause this and how to force SpiderMonkey to use the port I defined in the code?

Thx

When you set the port on the client you are setting the SERVER PORT that it will connect to. Like, every application on the face of the earth works this way. The client opens a connection to the server on whatever local port the operating systems gives out… which may be sequential. Who knows? It doesn’t matter. You do not have to tunnel this port. You do not have to tunnel anything for clients.

You only have to tunnel if you are running a server… and in that case the port is fixed.

1 Like

Oh OK I see. The port I’m seeing from the server’s point of view upon a connection is the outgoing port to reach that client, so no forwarding of course. +1’ed for making me realize I’m being stupid and/or I’m tired now lol :smiley:

OK… so that means that the issue is somewhere else then. Maybe you can help further more about this issue if you want.

I have forwarded the port in both TCP and UDP protocols on my router and I know the port is correctly forwarded because I can run a Minecraft server on that port and it’s working perfectly from outside of my LAN.

The problem is that the exact same compiled app can connect to localhost and work perfectly but will not connect to my external IP. Keep in my that a Minecraft server can run on that exact same port, same computer (but not at the same time of course!). So I was thinking it had to do with this port that is changing but as you pointed, it’s normal that the server sees a different connection port, because it’s the client’s chosen one.

Wait… If I push my stupidity a little further lol… could it be caused by having 2 CLIENT apps on the same computer trying to use the same outgoing port at the same time?

EDIT: No wait, doesn’t make sense, it works if I put “127.0.0.1” instead of my external IP. I’m really stumped. It’s pretty weird.

Inside your own network you may have trouble connecting back to your own server through your own external port. Your router probably gets confused. It may handle TCP this way just fine (which would be why Minecraft works) but not UDP.

You could test it by setting up a server and client with no UDP. Just specify -1 for the UDP port and it will turn off that transport.

Uhmmm… OK BUT on my LAN it is working just fine. [EDIT: Misread your post, now I read it correctly, sorry] As a matter of fact: it works if I put 127.0.0.1 (same with “localhost”) as server address and open a bunch of client apps. It also works just as fine if I put my 192.168.1.162 LAN IP and open a bunch of clients. Only one that freezes even the first client I try to connect with is if I put my external IP and after like 15 seconds of complete freezing, it throws: java.net.ConnectException: Connection timed out: connect

Nevertheless, I have disabled UDP by putting -1 as UDP PORT paramater on both client and server instanciation and it still freezes and eventually timeout when I try to connect to the server app via the external IP.

So, I have done a quick test case here to demonstrate the problem in high hopes you or anybody could help me resolve this by pointing the finger at something I might have overlooked. Keep it mind, it works perfectly with “127.0.0.1” AND “192.168.1.162” but NOT with my external IP.

#1 Here is my setup on my RT-N10+ router

As you can see, I’m forwarding port 5555 on both protocols to my computer on my LAN.

#2 Here are relevant (I think?) parts of my code:

SERVER SETUP:

[java]
this.server = Network.createServer(5555); // Also tried with: createServer(5555, -1)
this.server.addConnectionListener(sl);
this.server.addMessageListener(sl);
this.server.start();

        @Override
        public void connectionAdded(Server server, HostedConnection conn) {
                System.out.println("SERVER connection ESTABLISHED: " + conn.getAddress() + " / will be Player#" + conn.getId());
        }
        // THIS PRINTS OUT LIKE: SERVER connection ESTABLISHED: /127.0.0.1:52760 / will be Player#0
        // On external IP it doesn't print anything, therefore, the server does not see a connection or it's not fully made.

[/java]

CLIENT SETUP:

[java]
this.client = Network.connectToServer(server_ip, 5555); // Also tried with: connectToServer(server_ip, 5555, -1)
this.client.addClientStateListener(cl);
this.client.addMessageListener(cl);
this.client.start();
[/java]

#3 Windows firewall rules:

#4 Windows networking setup:

  • My wifi connection is set on OFFICE (the blue building icon) and I also tried HOME, doesn’t work.
  • Printers and file sharing is enabled. (I’ve read on other threads you said it could make a difference.)
  • Network discovery is enabled (not that it would actually matter would it?)

Thanks a lot for all your time, in the hope it can also help other people in the same situation but too shy to ask :stuck_out_tongue:

OK… MEGA UPDATE. I found out by restarting the router that when it is freshly rebooted, it works ONE TIME, meaning that if I do not close the server, I can connect and disconnect as many clients randomly and it always works, but the minute I close the server app and re-open it, external IP connections will NOT work anymore at all. It’s like if the EXTERNAL listening port gets stuck as “IN USE” if you will and the router will only forward LAN or local connections, but not external ones.

Is it possible that my app is not correctly closing the listening port when it is exited? This is the code I have right now and I know it’s called, because if I exit the app from the IDE, I can see the “CLIENT CLOSING…” and “SERVER CLOSING…” println()'s in the log:

[java]
@Override
public void destroy(){
// Disconnect client
if(this.client != null && this.client.isConnected()){
System.out.println(“CLIENT CLOSING…”);
this.client.close();
}

    // Close server
    if(this.server != null && this.server.isRunning()){
        System.out.println("SERVER CLOSING...");
        /*
        for(HostedConnection conn : server.getConnections()){
            conn.close("Shut Down");
        }
        */
        this.server.close();
    }
    
    // Clean up after ourselves.
    if (world != null) world.close();
    
    super.destroy();
}

[/java]

Anything else I should put or maybe do that differently? Also, is pressing ESC key the same as ALT-F4 and are they both executing the destroy function? There must be something related to how my app is shut down that prevents it from closing the listening port.

Thank you for all your inputs.

It takes a few seconds, sometimes up to 15 seconds, for a port to get flagged as free. If you try to bind to a port that is already bound to something else it will throw an exception. If it successfully binds, it isn’t the server that is the issue, its the client.

EDIT: You can also run “netstat -a -b” on a command prompt to check which processes are bound to a port. That may reveal some information for you.

1 Like

Interesting command line @jayfella, +1’ed for this information. When it’s working, I see used ports by java.exe and when it doesn’t want to connect externally I do not see any ports used by java.exe. It doesn’t tell me much about the current problem tough.

BUT, I think I have found what the issue is. It appears that my F**** ISP’s modem does not support uPnP correctly OR reverts itself after 10 minutes for whatever reason I can’t comprehend. What that means is that even if I disable the firewall on the modem, it WORKS for 10 minutes BUT THEN IT REVERTS back to firewall ON (no port forwarding) silently. I have lost like an hour just to discover this and another hour on how to fix it… I never ever would have thought a device could be set to something and roll back by itself for no reason like this and also… silently. That drove me insane.

I switched the uPnP off on the router and reset all the settings carefully on the ISP’s modem and it worked for 10 minutes and sadly, the ISP’s modem silently rolled back again to firewall ON and same thing happened, external connections would not be accepted anymore. When this happens (after the 10-ish minutes period, the only option is to reset all the settings on both devices and reboot them)

I have tested that if I connect a client to my server app and leave it communicating like sending player position every 100-200ms all along, it would NOT drop the connections. It just wouldn’t accept NEW ones.

SO TL;DR’ing:

What I just did now 30 minutes ago is to disable everything in the router that’s related to firewall or security and it’s been working great and DID NOT REVERT the modem’s firewall. It left it off. So I think it’s a uPnP thing that if you have firewall enabled on the router, the ISP’s modem follows every 10 minutes and reverts its own firewall to ON also. Now, I can still connect new clients to the server app and it works perfectly, it’s been up for 30 minutes, I can shut down the server and/or clients and re-open them, re-connect them no problem at all, it’s steady working and it just made my day.

So I think it’s related to something about the firewall on my router, just not exactly sure what it is, but Minecraft would run and not my app, so it must be something related to the UDP packets that the firewall is blocking AND it looks like it’s sending those settings ONTO the ISP’s modem and the modem is reverting its own firewall to reflect that of the router. Now that the firewall is off on the ISP’s modem AND my router, it works perfectly all the time. I’ll try and isolate the setting, but putting the router’s firewall to OFF completely works great! :smiley:

Thank you for your time both.

Problem “solved” and it looks like it had NOTHING to do with JME3 at all.