What might cause ServerSocket.Listen to fail?

I have a ServerSocket that I’m initializing with a Port and MaxumumSocketsConnected as below. However, after calling Listen(), the IsListening property is False. What might be the cause of this?

This issue is occurring on a Windows Server 2012 machine with firewall disabled for the app in question. I don’t want to share the exact port number I’m using, but it is above 1024. There is no exception thrown.

MyServerSocket = New ServerSocket
MyServerSocket.Port = 5555
MyServerSocket.MaxumumSocketsConnected = 20
MyServerSocket.Listen

If MyServerSocket.IsListening Then
  MsgBox("MyServerSocket is listening")
Else
  MsgBox("MyServerSocket is not listening")
End

What version of xojo?
Is this code running as a service?

2017r1. No, it’s running inside a program.

Trap the Error event using AddHandler. I’d say the port is already in use, but you’ll be able to tell from the ErrorCode parameter in Error.

I think the issue is probably related to antivirus/os protection/firewall.

However, there were some bugs in older versions of Xojo (which you may be using) in which ServerSocket.isConnected() wouldn’t be immediatly true. Try adding some delay / or polling before you check the value.

There is an event, ServerSocket.AddSocket that says “This is called when the ServerSocket first begins listening.”. Did this bug affect the value of IsListening in that event? I would expect that implementing this event is more reliable than arbitrary delays.
http://documentation.xojo.com/api/networking/serversocket.html#serversocket-addsocket

I may be misremembering the bug - perhaps it was with regular Sockets and not ServerSockets? I can’t find the bug report in feedback, but I think the behavior also depending on the # of CPUs - I discovered it when testing in a VM that had only a single CPU Core.

But this sounds like a documentation bug: With a ServerSocket, “listening” means “I’m ready to accept incoming connections” but does not mean “an incoming connection is being made” (that’s what AddSocket is for).

On second thought, I think I’m wrong - AddSocket() is called before a connection is made. It’s the Socket (not ServerSocket) which gets the .Connected() event when a connection is incoming. Nevermind!

I’ve just tested it, and it’s not a documentation error. I’ve also tested that IsListening is true in this event (2019r1.1 and 2017r1.1 Mac). I would be curious if this helps OP, so I’ve uploaded my test project: https://xojo.io/ServerSocketTest.zip

This seems like the correct way to determine that a ServerSocket is ready to listen.

Ok, after a bit of digging and testing on my W2012 server, take a look at Example Projects\Communication\Internet\ServerSocketServerTest.xojo_binary_project you have to implement AddSocket and actually allocate the sockets as shown in the example code there (which is mentioned in the docs).

The ServerSocket doc page could do with a big red warning mentioning that you have to implement AddSocket at minimum as shown in the example further down the page to guarantee that everything works as expected otherwise you can end up with random NOE errors and random IDE drops into debugger for no apparent reason and no ports actually listening.