Click here to Skip to main content
15,891,513 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi my name is vishal for past 1 week i have been doing some research and with little success on how to create control array of winsock control in c# windows forms.

I was able to bring winsock control to my toolbox in c# windows forms through toolbox->choose items->COM Components-> Microsoft Winsock Control,version 6.0.
I doing client-server program and that i have to use Winsock control only for this purpose since it is my BOSS order's.

Now that i am on transition from vb6 to C# windows forms. In vb6 given below is vb6 code of control array of winsock control which i was able to easily create and manipulate in vb6:
VB
Private Sub Form_Load()
    Dim x As Integer
    For x = 1 To 20    'This will allow a maximum of 20 clients to be connected at once
        Load sckClient(x)
        sckClient(x).Close
    Next
    StartListen
End Sub
Private Function StartListen()
    sckServer.Close
    sckServer.LocalPort = 25000
    sckServer.Listen
End Function

where sckClient is name of my winsock control in vb6.
Given below is my c# code translation/interpretation of above vb6 code:
C#
private void StartListen()
        {
            sckServer.Close();
            sckServer.LocalPort = 25000;
            sckServer.Listen();
        }
 private void Form1_Load(object sender, EventArgs e)
        {
            int x;
            for (x = 1; x == 20; x++)
            {
                
              
                StartListen();
            }
        }

In above code in c# i get stuck at translation/interpretation of vb6 line to c# windows forms
VB
Load sckClient(x)

In c# windows forms i find it nearly impossible to create control array of winsock control in c# windows forms.
I am using namespaces in my c# windows forms: using System.Net;
using System.Net.Sockets;
I have browsed through articles in net of how to create control arrays in c# windows forms.

There are solutions but not how to create control arrays(or arrays) of winsock control in c# windows forms.
Can anyone help me please?! Can anyone help me/guide me on how to create control array of winsock control in c# windows forms?
I just need a sample on how to create a control array of winsock control in c# windows forms and be able to some code manipulations using control array of winsock control in c# windows forms. Can anyone help me please!? Any help/guidance in solving of this problem would be greatly appreciated!
Posted

1 solution

You should probably be using a more modern Socket control, which can do everything a WinSock can, just differently.

However, if you must use WinSock, and you have added WinSock controls to your toolbox, then it's not complex.
Start by looking at what VS does for you to add a Winsock.
1) Drag one onto your form. It will be called axWinsock1
2) Go to your code view, and in any method use the variable: just type it's name.
3) Right click it, and select "Go to definition".
4) You will be looking at the Designer file, at the line:
C#
private AxMSWinsockLib.AxWinsock axWinsock1;

DO NOT MAKE ANY CHANGES TO THIS FILE.
5) Right click axWinsock1 again, and select "Find all references"
You will get a new symbol results pane showing you exactly what code is needed to create an instance.
All you have to do now is duplicate that code in your own file, but using an array of controls instead:
C#
private AxMSWinsockLib.AxWinsock[] mySockets = new AxMSWinsockLib.AxWinsock[20];
...
for (int i = 0; i < 20; i++)
   {
   mySockets[i] = new AxMSWinsockLib.AxWinsock();
   ((System.ComponentModel.ISupportInitialize)(this.axWinsock1)).BeginInit();
   mySockets[i].Enabled = true;
   ...
   }


Or at least, that's the theory - I've not tried, but it should work fine!
 
Share this answer
 
Comments
Member 10248768 23-Jun-14 1:42am    
Thank Mr.OriginalGriff
I just now tried your solution and It Works! Thank you and God Bless You Mr.OriginalGriff You are a TRUE Life Saver for me. This is what i wanted exactly! Thank you!
OriginalGriff 23-Jun-14 3:18am    
You';re welcome!
Member 10248768 4-Jul-14 5:02am    
Dear Mr.OriginalGriff
Sorry for troubling you again
I tried your solution again but my question is after creating control array named:mySockets for axWinsock1(winsock control in my c# windows form) when i click my properties windows/form properties windows i should get mySockets[0] axMSWinsockLib.axWinsock but instead why i am still getting axWinsock1 axMSWinsockLib.axWinsock on my properties window. Kindly help me/clarify this final doubt/question for me Please? I hope i get your reply! If you want can i post my entire code(even designer.cs code) of creation of axWinsock1 and mySockets so that it may help you/me to find the mistakes and fix it in order to get required output. Reply please! i am waiting for your reply!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900