Lbound and Ubound but for WinSock Control.

Posted on 2003-02-20
Medium Priority
Last Modified: 2013-11-13
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
Question by:tWiZtEr
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

Expert Comment

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

Author Comment

ID: 7991877
if thats the best way ill have to do that, but i was just wondering if there is a better way

Author Comment

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

LVL 17

Expert Comment

ID: 7992107
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:~)

Author Comment

ID: 7994897
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
LVL 17

Expert Comment

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


Expert Comment

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

Expert Comment

ID: 8901338
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?
LVL 49

Expert Comment

ID: 8964014
tWiZtEr, an EE Moderator will handle this for you.
Moderator, my recommended disposition is:

    Save as PAQ -- No Refund.

DanRollins -- EE database cleanup volunteer

Accepted Solution

YensidMod earned 0 total points
ID: 9103958
Question is PAQ'd and no points refunded.

Community Support Moderator @Experts Exchange

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Windows Script Host (WSH) has been part of Windows since Windows NT4. Windows Script Host provides architecture for building dynamic scripts that consist of a core object model, scripting hosts, and scripting engines. The key components of Window…
The purpose of this article is to demonstrate how we can use conditional statements using Python.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

770 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