cWinsock Properties Reference
Properties List
| Property Name | Type | Access | Description |
|---|---|---|---|
State | WinsockState | Read-only | Current Socket state |
Protocol | WinsockProtocol | Read/Write | Protocol type (TCP/UDP) |
RecvBuffer | Byte() | Read/Write | Custom receive buffer |
LocalPort | Long | Read/Write | Local port |
RemoteHost | String | Read/Write | Remote hostname |
RemotePort | Long | Read/Write | Remote port |
RemoteHostIP | String | Read-only | Resolved remote IP address |
LocalHostName | String | Read-only | Local hostname |
LocalIP | String | Read-only | Local IP address |
Tag | String | Read/Write | User-defined tag |
UserData | Variant | Read/Write | User-defined data |
SocketHandle | Long | Read-only | Socket handle |
BytesReceived | Long | Read-only | Available data byte count |
IsServer | Boolean | Read-only | Whether in server mode |
IsAcceptedClient | Boolean | Read-only | Whether accepted by server |
ParentServer | cWinsock | Read-only | Parent server object (client only) |
Clients | Collection | Read-only | Collection of all connected clients (server only) |
ClientCount | Long | Read-only | Client connection count (server only) |
CurrentUser | Variant | Read/Write | Bound username (user binding feature) |
CurrentUserToken | String | Read/Write | User authentication token (user binding feature) |
CurrentUserInfo | cJson | Read/Write | User extended info (user binding feature) |
CountUsers | Long | Read-only | Current bound user count (user binding feature) |
CountGroups | Long | Read-only | Current group count (user binding feature) |
State Property
Description
Returns the current Socket state.
Syntax
Property Get State() As WinsockStateReturn Values
| Constant | Value | Description |
|---|---|---|
sckClosed | 0 | Closed |
sckOpen | 1 | Open (after UDP binding) |
sckListening | 2 | Listening (TCP server) |
sckConnectionPending | 3 | Connection pending |
sckResolvingHost | 4 | Resolving hostname |
sckHostResolved | 5 | Hostname resolved |
sckConnecting | 6 | Connecting |
sckConnected | 7 | Connected |
sckClosing | 8 | Closing |
sckError | 9 | Error occurred |
Usage Example
Private Sub cmdConnect_Click()
If m_oClient.State = sckClosed Then
m_oClient.Connect "127.0.0.1", 8080
Else
MsgBox "Socket not closed, current state: " & GetStateName(m_oClient.State)
End If
End Sub
Private Function GetStateName(ByVal eState As WinsockState) As String
Select Case eState
Case sckClosed: GetStateName = "Closed"
Case sckOpen: GetStateName = "Open"
Case sckListening: GetStateName = "Listening"
Case sckConnected: GetStateName = "Connected"
Case sckClosing: GetStateName = "Closing"
Case sckError: GetStateName = "Error"
Case Else: GetStateName = "Unknown"
End Select
End FunctionProtocol Property
Description
Gets or sets the protocol type used by the Socket.
Syntax
Property Get Protocol() As WinsockProtocol
Property Let Protocol(ByVal Value As WinsockProtocol)Values
| Constant | Value | Description |
|---|---|---|
sckTCPProtocol | 1 | TCP protocol (reliable, connection-oriented) |
sckUDPProtocol | 2 | UDP protocol (unreliable, connectionless) |
Usage Example
' Set to TCP protocol
m_oSocket.Protocol = sckTCPProtocol
' Set to UDP protocol
m_oSocket.Protocol = sckUDPProtocol
' Check current protocol
If m_oSocket.Protocol = sckTCPProtocol Then
Debug.Print "Using TCP protocol"
Else
Debug.Print "Using UDP protocol"
End IfNotes
- Can only be modified when
State = sckClosed - After modification, need to call
Connect(),Listen()orBind()again
RecvBuffer Property
Description
Sets or gets custom receive buffer. Usually used for advanced scenarios.
Syntax
Property Let RecvBuffer(ByRef Value() As Byte)Usage Example
' Set custom buffer
Dim baCustomBuffer() As Byte
ReDim baCustomBuffer(0 To 8191) ' 8KB buffer
m_oSocket.RecvBuffer = baCustomBufferLocalPort Property
Description
Gets or sets local port number.
Syntax
Property Get LocalPort() As Long
Property Let LocalPort(ByVal Value As Long)Usage Example
' Set local port (must be done before calling Connect/Listen/Bind)
m_oServer.LocalPort = 8080
m_oServer.Listen
' Get actual bound port
Debug.Print "Local port: " & m_oSocket.LocalPortNotes
- Can only be set when
State = sckClosed - Range: 0-65535
- 0 means auto-assigned by system
RemoteHost Property
Description
Gets or sets remote hostname (domain name or IP).
Syntax
Property Get RemoteHost() As String
Property Let RemoteHost(ByVal Value As String)Usage Example
' Set remote host (can use domain name)
m_oClient.RemoteHost = "example.com"
m_oClient.RemotePort = 80
m_oClient.Connect
' Use IP address
m_oClient.RemoteHost = "192.168.1.100"
m_oClient.RemotePort = 8080
m_oClient.Connect
' Get remote hostname
Debug.Print "Remote host: " & m_oClient.RemoteHostRemotePort Property
Description
Gets or sets remote port number.
Syntax
Property Get RemotePort() As Long
Property Let RemotePort(ByVal Value As Long)Usage Example
' Set remote port
m_oClient.RemotePort = 8080
' Get remote port
Debug.Print "Remote port: " & m_oClient.RemotePortRemoteHostIP Property
Description
Gets resolved remote IP address (read-only).
Syntax
Property Get RemoteHostIP() As StringUsage Example
Private Sub m_oClient_Connect(Client As cWinsock)
Debug.Print "Connection successful!"
Debug.Print "Hostname: " & Client.RemoteHost
Debug.Print "IP address: " & Client.RemoteHostIP
Debug.Print "Port: " & Client.RemotePort
End SubSpecial Case: UDP Server Virtual Client
Private Sub m_oUdp_DataArrival(Client As cWinsock, ByVal bytesTotal As Long)
' In UDP server mode, virtual client's RemoteHostIP returns sender IP
Debug.Print "Received from " & Client.RemoteHostIP & ":" & Client.RemotePort & " data"
End SubLocalHostName Property
Description
Gets local hostname.
Syntax
Property Get LocalHostName() As StringUsage Example
Debug.Print "Local hostname: " & m_oSocket.LocalHostNameLocalIP Property
Description
Gets local IP address.
Syntax
Property Get LocalIP() As StringUsage Example
Debug.Print "Local IP: " & m_oSocket.LocalIPTag Property
Description
User-defined tag for identifying objects.
Syntax
Property Get Tag() As String
Property Let Tag(ByVal Value As String)Usage Example
' Set tag for each client
Private Sub m_oServer_ConnectionRequest(Client As cWinsock, ByRef DisConnect As Boolean)
' Server automatically sets Tag to "#1", "#2", "#3"...
' Can also customize
Client.Tag = "Client-" & Client.RemoteHostIP
Debug.Print "New client Tag: " & Client.Tag
End Sub
' Find client by Tag
Private Function FindClientByTag(ByVal sTag As String) As cWinsock
Dim oClient As cWinsock
For Each oClient In m_oServer.Clients
If oClient.Tag = sTag Then
Set FindClientByTag = oClient
Exit Function
End If
Next
Set FindClientByTag = Nothing
End FunctionUserData Property
Description
User-defined data storage, can store any type of data.
Syntax
Property Get UserData() As Variant
Property Let UserData(ByVal Value As Variant)
Property Set UserData(ByVal Value As Variant)Usage Example
' Store string
m_oClient.UserData = "User info: John"
' Store number
m_oClient.UserData = 12345
' Store object
Dim oUserInfo As New CUserInfo
oUserInfo.Name = "John"
oUserInfo.Age = 25
Set m_oClient.UserData = oUserInfo
' Read data
Dim sInfo As String
sInfo = m_oClient.UserData
Debug.Print sInfo
' Read object
Dim oUserData As CUserInfo
Set oUserData = m_oClient.UserData
Debug.Print oUserInfo.Name & ", " & oUserData.AgeAdvanced Usage: Client Session Data
Private Type tSessionData
LoginTime As Date
LastActivity As Date
LoginAttempts As Long
Authenticated As Boolean
End Type
Private Sub m_oServer_ConnectionRequest(Client As cWinsock, ByRef DisConnect As Boolean)
Dim tSession As tSessionData
tSession.LoginTime = Now
tSession.LastActivity = Now
tSession.LoginAttempts = 0
tSession.Authenticated = False
Client.UserData = tSession
End Sub
Private Sub CheckSessionTimeout()
Dim oClient As cWinsock
Dim tSession As tSessionData
For Each oClient In m_oServer.Clients
tSession = oClient.UserData
If DateDiff("s", tSession.LastActivity, Now) > 300 Then ' 5 minutes of inactivity
Debug.Print "Session timeout, disconnecting: " & oClient.Tag
oClient.Close_
End If
Next
End SubSocketHandle Property
Description
Gets underlying Socket handle (read-only).
Syntax
Property Get SocketHandle() As LongUsage Example
' Get Socket handle
Debug.Print "Socket handle: " & m_oSocket.SocketHandle
' Used for advanced operations (e.g., Win32 API interaction)
If m_oSocket.SocketHandle <> 0 Then
Call SomeWin32Function(m_oSocket.SocketHandle)
End IfBytesReceived Property
Description
Gets available byte count in receive buffer (read-only).
Syntax
Property Get BytesReceived() As LongUsage Example
Private Sub m_oClient_DataArrival(Client As cWinsock, ByVal bytesTotal As Long)
Debug.Print "Event notification: " & bytesTotal & " bytes"
Debug.Print "Buffer total: " & Client.BytesReceived & " bytes"
' Read only partial data
If Client.BytesReceived > 100 Then
Dim sData As String
Client.GetData sData, vbString, 100 ' Read only first 100 bytes
Debug.Print "Read partial data: " & sData
End If
End SubIsServer Property
Description
Determines if current object is in server mode (read-only).
Syntax
Property Get IsServer() As BooleanUsage Example
Private Sub m_oServer_DataArrival(Client As cWinsock, ByVal bytesTotal As Long)
If Client.IsServer Then
Debug.Print "Data from server"
Else
Debug.Print "Data from client"
End If
End SubIsAcceptedClient Property
Description
Determines if current object is a client accepted by server (read-only).
Syntax
Property Get IsAcceptedClient() As BooleanUsage Example
Private Sub SomeFunction(oSocket As cWinsock)
If oSocket.IsAcceptedClient Then
Debug.Print "This is a client accepted by server"
Debug.Print "Parent server: " & oSocket.ParentServer.Tag
Else
Debug.Print "This is an independent client or server object"
End If
End SubParentServer Property
Description
Gets parent server object (only valid for server-accepted clients).
Syntax
Property Get ParentServer() As cWinsockUsage Example
Private Sub m_oServer_ConnectionRequest(Client As cWinsock, ByRef DisConnect As Boolean)
' Server sets ParentServer
' Client can access parent server
Debug.Print "New client's parent server: " & Client.ParentServer.Tag
End SubAdvanced Usage: Client Broadcast Message
' In some client event, broadcast to other clients through parent server
Private Sub ClientBroadcastToOthers(ByVal oSender As cWinsock, ByVal sMessage As String)
Dim oClient As cWinsock
For Each oClient In oSender.ParentServer.Clients
If Not oClient Is oSender Then ' Don't send to self
oClient.SendData sMessage
End If
Next
End SubClients Property
Description
Gets collection of all connected clients (only valid for server objects).
Syntax
Property Get Clients() As CollectionUsage Example
' Iterate through all clients
Private Sub ListAllClients()
Debug.Print "Current connections: " & m_oServer.ClientCount
Dim oClient As cWinsock
For Each oClient In m_oServer.Clients
Debug.Print oClient.Tag & ": " & oClient.RemoteHostIP & ":" & oClient.RemotePort
Next
End Sub
' Find specific client
Private Function FindClientByIP(ByVal sIP As String) As cWinsock
Dim oClient As cWinsock
For Each oClient In m_oServer.Clients
If oClient.RemoteHostIP = sIP Then
Set FindClientByIP = oClient
Exit Function
End If
Next
Set FindClientByIP = Nothing
End Function
' Broadcast to all clients
Private Sub BroadcastToAll(ByVal sMessage As String)
Dim oClient As cWinsock
For Each oClient In m_oServer.Clients
On Error Resume Next
oClient.SendData sMessage
On Error GoTo 0
Next
End SubClientCount Property
Description
Gets current number of connected clients (read-only, only valid for server objects).
Syntax
Property Get ClientCount() As LongUsage Example
' Display connection count
lblClientCount.Caption = "Current connections: " & m_oServer.ClientCount
' Limit maximum connections
Private Sub m_oServer_ConnectionRequest(Client As cWinsock, ByRef DisConnect As Boolean)
If m_oServer.ClientCount >= m_lMaxClients Then
Debug.Print "Maximum connection limit reached: " & m_lMaxClients
DisConnect = True
End If
End SubCurrentUser Property
Description
Gets or sets bound username (for user binding feature). After binding user with BindUser method, this property is automatically set to the username.
Syntax
Public CurrentUser As VariantUsage Example
' Bind user when client connects
Private Sub m_oServer_DataArrival(Client As cWinsock, ByVal bytesTotal As Long)
Dim sData As String
Client.GetData sData
If Left$(sData, 6) = "LOGIN:" Then
Dim sUsername As String
sUsername = Mid$(sData, 7)
' Bind user
m_oServer.BindUser sUsername, Client
' Verify binding successful
Debug.Print "Client.CurrentUser = " & Client.CurrentUser
End If
End Sub
' Check if user is logged in
Private Sub CheckUserLogin(ByVal oClient As cWinsock)
If LenB(CStr(oClient.CurrentUser)) = 0 Then
Debug.Print "User not logged in"
Else
Debug.Print "Current user: " & oClient.CurrentUser
End If
End SubDifference from Tag Property
| Property | Purpose | Setting Method |
|---|---|---|
Tag | Identifies socket connection (e.g., "#001") | Auto-assigned or manually set |
CurrentUser | Identifies logged-in user | Bound via BindUser |
Auto Cleanup
When client disconnects (calls Close_ or object is destroyed), system automatically unbinds user, no manual handling needed.
CurrentUserToken Property
Description
Gets or sets user authentication token (for user binding feature). When binding user with BindUser method and passing Token parameter, this property is automatically set to the corresponding value.
Syntax
Public CurrentUserToken As StringUsage Example
' Bind user with Token
m_oServer.BindUser "alice", Client, "jwt_token_xyz123"
' Verify Token later
If Client.CurrentUserToken = "jwt_token_xyz123" Then
Debug.Print "Token verification passed"
End If
' Get current user's Token
Debug.Print "User " & Client.CurrentUser & "'s Token: " & Client.CurrentUserTokenCurrentUserInfo Property
Description
Gets or sets user extended info (for user binding feature). When binding user with BindUser method and passing Info parameter (cJson object), this property is automatically set to the corresponding value. Can be used to store additional user metadata such as login time, IP address, permission level, etc.
Syntax
Public CurrentUserInfo As cJsonUsage Example
' Bind user with extended info
Dim oInfo As New cJson
oInfo.Add "loginTime", Now
oInfo.Add "ip", Client.RemoteHostIP
oInfo.Add "role", "admin"
m_oServer.BindUser "alice", Client, , oInfo
' Read user extended info
Debug.Print "Login time: " & Client.CurrentUserInfo.Item("loginTime")
Debug.Print "Role: " & Client.CurrentUserInfo.Item("role")
' Dynamically add info
Client.CurrentUserInfo.Add "lastActivity", NowDifference from UserData
| Property | Purpose | Lifecycle |
|---|---|---|
UserData | General custom data storage | Manually managed by user |
CurrentUserInfo | User-bound structured info (JSON) | Auto-managed with BindUser/UnbindUser |
CountUsers Property
Description
Gets current number of bound users (read-only). Users bound via BindUser method are counted.
Syntax
Property Get CountUsers() As LongUsage Example
' Display current bound user count
Debug.Print "Current bound users: " & m_oServer.CountUsers
' Monitor user login status
Private Sub UpdateUserCount()
lblUserCount.Caption = "Online users: " & m_oServer.CountUsers
End Sub
' Check in DataArrival
Private Sub m_oServer_DataArrival(Client As cWinsock, ByVal bytesTotal As Long)
Debug.Print "Current total bound users: " & m_oServer.CountUsers
End SubCountGroups Property
Description
Gets current number of groups (read-only). Groups created via AddGroup method are counted.
Syntax
Property Get CountGroups() As LongUsage Example
' Display current group count
Debug.Print "Current group count: " & m_oServer.CountGroups
' Create default groups on initialization
Private Sub InitializeGroups()
If m_oServer.CountGroups = 0 Then
m_oServer.AddGroup "Default"
m_oServer.AddGroup "Admins"
End If
End SubProperty Usage Scenarios Summary
Common Client Properties
' Set before connecting
m_oClient.Protocol = sckTCPProtocol
m_oClient.RemoteHost = "192.168.1.100"
m_oClient.RemotePort = 8080
m_oClient.Connect
' Get after connecting
Debug.Print "IP: " & m_oClient.RemoteHostIP
Debug.Print "Port: " & m_oClient.RemotePort
Debug.Print "State: " & m_oClient.State
' Custom tags
m_oClient.Tag = "Client-001"
m_oClient.UserData = "User info"Common Server Properties
' Start server
m_oServer.Protocol = sckTCPProtocol
m_oServer.LocalPort = 8080
m_oServer.Listen
' Manage clients
Debug.Print "Connections: " & m_oServer.ClientCount
Dim oClient As cWinsock
For Each oClient In m_oServer.Clients
Debug.Print oClient.Tag & ": " & oClient.RemoteHostIP
oClient.SendData "Broadcast message"
NextLast Updated: 2026-05-17