Skip to content

cWinsock Properties Reference

Properties List

Property NameTypeAccessDescription
StateWinsockStateRead-onlyCurrent Socket state
ProtocolWinsockProtocolRead/WriteProtocol type (TCP/UDP)
RecvBufferByte()Read/WriteCustom receive buffer
LocalPortLongRead/WriteLocal port
RemoteHostStringRead/WriteRemote hostname
RemotePortLongRead/WriteRemote port
RemoteHostIPStringRead-onlyResolved remote IP address
LocalHostNameStringRead-onlyLocal hostname
LocalIPStringRead-onlyLocal IP address
TagStringRead/WriteUser-defined tag
UserDataVariantRead/WriteUser-defined data
SocketHandleLongRead-onlySocket handle
BytesReceivedLongRead-onlyAvailable data byte count
IsServerBooleanRead-onlyWhether in server mode
IsAcceptedClientBooleanRead-onlyWhether accepted by server
ParentServercWinsockRead-onlyParent server object (client only)
ClientsCollectionRead-onlyCollection of all connected clients (server only)
ClientCountLongRead-onlyClient connection count (server only)
CurrentUserVariantRead/WriteBound username (user binding feature)
CurrentUserTokenStringRead/WriteUser authentication token (user binding feature)
CurrentUserInfocJsonRead/WriteUser extended info (user binding feature)
CountUsersLongRead-onlyCurrent bound user count (user binding feature)
CountGroupsLongRead-onlyCurrent group count (user binding feature)

State Property

Description

Returns the current Socket state.

Syntax

vb
Property Get State() As WinsockState

Return Values

ConstantValueDescription
sckClosed0Closed
sckOpen1Open (after UDP binding)
sckListening2Listening (TCP server)
sckConnectionPending3Connection pending
sckResolvingHost4Resolving hostname
sckHostResolved5Hostname resolved
sckConnecting6Connecting
sckConnected7Connected
sckClosing8Closing
sckError9Error occurred

Usage Example

vb
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 Function

Protocol Property

Description

Gets or sets the protocol type used by the Socket.

Syntax

vb
Property Get Protocol() As WinsockProtocol
Property Let Protocol(ByVal Value As WinsockProtocol)

Values

ConstantValueDescription
sckTCPProtocol1TCP protocol (reliable, connection-oriented)
sckUDPProtocol2UDP protocol (unreliable, connectionless)

Usage Example

vb
' 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 If

Notes

  • Can only be modified when State = sckClosed
  • After modification, need to call Connect(), Listen() or Bind() again

RecvBuffer Property

Description

Sets or gets custom receive buffer. Usually used for advanced scenarios.

Syntax

vb
Property Let RecvBuffer(ByRef Value() As Byte)

Usage Example

vb
' Set custom buffer
Dim baCustomBuffer() As Byte
ReDim baCustomBuffer(0 To 8191) ' 8KB buffer
m_oSocket.RecvBuffer = baCustomBuffer

LocalPort Property

Description

Gets or sets local port number.

Syntax

vb
Property Get LocalPort() As Long
Property Let LocalPort(ByVal Value As Long)

Usage Example

vb
' 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.LocalPort

Notes

  • 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

vb
Property Get RemoteHost() As String
Property Let RemoteHost(ByVal Value As String)

Usage Example

vb
' 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.RemoteHost

RemotePort Property

Description

Gets or sets remote port number.

Syntax

vb
Property Get RemotePort() As Long
Property Let RemotePort(ByVal Value As Long)

Usage Example

vb
' Set remote port
m_oClient.RemotePort = 8080

' Get remote port
Debug.Print "Remote port: " & m_oClient.RemotePort

RemoteHostIP Property

Description

Gets resolved remote IP address (read-only).

Syntax

vb
Property Get RemoteHostIP() As String

Usage Example

vb
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 Sub

Special Case: UDP Server Virtual Client

vb
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 Sub

LocalHostName Property

Description

Gets local hostname.

Syntax

vb
Property Get LocalHostName() As String

Usage Example

vb
Debug.Print "Local hostname: " & m_oSocket.LocalHostName

LocalIP Property

Description

Gets local IP address.

Syntax

vb
Property Get LocalIP() As String

Usage Example

vb
Debug.Print "Local IP: " & m_oSocket.LocalIP

Tag Property

Description

User-defined tag for identifying objects.

Syntax

vb
Property Get Tag() As String
Property Let Tag(ByVal Value As String)

Usage Example

vb
' 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 Function

UserData Property

Description

User-defined data storage, can store any type of data.

Syntax

vb
Property Get UserData() As Variant
Property Let UserData(ByVal Value As Variant)
Property Set UserData(ByVal Value As Variant)

Usage Example

vb
' 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.Age

Advanced Usage: Client Session Data

vb
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 Sub

SocketHandle Property

Description

Gets underlying Socket handle (read-only).

Syntax

vb
Property Get SocketHandle() As Long

Usage Example

vb
' 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 If

BytesReceived Property

Description

Gets available byte count in receive buffer (read-only).

Syntax

vb
Property Get BytesReceived() As Long

Usage Example

vb
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 Sub

IsServer Property

Description

Determines if current object is in server mode (read-only).

Syntax

vb
Property Get IsServer() As Boolean

Usage Example

vb
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 Sub

IsAcceptedClient Property

Description

Determines if current object is a client accepted by server (read-only).

Syntax

vb
Property Get IsAcceptedClient() As Boolean

Usage Example

vb
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 Sub

ParentServer Property

Description

Gets parent server object (only valid for server-accepted clients).

Syntax

vb
Property Get ParentServer() As cWinsock

Usage Example

vb
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 Sub

Advanced Usage: Client Broadcast Message

vb
' 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 Sub

Clients Property

Description

Gets collection of all connected clients (only valid for server objects).

Syntax

vb
Property Get Clients() As Collection

Usage Example

vb
' 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 Sub

ClientCount Property

Description

Gets current number of connected clients (read-only, only valid for server objects).

Syntax

vb
Property Get ClientCount() As Long

Usage Example

vb
' 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 Sub

CurrentUser 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

vb
Public CurrentUser As Variant

Usage Example

vb
' 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 Sub

Difference from Tag Property

PropertyPurposeSetting Method
TagIdentifies socket connection (e.g., "#001")Auto-assigned or manually set
CurrentUserIdentifies logged-in userBound 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

vb
Public CurrentUserToken As String

Usage Example

vb
' 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.CurrentUserToken

CurrentUserInfo 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

vb
Public CurrentUserInfo As cJson

Usage Example

vb
' 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", Now

Difference from UserData

PropertyPurposeLifecycle
UserDataGeneral custom data storageManually managed by user
CurrentUserInfoUser-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

vb
Property Get CountUsers() As Long

Usage Example

vb
' 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 Sub

CountGroups Property

Description

Gets current number of groups (read-only). Groups created via AddGroup method are counted.

Syntax

vb
Property Get CountGroups() As Long

Usage Example

vb
' 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 Sub

Property Usage Scenarios Summary

Common Client Properties

vb
' 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

vb
' 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"
Next

Last Updated: 2026-05-17

VB6 and LOGO copyright of Microsoft Corporation