Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


When Making instance of Winsock Control...

Posted on 1999-07-24
Medium Priority
Last Modified: 2013-11-25
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 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.
Question by:aniston
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 10

Expert Comment

ID: 1527135
i guess you'd have to have 30 different winsock controls..

Expert Comment

ID: 1527136
Put one control with Index=0, and Load them as you need them.

Author Comment

ID: 1527137
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?
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Expert Comment

ID: 1527138
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


Author Comment

ID: 1527139
  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  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?

Author Comment

ID: 1527140
I will post some sample code for all of you to try out shortly. Please come back in a few hours: 6-7pm EST

Expert Comment

ID: 1527141
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. :>

Accepted Solution

chaos_59 earned 300 total points
ID: 1527142
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.


Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

670 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question