Server runs perfectly on a closed port

Hi, what I’m trying to do now with my SpiderMonkey server is test whether the server is running or not, and throw back an exception if uh, if an exception is thrown back. So I have a class (testServer.class, if ya must know), all it does is tries to create a client, and if that fails, just warn the user that the server is not running correctly.



[java]protected boolean test(int port) {



try {

Client testClient = Network.connectToServer(“localhost”, port);

testClient.start();



System.out.println("Server is running on port " + port );

connected = true;

} catch (Exception e) {

e.printStackTrace();

System.out.println("Server could not start, specified port may not be open ");

connected = false;

}



return connected;

}



[/java]



The trouble is, it works, even if i enter a close port. I have another client to connect in another project and that fails, like it should when I enter a closed port… I am so confused right now…

Not sure what to tell you. connectToServer() opens at least one TCP socket to the specific port. So for some reason, on localhost your OS is letting you setup a connection to that port.



You may have to add a connection and error listener to the client and wait for either a connected event or an error. That covers the case where you try to connect to a port your OS is coincidentally listening on but it’s not an SM server and so the connection setup will never complete.



I’m sort of surprised you didn’t see errors at least in the log, though.

1 Like

No errors, that ‘connected’ boolean comes out true.

@javagame said:
No errors, that 'connected' boolean comes out true.


Yes, but a socket being open and a successful connection to an SM server are two different things. You have to register a connection and error listener to know if there is a real SM server there or you just happened to connect to some other server using that port.

You can also test by opening a command line and trying to telnet to localhost with the port you are trying to use. If that doesn't reject the connection right away then something else on your machine is listening on that port.

Well, I can send messages over, it works on any port I try but not if i try the same port on the real game client… I dont have telnet installed, I could install it but…

@javagame said:
Well, I can send messages over, it works on any port I try but not if i try the same port on the real game client... I dont have telnet installed, I could install it but....


What OS are you running that doesn't have a telnet client? Even windows has one.
@javagame said:
Well, I can send messages over, it works on any port I try but not if i try the same port on the real game client... I dont have telnet installed, I could install it but....


Is your test app also starting a server on the same port maybe? Or when you say "real game client" do you mean from the same machine or a different one.

Note that many of the ports you can connect to on localhost will be unavailable from another machine.

I’m actually running windows 7. The ‘real game client’ is running on the same machine, but I’m going to try this out at school today.

Windows 7 has telnet… it’s just disabled by default. http://www.fettesps.com/windows-7-enable-telnet/



You can also use a command called netstat to see what ports are open… but it takes a little interpretation.



It a local game client fails then it would be interesting to see where/how it fails. Is it during connect or when it actually negotiates the server connection. If the former then I say something is really fishy with your test program.