teamtek3
02-18-2008, 01:11 PM
I am having a problem that I can't seem to figure out on my own. I have an array of winsock controls that loads a new control whenever the current winsock control receives a connection. Hence, anytime someone connects to the program, a new winsock control is loaded and starts to listen on the local port for connections allowing multiple users to connect at any time. When the user disconnects, the winsock control that they were using is unloaded. My problem is that I need a way to keep track of which winsock controls are being used so that if one person updates something on the program I can send it to the other users without having to try to send it to a hundred winsock index numbers in some kind of loop. Any help is appreciated!!
loquin
02-18-2008, 01:56 PM
Have you considered maintaining a separate dynamic array of boolean flag values? Whenever you add a new winsock control, add an associated flag array (redim preserve). You could even make the array an array of UDT, containing the index number, as well as the port number of the associated winsock control.
When you add a winsock control, update the flags array (add an element, set the flags value to true, and add the port number if you're using it. Likewise, when you unload the control, set the flag value to false. (If you're using the port number in a UDT, you wouldn't flush it. That way, when adding a new value, you would first search the flag array for a flag value equal to false. If you find one, re-use its port number. )
teamtek3
02-18-2008, 02:03 PM
i'm not fully sure if that would work. I am just trying to keep track of the winsocks that are being used so that I can set up a procedure to send all the winsocks data.
loquin
02-18-2008, 10:40 PM
Well, this approach would keep track of the winsocks that are being used.
When adding a new winsock control you would first loop through the existing flags array, looking for a flag value of False. If you find the false flag, you have a "hole" in your array of winsock controls that you can use. Grab the port number, add the new winsock control, using the port number and index number from the flags array, and set the flag to true. On the other hand, if you searched the entire array and there were no "holes," you need to add a new array element, set it's flag to true, and set the port number to the current highest port number + 1. Then, create your new winsock control, using the new port number and index number.
When unloading a winsock control, just be sure to set the flags array at that index number to false.
mkaras
02-19-2008, 12:40 AM
teamtek3:
And may I add....
When you want to send data to all the winsocks that are in use it is as simple as scanning down the list that loquin has suggested to those have the "in use" flag set and sending to that particular winsock.
This may seem very obvious but you may have missed this.
dilettante
02-19-2008, 01:10 AM
I'd take it even further.
Most of the time creating a reliable server based on the Winsock control requires a bit of scut work that isn't related to the application logic. For TCP based applications you generally need to at least add framing symbols to the outbound stream as well as buffer and deblock received data stream segments as they arrive.
You can take that logic along with an "in use" indicator to wrap the Winsock control within a UserControl. Then you can have an InUse property to track that status fairly cleanly. For that matter it is sometimes handy to have a Timer control associated with each Winsock control, so just wrap that right in there too along with Timer event processing.
Instead of a raw DataArrival event your form's logic could receive a higher level MessageArrival event instead. This could present the received messages to you already parsed into subfields, etc. In the form you'd have a Control Array of your MessageSock controls instead of the raw Winsock controls.
Encapsulation in such a manner can help keep your form's logic clearer and easier to maintain.
By the way...
Unloading and reloading Winsock controls is seldom worth the overhead. By resetting a few simple properties before or after each use a Winsock control is perfectly reusable, and the Accept method clears these for you in a server.