Lbound and Ubound but for WinSock Control.

I have a control array, and i need to use a Lbound/Ubound equivalent but instead for my Socket() control.
This is a winsock control that changes indexes for every connection attempt or something.

here is the code: [code][vbcode]

Private Sub cmdClose_Click()
txt = txt & "******Closed All Connections!" & vbNewLine
Socket(daIndex).SendData ctvp
bSocketClosed = True
End Sub

Private Sub cmdSend_Click()
Socket(daIndex).SendData "<" & txtAlias & ">" & txtSend
txt = txt & "<" & txtAlias & ">" & txtSend & vbNewLine
End Sub

Private Sub Form_Load()
    Socket(0).LocalPort = 23
    sServerMsg = "******Listening to port: " & Socket(0).LocalPort
    txt = txt & sServerMsg & vbNewLine
End Sub

Private Sub socket_ConnectionRequest(Index As Integer, ByVal requestID As Long)
    sServerMsg = "Connection request id " & requestID & " from " & Socket(Index).RemoteHostIP
  If Index = 0 Then
   txt = txt & sServerMsg & vbNewLine
    sRequestID = requestID
    iSockets = iSockets + 1
    txt = txt & "******Number Of Connections: " & iSockets & vbNewLine
    Load Socket(iSockets)
    Socket(iSockets).LocalPort = 23
    Socket(iSockets).Accept requestID
  End If

End Sub

Private Sub socket_Close(Index As Integer)
    sServerMsg = "******Connection closed: " & Socket(Index).RemoteHostIP
    txt = txt & sServerMsg & vbNewLine
    Unload Socket(Index)
    iSockets = iSockets - 1
    txt = txt & "******Number Of Connections: " & iSockets & vbNewLine
End Sub

Private Sub socket_DataArrival(Index As Integer, ByVal bytesTotal As Long)
    If bSocketClosed = True Then
        Exit Sub
    daIndex = Index
   Dim sItemData As String
   Dim strData As String
   Dim strOutData As String
   Dim strConnect As String
    ' get data from client
    Socket(Index).GetData sItemData, vbString
    sServerMsg = sItemData
    If sItemData = cevp Then
    txt = txt & "******Connection Establishment Verification Protocol - Received" & vbNewLine
    Socket(Index).SendData cevp
    Exit Sub
    End If
    txt = txt & sServerMsg & vbNewLine
    End If
End Sub
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

You can browse the lower position by moving the index in step -1, when an error appear you can say that that was the first position (lbound) and to have the top position is a similar thing.
tWiZtErAuthor Commented:
if thats the best way ill have to do that, but i was just wondering if there is a better way
tWiZtErAuthor Commented:
oh i figured it out, i just put a for loop
For i = 1 to iSockets 'the counter of all connections.
 Send message
Next i
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

You can't use an array for handling clients becuase clients will come and goodisconnect randomly so you will end up wioth gaps in you array.  It is easier and simpler to create a class to handle your clients.

You need a dummy form with your winsock control on it.

In your class_initialize you need code like this:

Dim MyForm As Form
Public WithEvents Sock As Winsock
Dim bConnected as Boolean
Dim ID as Long
Dim ParentForm as Form

Private Sub Class_Initialize()
    Set MyForm = New frmDummy
    Set Sock = frm.Winsock1
    bConnected = False
    ' setup other defaults
    ConnectionTimeout = 30
End Sub

Private Sub Class_Terminate()
    If Connected Or ServerMode Then
        Connected = False
    End If
    Set Sock = Nothing
    UnLoad MyForm
    Set MyForm = Nothing
End Sub

So in your server you just need 1 winsock per IP address and when you get a new connection request you create a new instance of the client...

Private Sub Sock_ConnectionRequest(ByVal requestID As Long)

Dim OutsideWorld As clsClient

' Create the new client
NextClient = NextClient + 1     'Increase incomming client count

' Create a new instance of a client

Set OutsideWorld = New clsClient
clsClient.ID = NextClient
Clients.Add clsClient cStr(clsClient.ID)

Set OutsideWorld.ParentForm = Me

' now accept the request
OutsideWorld.Sock.Accept requestID

' you may wish to send a welcome
OutSideWorld.Sock.SendData "Hi......"


So you could now loop through all of your connected clients like this

Dim Client as clsClient

For Each Client in Clients
   Client.Sock.SendData "My Broadcast"

When data arrives at the client the client can signal you your form like this:

Private Sub Sock_DataArrival(ByVal bytesTotal As Long)

' First get data
Dim Message As String
Dim MessageToSend As String

' GetData can fail (error 40006) if the client sends part of the data and disconnects
On Error Resume Next
Sock.GetData Message, vbString
If Err.Number <> 0 Then Exit Sub

' send the data back to the form

ParentForm.MessageFromClient Me, ID, Message, MessageToSend

If Len(MessageToSend) > 0 Then
   Sock.SendData MessageToSend
End If

End Sub


In your form you can now handle data from the client like this.

Public Sub MessageFormClient (Client as clsClient, ID as Long, Message as String, Reply as String)

' decode the incomming data/request
if message = "GET DATA" Then
   Reply = "........"
end if

perhaps you want to evict the client

DicConnectClient Client

Public Sub DisconnectClient(Client as clsClient)
DIm ID as Long
ID = Client.ID
Clients.Remove cStr(ID) ' remove the client from the client stack.
End Sub

Hope this helps:~)
tWiZtErAuthor Commented:
hey thanks for the help. but im not super good at VB.
I dont understand what this piece of code does since i dont have anything called "Client" with a method "ADD":

NextClient = NextClient + 1     'Increase incomming client count

' Create a new instance of a client

Set OutsideWorld = New clsClient
clsClient.ID = NextClient

Clients.Add clsClient cStr(clsClient.ID)

Set OutsideWorld.ParentForm = Me
The code sample I posted was was an example of how to handle winsock without need for an array.

It works by having a class clsClient.cls (part of this code for this class) was shown in the post (you may want to add functions like handling disconnect etc).

In simple terms in your form (or global) you have a collection of clients.

Dim Clients as New Collection
Dim NextClient as Long
You walso need a counter for each new connection.  The counter acts an a unique key for each client.

You bond your server winsock to an IP and wait for connection requests.  When a connection request comes in you need code like this:

Dim Client as New clsClient

NextClient = NextClient + 1 ' create a unique ID
Client.ID = NextClient ' save the ID into the class
Set Client.ParentForm = Me ' tell the client where to pass callbacks

Now store the new Client class into the clients collection.
The cStr() turns a number into a string, in this way you have a unique key for the client.

(See using collections in your documentation)

Clients.Add Client, cStr(Client.ID)

You can remove things from connections like this:

Clients.Remove cStr(ID) ' the key to delete

Assuming you have a Winsock control with the Index set to a number.   0 is the instance on your form.  You can tell how many are loaded with Ws.count
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
Experts: Post your closing recommendations!  Who deserves points here?
tWiZtEr, an EE Moderator will handle this for you.
Moderator, my recommended disposition is:

    Save as PAQ -- No Refund.

DanRollins -- EE database cleanup volunteer
Question is PAQ'd and no points refunded.

Community Support Moderator @Experts Exchange

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming Languages-Other

From novice to tech pro — start learning today.

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.