CSocket

Classe rappresentante una socket TCPIP per Visual Basic

Autore: Copyright © 2002 by Oleg Gdalevich
Data : Settembre 2004

To use this class module you need:' MSocketSupport code module
Version: 1.0.12 Modified: 17-OCT-2002
To get latest version of this code please visit the following web page:
http://www.vbip.com/winsock-api/csocket-class/csocket-class-01.asp

Classe adattata da Giorgio Bernardi
Importante: le socket vanno chiuse esplicitamente altrimenti il GarbageCollector di Visual Basic le lascia aperte indefinitamente causando spiacevoli errori ed inconvenienti!

Codice
Event Summary
Public OnClose()
These are those MS Winsock's events.
Public OnConnect()
These are those MS Winsock's events.
Public OnConnectionRequest( ByVal requestID As Long)
These are those MS Winsock's events.
Public OnDataArrival( ByVal bytesTotal As Long)
These are those MS Winsock's events.
Public OnError( ByVal Number As Integer, Description As String, ByVal l As Long, ByVal l As String, ByVal l As String, ByVal l As Long, CancelDisplay As Boolean)
These are those MS Winsock's events.
Public OnSendComplete()
These are those MS Winsock's events.
Public OnSendProgress( ByVal bytesSent As Long, ByVal l As Long)
These are those MS Winsock's events.
 
Types Summary
Public enmAddressFamily
Enum enmAddressFamily
AF_UNSPEC = 0 '/* unspecified */
AF_UNIX = 1 '/* local to host (pipes, portals) */
AF_INET = 2 '/* internetwork: UDP, TCP, etc. */
AF_IMPLINK = 3 '/* arpanet imp addresses */
AF_PUP = 4 '/* pup protocols: e.g. BSP */
AF_CHAOS = 5 '/* mit CHAOS protocols */
AF_NS = 6 '/* XEROX NS protocols */
AF_IPX = AF_NS '/* IPX protocols: IPX, SPX, etc. */
AF_ISO = 7 '/* ISO protocols */
AF_OSI = AF_ISO '/* OSI is ISO */
AF_ECMA = 8 '/* european computer manufacturers */
AF_DATAKIT = 9 '/* datakit protocols */
AF_CCITT = 10 '/* CCITT protocols, X.25 etc */
AF_SNA = 11 '/* IBM SNA */
AF_DECnet = 12 '/* DECnet */
AF_DLI = 13 '/* Direct data link interface */
AF_LAT = 14 '/* LAT */
AF_HYLINK = 15 '/* NSC Hyperchannel */
AF_APPLETALK = 16 '/* AppleTalk */
AF_NETBIOS = 17 '/* NetBios-style addresses */
AF_VOICEVIEW = 18 '/* VoiceView */
AF_FIREFOX = 19 '/* Protocols from Firefox */
AF_UNKNOWN1 = 20 '/* Somebody is using this! */
AF_BAN = 21 '/* Banyan */
AF_ATM = 22 '/* Native ATM Services */
AF_INET6 = 23 '/* Internetwork Version 6 */
AF_CLUSTER = 24 '/* Microsoft Wolfpack */
AF_12844 = 25 '/* IEEE 1284.4 WG AF */
AF_MAX = 26
End Enum

Enumerazione di tutte le famiglie di indirizzi ammesse per le Socket.
Public enmSocketOptions
Enum enmSocketOptions
SO_DEBUG = &H1& ' Turn on debugging info recording
SO_ACCEPTCONN = &H2& ' Socket has had listen() - READ-ONLY.
SO_REUSEADDR = &H4& ' Allow local address reuse.
SO_KEEPALIVE = &H8& ' Keep connections alive.
SO_DONTROUTE = &H10& ' Just use interface addresses.
SO_BROADCAST = &H20& ' Permit sending of broadcast msgs.
SO_USELOOPBACK = &H40& ' Bypass hardware when possible.
SO_LINGER = &H80& ' Linger on close if data present.
SO_OOBINLINE = &H100& ' Leave received OOB data in line.
 = 
SO_DONTLINGER = Not SO_LINGER
SO_EXCLUSIVEADDRUSE = Not SO_REUSEADDR ' Disallow local address reuse.
 = 
' = 
SO_SNDBUF = &H1001& ' Send buffer size.
SO_RCVBUF = &H1002& ' Receive buffer size.
SO_ERROR = &H1007& ' Get error status and clear.
SO_TYPE = &H1008& ' Get socket type - READ-ONLY.
End Enum

Opzioni per le socket
Public enmSocketProtocol
Enum enmSocketProtocol
IPPROTO_IP = 0 '/* dummy for IP */
IPPROTO_ICMP = 1 '/* control message protocol */
IPPROTO_IGMP = 2 '/* internet group management protocol */
IPPROTO_GGP = 3 '/* gateway^2 (deprecated) */
IPPROTO_TCP = 6 '/* tcp */
IPPROTO_PUP = 12 '/* pup */
IPPROTO_UDP = 17 '/* user datagram protocol */
IPPROTO_IDP = 22 '/* xns idp */
IPPROTO_ND = 77 '/* UNOFFICIAL net disk proto */
IPPROTO_RAW = 255 '/* raw IP packet */
IPPROTO_MAX = 256
End Enum

Protocoli ammissibili per le Socket
Public enmSocketType
Enum enmSocketType
SOCK_STREAM = 1 ' /* stream socket */
SOCK_DGRAM = 2 ' /* datagram socket */
SOCK_RAW = 3 ' /* raw-protocol interface */
SOCK_RDM = 4 ' /* reliably-delivered message */
SOCK_SEQPACKET = 5 ' /* sequenced packet stream */
End Enum

Tipi di Socket ammissibili
Public ErrorConstants
Enum ErrorConstants
sckAddressInUse = 10048
sckAddressNotAvailable = 10049
sckAlreadyComplete = 10037
sckAlreadyConnected = 10056
sckBadState = 40006
sckConnectAborted = 10053
sckConnectionRefused = 10061
sckConnectionReset = 10054
sckGetNotSupported = 394
sckHostNotFound = 11001
sckHostNotFoundTryAgain = 11002
sckInProgress = 10036
sckInvalidArg = 40014
sckInvalidArgument = 10014
sckInvalidOp = 40020
sckInvalidPropertyValue = 380
sckMsgTooBig = 10040
sckNetReset = 10052
sckNetworkSubsystemFailed = 10050
sckNetworkUnreachable = 10051
sckNoBufferSpace = 10055
sckNoData = 11004
sckNonRecoverableError = 11003
sckNotConnected = 10057
sckNotInitialized = 10093
sckNotSocket = 10038
sckOpCanceled = 10004
sckOutOfMemory = 7
sckOutOfRange = 40021
sckPortNotSupported = 10043
sckSetNotSupported = 383
sckSocketShutdown = 10058
sckSuccess = 40017
sckTimedout = 10060
sckUnsupported = 40018
sckWouldBlock = 10035
sckWrongProtocol = 40026
End Enum

Private InternalStateConstants
Enum InternalStateConstants
istConnecting = 
istSendingDatagram = 
End Enum

In order to resolve a host name the MSocketSupport.ResolveHost function can be called from the Connect and SendData methods of this class. The callback acceptor for that routine is the PostGetHostEvent procedure. This procedure determines what to do next with the received host's address checking a value of the m_varInternalState variable.
Public ProtocolConstants
Enum ProtocolConstants
sckTCPProtocol = 0
sckUDPProtocol = 1
End Enum

The CSocket protocol's constants as for the MS Winsock Control interface
Public StateConstants
Enum StateConstants
sckClosed = 0
sckOpen = 1
sckListening = 2
sckConnectionPending = 3
sckResolvingHost = 4
sckHostResolved = 5
sckConnecting = 6
sckConnected = 7
sckClosing = 8
sckError = 9
End Enum

The CSocket state's constants as for the MS Winsock Control interface
 
Field Summary
Private(enmAddressFamily) AddressFamily
Variabile contenente gli ultimi dati arrivati sulla Socket
Public(Boolean) CatchErrors
Utile per non generare eccezioni ma usare le funzioni gestendo eventuali risultati non validi
Private(Long 'value returned by the RecvDataToBuffer function) lngBytesReceived
Private(Long 'value returned by the accept Winsock API function) lngNewSocket
Private(Long 'value returned by the getsockname Winsock API function) lngRetValue
Private(Long 'value returned by the connect Winsock API function) lngRetValue
Private(Long) m_lngBytesReceived
Local (module level) variables to hold values of the properties of this (CSocket) class.
Private(Long) m_lngRequestID
Resolving host names is performed in an asynchronous mode, the m_lngRequestID variable just holds the value returned by the ResolveHost function from the MSocketSupport module.
Private(InternalStateConstants) m_varInternalState
Stato interno della connessione
Private(Boolean 'Added: 09-JULY-2002) mblnBroadcast
This flag variable indicates that the SO_BROADCAST option
Private(Boolean 'Added: 12-SEP-2002) mblnSendFlag
Private(Boolean 'Added: 10-MAR-2002) mblnSocketIsBound
This flag variable indicates that the socket is bound to some local socket address
Private(Long) mlngLastErrorCode
Private(Long) mlngMaxMsgSize
Maximum size of a datagram that can be sent through a message-oriented (UDP) socket. This value is returned by the InitWinsock function from the MSocketSupport module.
Private(Long) mlngRecvBufferLen
Lenght of the Winsock buffers. By default = 8192 bytes for TCP sockets.
Private(Long) mlngSendBufferLen
Lenght of the Winsock buffers. By default = 8192 bytes for TCP sockets.
Private(Long) mlngSocketID
Identificativo del sistema per la socket aperta. Vale 0 se è chiusa
Private(CRemoteHost) mLocalHost
Indirizzo locale della socket
Private(enmSocketProtocol) mProtocollo
Private(CRemoteHost) mRemoteHost
Indirizzo remoto della socket
Private(String) mstrLastError
Ultimi dati ricevuti ma non ancora recuperati dall'utente
Private(String) mstrRecvBuffer
byteArray() = StrConv(strBuffer, vbFromUnicode)
Private(String) mstrSendBuffer
byteArray() = StrConv(strBuffer, vbFromUnicode)
Private(ProtocolConstants) mvarProtocol
Local (module level) variables to hold values of the properties of this (CSocket) class.
Private(StateConstants) mvarState
Local (module level) variables to hold values of the properties of this (CSocket) class.
Private(enmSocketType) SocketType
Famiglia di indirizzi della socket creata
Private(SOCK_ADDR 'socket address - used by the connect Winsock API function) udtAddress
Private(SOCK_ADDR 'remote socket address for the accept Winscok API function) udtSockAddr
 
Properties Summary
Get Public(Long)
Property Get BytesReceived()

Codice
Numero di bytes ricevuti e pronti per essere recuperati
Get Public(Boolean)
Property Get IsBound()

Codice
Indica se la socket è attualmente creata
Get Public(Boolean)
Property Get IsCreated()

Codice
Indica se la socket è attualmente creata
Get Public(Boolean)
Property Get IsOpen()

Codice
Indica se la socket è attualmente aperta
Get Public(String)
Let Private(String)
Property Get LastError()
Property Let LastError( newValue As String)

Codice
Descrizione dell'ultimo errore
Get Public(Long)
Property Get LastErrorCode()

Codice
Numero dell'ultimo errore
Get Public(CRemoteHost)
Set Private(CRemoteHost)
Property Get LocalHost()
Property Set LocalHost( newValue As CRemoteHost)

Codice
Indirizzo locale della socket
Get Public(ProtocolConstants)
Let Public(ProtocolConstants)
Property Get Protocol()
Property Let Protocol( newValue As ProtocolConstants)

Codice
Tipo di protocollo utilizzato in breve
Get Public(CRemoteHost)
Set Private(CRemoteHost)
Property Get RemoteHost()
Property Set RemoteHost( newValue As CRemoteHost)

Codice
Indirizzo remoto della socket
Get Public(Long)
Property Get SocketHandle()

Codice
Get Public(Long)
Let Private(Long)
Property Get SocketID()
Property Let SocketID( newValue As Long)

Codice
Contiene l'identificativo univoco attribuito dal sistema alla Socket creata
Get Public(StateConstants)
Property Get State()

Codice
Permette di spedire dati attraverso la Socket.
 
Method Summary
Public(Boolean) Accept( requestID As Long)
Codice
Procedura duplicata in OpenFromRequestID per compatibilita con MsWinSock control
Public(CSocket) AcceptNextConnection( requestID As Long)
Codice
Metodo da chiamare per le socket di tipo Server in stato Listening.
Public(Boolean) Bind( Optional LocalHostDescription As CRemoteHost, Optional l As Boolean)
Codice
Tenta di Collegare la socket creata ad un numero di porta nell'Host locale.
Private Class_Initialize()
Codice
Private Class_Terminate()
Codice
Public CloseSocket()
Codice
Permette di chiudere la socket
Public(Boolean) Connect( Optional RemoteHostDescription As CRemoteHost)
Codice
Consente di collegare la socket creata all'host remoto.
Public(Boolean) Create( Optional ByVal l As enmAddressFamily, Optional l As enmSocketType)
Codice
Tenta di creare l'oggetto Socket a basso livello.
Private DestroySocket()
Codice
Private GenerateError( Description As String, Optional l As Long)
Codice
Gestisce l'errore
Private GenerateErrorGenerico( Procedure As String)
Codice
Gestisce l'errore
Public(Boolean) GetData( varData As Variant, Optional l As Variant, Optional l As Variant)
Codice
Recupera i dati dal buffer eliminandoli
Private(Variant) GetLastErrorCode( Optional ByVal l As String)
Codice
Private GetWinsockBuffers()
Codice
Public(Boolean) IsDataAvailable()
Codice
Indica se ci sono attualmente dati utili in ingresso
Public(Boolean) Listen()
Codice
Consente di mettere in ascolto la socket creata.
Private mRemoteHost_OnChangeIPAddress()
Codice
Public(Boolean) OpenClientTCPConnection( Optional ResetLocalPort As Boolean)
Codice
Public(Boolean) OpenClientUDPConnection()
Codice
Consente di creare una Socket lato Client di tipo DataGram UDP
Public(Boolean) OpenFromReqID( requestID As Long)
Codice
Consente di creare una Socket server
Public(Boolean) OpenServerConnection()
Codice
Public PeekData( varData As Variant, Optional l As Variant, Optional l As Variant)
Codice
Permette di verificare se nel buffer di ricezione sono presenti dati.
Private(Long) RecvData( varData As Variant, blnPeek As Boolean, Optional l As Variant, Optional l As Variant)
Codice
Private(Long) RecvDataToBuffer()
Codice
Private(Boolean) SendBufferedData()
Codice
Public(Boolean) SendData( varData As Variant)
Codice
 
Details

OnClose

Public Event OnClose()

Codice
These are those MS Winsock's events.
Pay attention that the "On" prefix is added.

OnConnect

Public Event OnConnect()

Codice
These are those MS Winsock's events.
Pay attention that the "On" prefix is added.

OnConnectionRequest

Public Event OnConnectionRequest(ByVal requestID As Long)

Codice
These are those MS Winsock's events.
Pay attention that the "On" prefix is added.

OnDataArrival

Public Event OnDataArrival(ByVal bytesTotal As Long)

Codice
These are those MS Winsock's events.
Pay attention that the "On" prefix is added.

OnError

Public Event OnError(ByVal Number As Integer, Description As String, ByVal Scode As Long, ByVal Source As String, ByVal HelpFile As String, ByVal HelpContext As Long, CancelDisplay As Boolean)

Codice
These are those MS Winsock's events.
Pay attention that the "On" prefix is added.

OnSendComplete

Public Event OnSendComplete()

Codice
These are those MS Winsock's events.
Pay attention that the "On" prefix is added.

OnSendProgress

Public Event OnSendProgress(ByVal bytesSent As Long, ByVal bytesRemaining As Long)

Codice
These are those MS Winsock's events.
Pay attention that the "On" prefix is added.

enmAddressFamily

Public Enum enmAddressFamily
    AF_UNSPEC = 0      '/* unspecified */
    AF_UNIX = 1        '/* local to host (pipes, portals) */
    AF_INET = 2        '/* internetwork: UDP, TCP, etc. */
    AF_IMPLINK = 3     '/* arpanet imp addresses */
    AF_PUP = 4         '/* pup protocols: e.g. BSP */
    AF_CHAOS = 5       '/* mit CHAOS protocols */
    AF_NS = 6          '/* XEROX NS protocols */
    AF_IPX = AF_NS     '/* IPX protocols: IPX, SPX, etc. */
    AF_ISO = 7         '/* ISO protocols */
    AF_OSI = AF_ISO    '/* OSI is ISO */
    AF_ECMA = 8        '/* european computer manufacturers */
    AF_DATAKIT = 9     '/* datakit protocols */
    AF_CCITT = 10      '/* CCITT protocols, X.25 etc */
    AF_SNA = 11        '/* IBM SNA */
    AF_DECnet = 12     '/* DECnet */
    AF_DLI = 13        '/* Direct data link interface */
    AF_LAT = 14        '/* LAT */
    AF_HYLINK = 15     '/* NSC Hyperchannel */
    AF_APPLETALK = 16  '/* AppleTalk */
    AF_NETBIOS = 17    '/* NetBios-style addresses */
    AF_VOICEVIEW = 18  '/* VoiceView */
    AF_FIREFOX = 19    '/* Protocols from Firefox */
    AF_UNKNOWN1 = 20   '/* Somebody is using this! */
    AF_BAN = 21        '/* Banyan */
    AF_ATM = 22        '/* Native ATM Services */
    AF_INET6 = 23      '/* Internetwork Version 6 */
    AF_CLUSTER = 24    '/* Microsoft Wolfpack */
    AF_12844 = 25      '/* IEEE 1284.4 WG AF */
    AF_MAX = 26
End Enum

Codice
Enumerazione di tutte le famiglie di indirizzi ammesse per le Socket.
Although AF_UNSPEC is defined for backwards compatibility, using AF_UNSPEC for the "af" parameter when creating a socket is STRONGLY DISCOURAGED. The interpretation of the "protocol" parameter depends on the actual address family chosen. As environments grow to include more and more address families that use overlapping protocol values there is more and more chance of choosing an undesired address family when AF_UNSPEC is used.

enmSocketOptions

Public Enum enmSocketOptions
    SO_DEBUG = &H1&         ' Turn on debugging info recording
    SO_ACCEPTCONN = &H2&    ' Socket has had listen() - READ-ONLY.
    SO_REUSEADDR = &H4&     ' Allow local address reuse.
    SO_KEEPALIVE = &H8&     ' Keep connections alive.
    SO_DONTROUTE = &H10&    ' Just use interface addresses.
    SO_BROADCAST = &H20&    ' Permit sending of broadcast msgs.
    SO_USELOOPBACK = &H40&  ' Bypass hardware when possible.
    SO_LINGER = &H80&       ' Linger on close if data present.
    SO_OOBINLINE = &H100&   ' Leave received OOB data in line.
    
    SO_DONTLINGER = Not SO_LINGER
    SO_EXCLUSIVEADDRUSE = Not SO_REUSEADDR ' Disallow local address reuse.
    
    ' Additional options.
    SO_SNDBUF = &H1001&     ' Send buffer size.
    SO_RCVBUF = &H1002&     ' Receive buffer size.
    SO_ERROR = &H1007&      ' Get error status and clear.
    SO_TYPE = &H1008&       ' Get socket type - READ-ONLY.
End Enum

Codice
Opzioni per le socket

enmSocketProtocol

Public Enum enmSocketProtocol
    IPPROTO_IP = 0             '/* dummy for IP */
    IPPROTO_ICMP = 1           '/* control message protocol */
    IPPROTO_IGMP = 2           '/* internet group management protocol */
    IPPROTO_GGP = 3            '/* gateway^2 (deprecated) */
    IPPROTO_TCP = 6            '/* tcp */
    IPPROTO_PUP = 12           '/* pup */
    IPPROTO_UDP = 17           '/* user datagram protocol */
    IPPROTO_IDP = 22           '/* xns idp */
    IPPROTO_ND = 77            '/* UNOFFICIAL net disk proto */
    IPPROTO_RAW = 255          '/* raw IP packet */
    IPPROTO_MAX = 256
End Enum

Codice
Protocoli ammissibili per le Socket

enmSocketType

Public Enum enmSocketType
    SOCK_STREAM = 1    ' /* stream socket */
    SOCK_DGRAM = 2     ' /* datagram socket */
    SOCK_RAW = 3       ' /* raw-protocol interface */
    SOCK_RDM = 4       ' /* reliably-delivered message */
    SOCK_SEQPACKET = 5 ' /* sequenced packet stream */
End Enum

Codice
Tipi di Socket ammissibili

ErrorConstants

Public Enum ErrorConstants
    sckAddressInUse = 10048
    sckAddressNotAvailable = 10049
    sckAlreadyComplete = 10037
    sckAlreadyConnected = 10056
    sckBadState = 40006
    sckConnectAborted = 10053
    sckConnectionRefused = 10061
    sckConnectionReset = 10054
    sckGetNotSupported = 394
    sckHostNotFound = 11001
    sckHostNotFoundTryAgain = 11002
    sckInProgress = 10036
    sckInvalidArg = 40014
    sckInvalidArgument = 10014
    sckInvalidOp = 40020
    sckInvalidPropertyValue = 380
    sckMsgTooBig = 10040
    sckNetReset = 10052
    sckNetworkSubsystemFailed = 10050
    sckNetworkUnreachable = 10051
    sckNoBufferSpace = 10055
    sckNoData = 11004
    sckNonRecoverableError = 11003
    sckNotConnected = 10057
    sckNotInitialized = 10093
    sckNotSocket = 10038
    sckOpCanceled = 10004
    sckOutOfMemory = 7
    sckOutOfRange = 40021
    sckPortNotSupported = 10043
    sckSetNotSupported = 383
    sckSocketShutdown = 10058
    sckSuccess = 40017
    sckTimedout = 10060
    sckUnsupported = 40018
    sckWouldBlock = 10035
    sckWrongProtocol = 40026
End Enum

Codice

InternalStateConstants

Private Enum InternalStateConstants
    istConnecting
    istSendingDatagram
End Enum

Codice
In order to resolve a host name the MSocketSupport.ResolveHost function can be called from the Connect and SendData methods of this class. The callback acceptor for that routine is the PostGetHostEvent procedure. This procedure determines what to do next with the received host's address checking a value of the m_varInternalState variable.

ProtocolConstants

Public Enum ProtocolConstants
    sckTCPProtocol = 0
    sckUDPProtocol = 1
End Enum

Codice
The CSocket protocol's constants as for the MS Winsock Control interface

StateConstants

Public Enum StateConstants
    sckClosed = 0
    sckOpen = 1
    sckListening = 2
    sckConnectionPending = 3
    sckResolvingHost = 4
    sckHostResolved = 5
    sckConnecting = 6
    sckConnected = 7
    sckClosing = 8
    sckError = 9
End Enum

Codice
The CSocket state's constants as for the MS Winsock Control interface

AddressFamily

Private AddressFamily           As enmAddressFamily

Codice
Variabile contenente gli ultimi dati arrivati sulla Socket
Famiglia di indirizzi della socket creata

CatchErrors

Public CatchErrors              As Boolean

Codice
Utile per non generare eccezioni ma usare le funzioni gestendo eventuali risultati non validi

lngBytesReceived

    Dim lngBytesReceived    As Long         'value returned by the RecvDataToBuffer function

Codice

lngNewSocket

    Dim lngNewSocket        As Long         'value returned by the accept Winsock API function

Codice

lngRetValue

    Dim lngRetValue         As Long         'value returned by the getsockname Winsock API function

Codice

lngRetValue

    Dim lngRetValue     As Long         'value returned by the connect Winsock API function

Codice

m_lngBytesReceived

Private m_lngBytesReceived  As Long

Codice
Local (module level) variables to hold values of the properties of this (CSocket) class.

m_lngRequestID

Private m_lngRequestID      As Long

Codice
Resolving host names is performed in an asynchronous mode, the m_lngRequestID variable just holds the value returned by the ResolveHost function from the MSocketSupport module.

m_varInternalState

Private m_varInternalState As InternalStateConstants

Codice
Stato interno della connessione

mblnBroadcast

Private mblnBroadcast      As Boolean  'Added: 09-JULY-2002

Codice
This flag variable indicates that the SO_BROADCAST option
is set on the socket

mblnSendFlag

Private mblnSendFlag As Boolean        'Added: 12-SEP-2002

Codice

mblnSocketIsBound

Private mblnSocketIsBound  As Boolean  'Added: 10-MAR-2002

Codice
This flag variable indicates that the socket is bound to some local socket address

mlngLastErrorCode

Private mlngLastErrorCode       As Long

Codice

mlngMaxMsgSize

Private mlngMaxMsgSize     As Long

Codice
Maximum size of a datagram that can be sent through a message-oriented (UDP) socket. This value is returned by the InitWinsock function from the MSocketSupport module.

mlngRecvBufferLen

Private mlngRecvBufferLen  As Long

Codice
Lenght of the Winsock buffers. By default = 8192 bytes for TCP sockets.
These values are initialized in the SocketExists function.
Now, I really don't know why I was in need to get these values.

mlngSendBufferLen

Private mlngSendBufferLen  As Long

Codice
Lenght of the Winsock buffers. By default = 8192 bytes for TCP sockets.
These values are initialized in the SocketExists function.
Now, I really don't know why I was in need to get these values.

mlngSocketID

Private mlngSocketID            As Long

Codice
Identificativo del sistema per la socket aperta. Vale 0 se è chiusa

mLocalHost

Private WithEvents mLocalHost       As CRemoteHost

Codice
Indirizzo locale della socket

mProtocollo

Private mProtocollo             As enmSocketProtocol

Codice

mRemoteHost

Private WithEvents mRemoteHost      As CRemoteHost

Codice
Indirizzo remoto della socket

mstrLastError

Private mstrLastError           As String

Codice
Ultimi dati ricevuti ma non ancora recuperati dall'utente
Lunghezza ultimi dati ricevuti ma non ancora recuperati dall'utente

mstrRecvBuffer

Private mstrRecvBuffer     As String

Codice
byteArray() = StrConv(strBuffer, vbFromUnicode)

The internal buffer for outgoing data

mstrSendBuffer

Private mstrSendBuffer     As String

Codice
byteArray() = StrConv(strBuffer, vbFromUnicode)

The internal buffer for outgoing data

mvarProtocol

Private mvarProtocol        As ProtocolConstants

Codice
Local (module level) variables to hold values of the properties of this (CSocket) class.

mvarState

Private mvarState           As StateConstants

Codice
Local (module level) variables to hold values of the properties of this (CSocket) class.

SocketType

Private SocketType              As enmSocketType

Codice
Famiglia di indirizzi della socket creata

udtAddress

    Dim udtAddress      As SOCK_ADDR  'socket address - used by the connect Winsock API function

Codice

udtSockAddr

    Dim udtSockAddr         As SOCK_ADDR  'remote socket address for the accept Winscok API function

Codice

BytesReceived

Public Property Get BytesReceived() As Long

Codice
Numero di bytes ricevuti e pronti per essere recuperati

IsBound

Public Property Get IsBound() As Boolean

Codice
Indica se la socket è attualmente creata

IsCreated

Public Property Get IsCreated() As Boolean

Codice
Indica se la socket è attualmente creata

IsOpen

Public Property Get IsOpen() As Boolean

Codice
Indica se la socket è attualmente aperta

LastError

Public Property Get LastError() As String

Codice
Descrizione dell'ultimo errore

LastError

Private Property Let LastError(newValue As String)

Codice
Descrizione dell'ultimo errore

LastErrorCode

Public Property Get LastErrorCode() As Long

Codice
Numero dell'ultimo errore

LocalHost

Public Property Get LocalHost() As CRemoteHost

Codice
Indirizzo locale della socket

LocalHost

Private Property Set LocalHost(newValue As CRemoteHost)

Codice
Indirizzo locale della socket

Protocol

Public Property Get Protocol() As ProtocolConstants

Codice
Tipo di protocollo utilizzato in breve

Protocol

Public Property Let Protocol(newValue As ProtocolConstants)

Codice
Tipo di protocollo utilizzato in breve

RemoteHost

Public Property Get RemoteHost() As CRemoteHost

Codice
Indirizzo remoto della socket

RemoteHost

Private Property Set RemoteHost(newValue As CRemoteHost)

Codice
Indirizzo remoto della socket

SocketHandle

Public Property Get SocketHandle() As Long

Codice

SocketID

Public Property Get SocketID() As Long

Codice
Contiene l'identificativo univoco attribuito dal sistema alla Socket creata

SocketID

Private Property Let SocketID(newValue As Long)

Codice
Contiene l'identificativo univoco attribuito dal sistema alla Socket creata

State

Public Property Get State() As StateConstants

Codice
Permette di spedire dati attraverso la Socket.
Il metodo è sincrono se tutti i dati vengono inviati in un solo invio, altrimenti l'oggetto gestisce l'invio multiplo

Accept

Public Function Accept(requestID As Long) As Boolean

Codice
Procedura duplicata in OpenFromRequestID per compatibilita con MsWinSock control

AcceptNextConnection

Public Function AcceptNextConnection(requestID As Long) As CSocket

Codice
Metodo da chiamare per le socket di tipo Server in stato Listening.

Bind

Public Function Bind(Optional LocalHostDescription As CRemoteHost = Nothing, Optional ReusePort As Boolean = True) As Boolean

Codice
Tenta di Collegare la socket creata ad un numero di porta nell'Host locale.
Se non vengono passati i parametri necessari (Porta e LocalIP) vengono usati quelli precedentemente settati.
Per creare una connessione locale senza specificare una porta, lasciare 0 come porta locale.

Class_Initialize

Private Sub Class_Initialize()

Codice

Class_Terminate

Private Sub Class_Terminate()

Codice

CloseSocket

Public Sub CloseSocket()

Codice
Permette di chiudere la socket

Connect

Public Function Connect(Optional RemoteHostDescription As CRemoteHost = Nothing) As Boolean

Codice
Consente di collegare la socket creata all'host remoto.
La socket deve essere già stata collegata all'host locale attraverso la Bind()
Se non viene specificato l'host remoto verranno usate le informazioni già settate.

Create

Public Function Create(Optional ByVal af As enmAddressFamily = AF_INET, Optional ByVal st As enmSocketType = SOCK_STREAM) As Boolean

Codice
Tenta di creare l'oggetto Socket a basso livello.
Viene creata una socket di tipo dediderato.

DestroySocket

Private Sub DestroySocket()

Codice

GenerateError

Private Sub GenerateError(Description As String, Optional Number As Long = vbObjectError)

Codice
Gestisce l'errore

GenerateErrorGenerico

Private Sub GenerateErrorGenerico(Procedure As String)

Codice
Gestisce l'errore

GetData

Public Function GetData(varData As Variant, Optional VarType As Variant, Optional maxLen As Variant) As Boolean

Codice
Recupera i dati dal buffer eliminandoli

GetLastErrorCode

Private Function GetLastErrorCode(Optional ByVal strAdditionalInfo As String)

Codice

GetWinsockBuffers

Private Sub GetWinsockBuffers()

Codice

IsDataAvailable

Public Function IsDataAvailable() As Boolean

Codice
Indica se ci sono attualmente dati utili in ingresso

Listen

Public Function Listen() As Boolean

Codice
Consente di mettere in ascolto la socket creata.
La socket deve essere già stata collegata all'host locale attraverso la Bind()

mRemoteHost_OnChangeIPAddress

Private Sub mRemoteHost_OnChangeIPAddress()

Codice

OpenClientTCPConnection

Public Function OpenClientTCPConnection(Optional ResetLocalPort As Boolean = True) As Boolean

Codice

OpenClientUDPConnection

Public Function OpenClientUDPConnection() As Boolean

Codice
Consente di creare una Socket lato Client di tipo DataGram UDP
Vengono in pratica effettuate le seguenti operazioni:
  1. Create()
  2. Bind()


OpenFromReqID

Public Function OpenFromReqID(requestID As Long) As Boolean

Codice
Consente di creare una Socket server
Vengono in pratica effettuate le seguenti operazioni:
  1. Create()
  2. Bind()
  3. Connect()


OpenServerConnection

Public Function OpenServerConnection() As Boolean

Codice

PeekData

Public Sub PeekData(varData As Variant, Optional VarType As Variant, Optional maxLen As Variant)

Codice
Permette di verificare se nel buffer di ricezione sono presenti dati.

RecvData

Private Function RecvData(varData As Variant, blnPeek As Boolean, Optional VarType As Variant, Optional maxLen As Variant) As Long

Codice

RecvDataToBuffer

Private Function RecvDataToBuffer() As Long

Codice

SendBufferedData

Private Function SendBufferedData() As Boolean

Codice

SendData

Public Function SendData(varData As Variant) As Boolean

Codice