cWinsock Properties Reference
📋 Property List
| Property Name | Type | Read/Write | 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 bytes |
IsServer | Boolean | Read-only | Whether in server mode |
IsAcceptedClient | Boolean | Read-only | Whether a server-accepted client |
ParentServer | cWinsock | Read-only | Parent server object (clients only) |
Clients | Collection | Read-only | Collection of all connected clients (servers only) |
ClientCount | Long | Read-only | Number of client connections (servers only) |
🔄 State Property
Description
Returns the current state of the Socket.
Syntax
Property Get State() As WinsockStateReturn Values
| Constant | Value | Description |
|---|---|---|
sckClosed | 0 | Closed |
sckOpen | 1 | Open (after UDP bind) |
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 |
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 Function🌐 Protocol 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 If⚠️ Notes
- Can only be modified when
State = sckClosed - After modification, need to call
Connect(),Listen(), orBind()again
📦 RecvBuffer Property
Description
Sets or gets a custom receive buffer. Usually used in 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 = baCustomBuffer🔌 LocalPort Property
Description
Gets or sets the local port number.
Syntax
Property Get LocalPort() As Long
Property Let LocalPort(ByVal Value As Long)Usage Example
' 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
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.RemoteHost🔢 RemotePort Property
Description
Gets or sets the 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.RemotePort🖥️ RemoteHostIP Property
Description
Gets the 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 data from " & Client.RemoteHostIP & ":" & Client.RemotePort
End Sub💻 LocalHostName Property
Description
Gets the local hostname.
Syntax
Property Get LocalHostName() As StringUsage Example
Debug.Print "Local hostname: " & m_oSocket.LocalHostName🌐 LocalIP Property
Description
Gets the local IP address.
Syntax
Property Get LocalIP() As StringUsage Example
Debug.Print "Local IP: " & m_oSocket.LocalIP🏷️ Tag 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 Function💾 UserData 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: 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.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 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
Property Get SocketHandle() As LongUsage Example
' 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
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"
' 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
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 Sub🔗 IsAcceptedClient Property
Description
Determines if the current object is a client accepted by the 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 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
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 to Others
' 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
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 Sub🔢 ClientCount Property
Description
Gets the number of currently 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 Sub📌 Property Usage Scenarios Summary
Common Client Properties
' 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
' 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-01-09