Skip to content

cWinsock Properties Reference

📋 Property List

Property NameTypeRead/WriteDescription
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 bytes
IsServerBooleanRead-onlyWhether in server mode
IsAcceptedClientBooleanRead-onlyWhether a server-accepted client
ParentServercWinsockRead-onlyParent server object (clients only)
ClientsCollectionRead-onlyCollection of all connected clients (servers only)
ClientCountLongRead-onlyNumber of client connections (servers only)

🔄 State Property

Description

Returns the current state of the Socket.

Syntax

vb
Property Get State() As WinsockState

Return Values

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

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 a custom receive buffer. Usually used in 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 the 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 before calling Connect/Listen/Bind)
m_oServer.LocalPort = 8080
m_oServer.Listen

' Get actually bound port
Debug.Print "Local port: " & m_oSocket.LocalPort

⚠️ Notes

  • Can only be set when State = sckClosed
  • Range: 0-65535
  • 0 means automatically assigned by system

🌍 RemoteHost Property

Description

Gets or sets the 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 the 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 the 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 data from " & Client.RemoteHostIP & ":" & Client.RemotePort
End Sub

💻 LocalHostName Property

Description

Gets the local hostname.

Syntax

vb
Property Get LocalHostName() As String

Usage Example

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

🌐 LocalIP Property

Description

Gets the 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: Zhang San"

' Store number
m_oClient.UserData = 12345

' Store object
Dim oUserInfo As New CUserInfo
oUserInfo.Name = "Zhang San"
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 no activity
            Debug.Print "Session timeout, disconnecting: " & oClient.Tag
            oClient.Close_
        End If
    Next
End Sub

🔑 SocketHandle Property

Description

Gets the 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., interacting with Win32 API)
If m_oSocket.SocketHandle <> 0 Then
    Call SomeWin32Function(m_oSocket.SocketHandle)
End If

📊 BytesReceived Property

Description

Gets the number of bytes available in the 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"
    
    ' Only read partial data
    If Client.BytesReceived > 100 Then
        Dim sData As String
        Client.GetData sData, vbString, 100 ' Only read first 100 bytes
        Debug.Print "Read partial data: " & sData
    End If
End Sub

🏢 IsServer Property

Description

Determines if the 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 the current object is a client accepted by the 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 the 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 the 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 to Others

vb
' Broadcast to other clients through parent server in some client's event
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 the 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 the number of currently 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

📌 Property Usage Scenarios Summary

Common Client Properties

vb
' Set before connection
m_oClient.Protocol = sckTCPProtocol
m_oClient.RemoteHost = "192.168.1.100"
m_oClient.RemotePort = 8080
m_oClient.Connect

' Get after connection
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-01-09

VB6 and LOGO copyright of Microsoft Corporation