Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 869
  • Last Modified:

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
1 Solution
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now