ServerSocket still listening after StopListening

Does anyone here use the ServerSocket? I’m having problems getting it to stop listening for new connections after calling StopListening.

Even running the example web server app in Example Projects\Communication\Internet\Web Server I can still connect a telnet client to the port after starting and stopping the server.

I have tried setting the ServerSocket instance to Nil which seemed to be a way around it, however after I do that I no longer have access to ServerSocket.ActiveConnections to see if everyone has disconnected.

If you do use it, do you wrap it up in your own class that keeps track of the sockets it creates or do you have any other suggestions for using it?

I found <https://xojo.com/issue/14042> talking about it but this is dating back to 2011 and I’m worried that its been forgotten about and I dont even know if ticket additions to tickets this old even get flagged up at Xojo.

Thanks in advance.

Just thinking…are you iterating through the sockets and making sure they’re closed before you stop listening?

I’ve tried that Bob, it makes no difference as technically they arent activeconnections (established in tcpview) until ServerSocket hands off the connection to them so its actually the ServerSocket thats is allowing the connection to 8080.

Above, I have no idea what the 53035 < 60554 connection is, but this is what I see when ServerSocket is listening as well as not listening, the other connections arent even registering until a connection attempt is made, which they do even if its set to StopListening.

Below, ServerSocket is set to StopListening and I try a telnet to 8080 and I get a connection.

Sorry I forgot to mention that I dont want to disconnect activeconnections when stopping the program from listening as I would like the connections to finish what they are doing.

You could just stop returning sockets in the AddSocket event.

Just out of curiosity… what platform are you seeing this on?

Windows

Calling StopListening doesnt dispose of the already allocated TCPSockets, they are still being managed by ServerSocket and connected, though no traffic is passed through them. But the fact that they connect is a headache.

I’ve just come up against this problem though <https://xojo.com/issue/41046> so I don’t know what to do at the moment.

Ok, so I found a way that you can do this. If you haven’t already, subclass ServerSocket so that you can override the behavior of StopListening.

Make a new method in your subclass that looks like this

[code]Public Sub StopListening()
// Tell the socket to stop allocating new sockets
super.StopListening()

// Set the min sockets available and max sockets connected to zero so no more connections will be accepted
// once the current ones are done.
dim minSockets as integer = self.MinimumSocketsAvailable
self.MinimumSocketsAvailable = 0
self.MaximumSocketsConnected = 0

// Connect to the remaining sockets so they die off
for i as integer = 1 to minSockets
dim t as new TCPSocket
t.Address = “127.0.0.1”
t.Port = self.Port
t.Connect
next
End Sub
[/code]

Similarly you’ll need to override Listen if you plan on calling listen in the future to increase MinimumSocketsAvailable and MaximumSocketsConnected back to their original values.

[code]Public Sub Listen()
self.MinimumSocketsAvailable = 2
self.MaximumSocketsConnected = 200

// Must tell the super to listen
Super.Listen
End Sub
[/code]

Very nice Greg, not very elegant, but it works well. Thanks!

Problems again :frowning:

This seems to have caused a side effect of losing track of ActiveConnections.

Just for the record, I’ve removed the following as it doesn’t make any difference to the fix or this new problem:

self.MinimumSocketsAvailable = 0 self.MaximumSocketsConnected = 0

as well as removing the modification to Listen as I am not changing these values now.

If I start the program, connect a socket then call ActiveConnections.Ubound on my ServerSocket reference I get an answer of 0 (1 socket connected)

If I then stop listening, start listening then call ActiveConnections.Ubound on my ServerSocket reference I get an answer of -1 (0 sockets connected) even though my socket is still connected via telnet in another window, and can transfer data.

I’ve double checked and I’m not instantiating a new copy of ServerSocket for it to lose track of the old object.

You can see this in the attached project on <https://xojo.com/issue/46806> that I have just put in. It’s opening 1 port, if you connect to it via telnet, stop and start the web server, the value goes down to 0.

It doesn’t surprise me that serversocket forgets how many open connections it has. With a little creative work, you could keep track of this yourself though. I’ll grab the example I created for looking at this problem because I’d done most of that work already and post it shortly.

See if this satisfies your needs…

https://www.dropbox.com/s/91lmm9xl1che979/ServerSocketTest.xojo_binary_project?dl=1

Thanks Greg, I’ll take a look.

Maybe it’s time to fix the issue: Case #14042 instead of avoiding it all the time. :slight_smile:

I’m not avoiding it. ServerSocket is a messy piece of legacy code that has a sign on it that says “if it ain’t broke, don’t touch this”. :stuck_out_tongue: