ServerSocket problems

I think this is not the problem here: with limited resources, one might expect failed connections, slow responses, etc., but a StackOverflowException doesn’t make sense and clearly seems like a bug to me. The fact that this doesn’t happen on macOS (at least in my testing) also points to it being a bug.

Thanks to @jury_buono1 for reporting it.

1 Like

The ServerSocket manages a pool of TCPSockets that are set to the listen state.

  • The ServerSocket.AddSocket event is where you create the TCPSockets that ServerSocket sets to listen.
  • Callling ServerSocket.Listen causes ServerSocket.AddSocket to be called 10 + ServerSocket.MinimumSocketsAvailable times.
  • When a client connects the socket it connects to transitions from the listen state to the connected state and is no longer available to the listening pool.
  • Each time ServerSocket accepts a new client connection the listening pool is depleted by 1.
  • When the number of sockets in the listening pool falls below MinimumSocketsAvailable, AddSocket is again called 10 + MinimumSocketsAvailable times to replenish the listening pool.

MaximumSocketsConnected is the maximum number of sockets ServerSocket allows to be in the connected state simultaneously.

  • If MaximumConnectedSockets is set to 4 and 4 clients have connected and no clients have disconnected, ServerSocket will reject the next client that attempts to connect, although there are at least 6 sockets left in the listening pool.
  • When a client disconnects, the number of connected sockets falls below MaximumSocketsConnected and ServerSocket will accept a new connection.
1 Like

Try this

SUB TCPSocket.DataAvailable()
  static isActive as boolean = False

  if isActive then return
  isActive = True

  app.arrayDati.Add(me.ReadAll)
  me.Write("reply")

  isActive = False
END

with this variant I have the same problem (I have the problem even if there is no code in the available data)

SUB TCPSocket.DataAvailable()
  static isActive as boolean = False

  if isActive then return
  isActive = True

  app.arrayDati.Add(me.ReadAll)
  me.Write("reply")

  isActive = False
END

a clarification for those who want to reproduce the problem,
the exception is raised much earlier if there are 4 or 5 clients calling the server

i modified the examples from issue

i’m tring your changes on client part (with slovely creation of new socket )

i’have modify the code of server increasing the number of max connection

srvSK.Port=6000
srvSK.MinimumSocketsAvailable=100
srvSK.MaximumSocketsConnected=10000
srvSK.NetworkInterface=System.NetworkInterface(PopupMenu1.SelectedRowIndex)
srvSK.Listen

Me.Enabled=False

and the stack exception is thrown even before and exactly at the time the clients try to create connections!!!
even worse, we are not even in the transmission phase, an even bigger problem because it exposes the server to a DOS attack!!

if you want to reproduce the problem, start 5 clients and connect them to the server at the same time

i not get that stack exeption. (i used Xojo 2024r1.1 started from ide)
have you also add Super.Constructor in the skAscolto Constructor in Server Project?

i opened 5 compiled client exe and start one by one.

1 Like

I didn’t manage to get the stackoverflow to happen. Somehow this must be reproducable.

do you have try with 5 clients at the same time ? (or not)

(i’m using your code with only max connection correction )

Could this be because they all connected from the same machine to the same machine?

So that remoteaddress is somehow the same and causes a stackoverow in the server side?

not perfect at the same time but yes i clicked the button for 1000 connections in each app.

btw i use Windows 11

1 Like

Yes but on macos. Again this is not a realistic scenario from the same machine to the same machine, but could be in some use cases.

Just to be clear does this also happen with multiple differ Machines to a songle server so that the server gets the stackoverflow exception?

i tried this with a server (w2019) in other network via internet

Interesting. So this is probably havoc, now please try the following while only setting the data and calling another method, nothing more.

DataAvailable:

self.Buffer = self.buffer + me.readall // just store the data
Processdata // this method can run next to the datavailable when reentry happens.

ProcessData method can basicly do anything like showing the data somewhere then clearing it from the buffer in the shortest way possible.

now i tried another scenario

i have chamge max concurred connection on server like this

srvSK.Port=val(txtPort.Text)
srvSK.MinimumSocketsAvailable=100
srvSK.MaximumSocketsConnected=10000
srvSK.NetworkInterface=System.NetworkInterface(PopupMenu1.SelectedRowIndex)
srvSK.Listen

then i have change the number of requested connection from client to 3000
using client modified from MarkusR that estabilish connetion with a time and not with for next cycle

If SocketObjectCount < 3000 Then 
  NewConnection
  SocketObjectCount=SocketObjectCount+1
End If

and with only one client the server generate exception in connectted event when client try to make connections

sorry i dont undertsand …

i put this ind dataavailable

self.Buffer = self.buffer + me.readall // just store the data

and then ?

it may be because of re-entry of the same method.

if there was only a buffering in the DataAvailable event as @Greg_O tried to suggest you may get away without the exception.
(i do think xojo must fix it somehow, but we need to find the cause first).

It could be that the DataAvailable is called prematurely again and again instead of the other stuff happening.

Ok, in the second scenario, server and One client that open 3000 connectionts , the exception on server Is raised on connected event

could it be a network driver issue?

i not get an exception in xojo server

Network error on same host Is improbable , then

it can be anything,
the big problem is that these errors cannot be intercepted and therefore cannot be managed by code and this can generate the loss of the data received