ServerSocket pool of sockets - can they go into an array which contains other TCPSockets

I’m making some outgoing TCP connections using a simple class to handle the events. I would like to use the same class for incoming connections, that is easy enough to do.

The goal is to create a pool of say 5 incoming connections and 5 outgoing connections and store these connections in the same place - an array so I can iterate through all connections.

To me it makes sense that all of the connections (incoming or outgoing) are in an array for what I’m doing.

This is how I create outgoing connections from the console app, it’s a daemon designed for Linux :

This is simple enough for outgoing connections

iplist() and portlist() are arrays which contain the IP address and port numbers to use for each connection :
The Sockets() array is a property of type ClientSocket which is my TCPSocket class.

[code] for tempint = 0 to nodecount-1
// Connect to each node one by one, first adding a new socket to the array
Sockets.Append( new PeerSocket )

// Get array element number
socketid = Sockets.Ubound

// Set it up and connect
Sockets(socketid).address = iplist(tempint)
Sockets(socketid).port = portlist(tempint)
sockets(socketid).socketid = socketid
Sockets(socketid).Connect

next tempint
[/code]

When I want to send a message to all the sockets I do something like the following :

[code] for tempint = 0 to nodecount-1
// Iterate through the sockets one by one sending the message to each of them

msg = "Some meaningful binary message goes here"
Sockets(tempint).Write(msg)
Sockets(tempint).Flush

// Some messages are long - tens of KB so loop until all data is sent - NOTE - CONSOLE APP so using doevents here
// if I don't wait here then the outgoing buffer is overwritten the next time I do a .write()
while Sockets(tempint).BytesLeftToSend >0
  doevents(200)
wend

// Response from remote listener is handled by sockets events

next tempint
[/code]

Is there any way to get incoming connections from the ServerSocket AddSocket event to add the new socket into the above Sockets() array which I’m using.

This way when I need to send outgoing messages I can simply loop through all the connections in the Sockets() array as in the above example.

I know a pool of empty connections is created for the ServerSocket when it’s instantiated so I can check the IsCconnected property before using it to make sure it’s an active and current connection instead of an unused pool socket witing for a connection.

I guess what’s confusing me right now is the return value in the AddSocket event handler - I know it’s returning a socket but where is it returning it to and what’s done with it once it’s returned ?

My guess, as I can’t find any more detailed information right now is that this newly created TCPSocket is returned to the ServerSocket but it’s not clear to me how these are handled internally.

Does the ServerSocket object keep some sort of list of which sockets belong to it, would it be possible to keep these in an array which also contains outbound sockets of the same class type without having the ServerSocket interfere with them ?

So, can I use an already existing array of type TCPSocket and just place the pool of connections inside that array ?

This is for a kind of p2p type program where there are both incoming and outgoing connections, once the initial connection is made they are not treated differently at all so from the point of view of my program it doesn’t matter if the connection is outbound from the console app or an incoming connection via the ServerSocket.

I would rather handle all connections as a single array instead of having a ServerSocket pool of incoming connections and a separate array of outgoing connections.

I hope I’ve explained this in enough detail, I can clarify further if needed.

Thanks for any thoughts on this. I can handle incoming and outgoing connections completely separately if needed…

I wouldn’t. ServerSocket maintains its own array and disposes of them when it’s done. Remember, ServerSocket listens on one port and hands them off to another port when a connection is made which is very different than how sockets normally work. Keeping your own array risks memory leaks because you will never truly know when the ServerSocket is done with the socket.

Ok, thanks. I guess I will use the ServerSocket.ActiveConnections() array and handle incoming and outgoing connections separately.