How to create multiple sockets as an index
I have a VB application where winsock is used. I want to convert it to VB.Net, so as I understand I should use System.net.sockets to do this.
But the problem is in the VB application multiple sockets are created by using the index feature of the winsock . example
Load 开发者_Python百科Socket(isocket)
where isocket is an integer variable that is incremented each time, for a new connection request.
So how do I do the same, ie. open multiple sockets in vb.net?
In VB6 the Winsock is an OCX so what you are doing by calling Load Socket(Index)
is creating a new instance of the ActiveX Control. There is no such equivalent in VB.NET so what you could do is create a collection of System.Net.Sockets.Socket:
Dim collSockets As New Collection(Of System.Net.Sockets.Socket)
When you get a new connection request, create a new item in the collection:
Dim sck As New System.Net.Sockets.Socket(**Initialise Your New Socket Here**)
collSockets.Add(sck)
Then you can access your sockets by Index (zero based):
collSockets(0).Whatever()
Then we can do something like this:
dim x as integer = 0
x += 1
collSockets(x).Whatever()
That will make new socket in each event and the x will double will be like
collSocket(1).Whatever()
x+= 1
collSocket(2).Whatever()
Etc.
精彩评论