When Making instance of Winsock Control...

I have a form that has a single Winsock control on it. What i want to do is make several instances of that form so that i can utilize that winsock control on each instanced form so as to call out on the net and do certain tasks. The problem i have noticed when i do this is that it appears that only one of those running instances runs ok while the others seem to "BOG" down big time.
When making an instance of that form by:

Dim MyInstanceForm as Form1

Set MyInstanceForm = New Form1

Does this only make a single copy of the controls on that form...in this case the Winsock Control only really exists once in memory. So when i make tons of instances of this form and start to activate each Winsock on each instanced form it in fact is only accessing the single Winsock control from the original instance?  So when i start using that control then when others begin to use that control it will be back logged because 1 instance is already using it?

What would be the best solution to solve this problem?  Should i place ie. 30 different Winsock Controls (Indexed) on the Form i want instanced and for each instance i make it only uses its own pre-assigned Winsock control, depending on its instance #?  Or is there a better way that will allow me to connect to the net with multi-instances without bog down and also use up low amounts of physical and virtual memory?

I hope this makes sense.
Who is Participating?
chaos_59Connect With a Mentor Commented:
Aniston, when you let windows assign the port number it will assign the next unused port, so it SHOULDN"T be assigning only 1 port. I think you are correct in your first assumption that you are creating multiple forms, but only 1 winsock control. I haven't seen your code so I'm not positive about this. One solution would be to shell to your application from a menu option or button click. It would be like the New->window menu selection in microsoft explorer. On the menu option or button click, put the command SHELL MYAPP.EXE. Then create the .exe. When a user executes event that starts the shell it will cause a new instance of your application to start running. New form, new winsock control, with it's own port number, and completely independent from the initial application.

i guess you'd have to have 30 different winsock controls..
Put one control with Index=0, and Load them as you need them.
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

anistonAuthor Commented:
I just tried that out and apparently it still halts the 1st instance when attempting to Connect, while the 2nd instance connects and transfers fine...When i stop the the 2nd instance the 1st instance still has no clue to connect. But if i stop it and then start it again the 1st instance works fine. But as soon as i start the 2nd instance while the 1st is going the 1st instance halts immediately.

Can someone explain this?
Accepting More than One Connection Request:

The basic server outlined above accepts only one connection request. However, it is possible to accept several connection requests using the same control by creating a control array. In that case, you do not need to close the connection, but simply create a new instance of the control (by setting its Index property), and invoking the Accept method on the new instance.
The code below assumes there is a Winsock control on a form named sckServer, and that its Index property has been set to 0; thus the control is part of a control array. In the Declarations section, a module-level variable intMax is declared. In the form's Load event, intMax is set to 0, and the LocalPort property for the first control in the array  is set to 1001. Then the Listen method is invoked on the control, making it the "listening control. As each connection request arrives, the code tests to see if the Index is 0 (the value of the "listening" control). If so, the listening control increments intMax, and uses that number to create a new control instance. The new control instance is then used to accept the connection request.
Private intMax As Long

Private Sub Form_Load()
      intMax = 0
      sckServer(0).LocalPort = 1001
End Sub

Private Sub sckServer_ConnectionRequest _
(Index As Integer, ByVal requestID As Long)
      If Index = 0 Then
            intMax = intMax + 1
            Load sckServer(intMax)
            sckServer(intMax).LocalPort = 0
            sckServer(intMax).Accept requestID
            Load txtData(intMax)
      End If
End Sub

anistonAuthor Commented:
  Thank you for that wonderful answer, but that only solves the problem if you are playing "Server"...What i am trying to do is connect and "get" and interpret data from multiple sites using multiple sockets, as my question outlined. For example, when you use Netscape Navigator you can have 1 window connecting and transfering HTML code for Experts-Exchange but at the same time you can have another instance Netscape window connecting and transfering a page from www.Nasa.gov.  When i attempt to do this the one instance does it fine but the other completely halts and freezes indefinitely.  I think it may have something to do with the Winsock's port #. I allow Windows to assign one for me rather than giving it one for each instance. So, what i am thinking (hypothesising) is that Windows has assigned my app 1 port # for all the instances i make so when i attempt to connect to other sites via the instance's Winsock control it is using the app's single port # and when others try to use it Window's tells them that it is currently busy and can't be processed.  Any ideas anyone?
anistonAuthor Commented:
I will post some sample code for all of you to try out shortly. Please come back in a few hours: 6-7pm EST
Aniston, I got your same problem also. I am now wondering if it is the winsock control problem because I got a third party winsock which is no problem at all.
I have given up the microsoft winsock control now. Surely, I would like
someone can answer this question and I can use the microsoft winsock control again. :>
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.