' (1) I have never used WS_SELECT (select), therefore I must warn that I do
' not know if fd_set and timeval are properly defined.
' (2) Alot of the functions are declared with “buf as any”, when calling these
' functions you may either pass strings, byte arrays or UDT's. For 32bit I
' I recommend Byte arrays and the use of memcopy to copy the data back out
' (3) The async functions (wsaAsync*) require the use of a message hook or
' message window control to capture messages sent by the winsock stack. This
' is not to be confused with a CallBack control, The only function that uses
' callbacks is WSASetBlockingHook()
' (4) Alot of “helper” functions are provided in the file for various things
' before attempting to figure out how to call a function, look and see if
' there is already a helper function for it.
' (5) Data types (hostent etc) have kept there 16bit definitions, even under 32bit
' windows due to the problem of them not working when redfined following the
' suggested rules.
' (1) I have never used WS_SELECT (select), therefore I must warn that I do
' not know if fd_set and timeval are properly defined.
' (2) Alot of the functions are declared with “buf as any”, when calling these
' functions you may either pass strings, byte arrays or UDT's. For 32bit I
' I recommend Byte arrays and the use of memcopy to copy the data back out
' (3) The async functions (wsaAsync*) require the use of a message hook or
' message window control to capture messages sent by the winsock stack. This
' is not to be confused with a CallBack control, The only function that uses
' callbacks is WSASetBlockingHook()
' (4) Alot of “helper” functions are provided in the file for various things
' before attempting to figure out how to call a function, look and see if
' there is already a helper function for it.
' (5) Data types (hostent etc) have kept there 16bit definitions, even under 32bit
' windows due to the problem of them not working when redfined following the
' suggested rules.
Option Explicit
Public Const FD_SETSIZE = 64
Type fd_set
fd_count As Integer
fd_array(FD_SETSIZE) As Integer
End Type
Type timeval
tv_sec As Long
tv_usec As Long
End Type
Type HostEnt
h_name As Long
h_aliases As Long
h_addrtype As Integer
h_length As Integer
h_addr_list As Long
End Type
Public Const hostent_size = 16
Type servent
s_name As Long
s_aliases As Long
s_port As Integer
s_proto As Long
End Type
Public Const servent_size = 14
Type protoent
p_name As Long
p_aliases As Long
p_proto As Integer
End Type
Public Const protoent_size = 10
Public Const IPPROTO_TCP = 6
Public Const IPPROTO_UDP = 17
Public Const INADDR_NONE = &HFFFFFFFF
Public Const INADDR_ANY = &H0
Type sockaddr
sin_family As Integer
sin_port As Integer
sin_addr As Long
sin_zero As String * 8
End Type
Public Const sockaddr_size = 16
Public saZero As sockaddr
Public Const WSA_DESCRIPTIONLEN = 256
Public Const WSA_DescriptionSize = WSA_DESCRIPTIONLEN + 1
Public Const WSA_SYS_STATUS_LEN = 128
Public Const WSA_SysStatusSize = WSA_SYS_STATUS_LEN + 1
Type WSADataType
wVersion As Integer
wHighVersion As Integer
szDescription As String * WSA_DescriptionSize
szSystemStatus As String * WSA_SysStatusSize
iMaxSockets As Integer
iMaxUdpDg As Integer
lpVendorInfo As Long
End Type
Public Const INVALID_SOCKET = -1
Public Const SOCKET_ERROR = -1
Public Const SOCK_STREAM = 1
Public Const SOCK_DGRAM = 2
Public Const MAXGETHOSTSTRUCT = 1024
Public Const AF_INET = 2
Public Const PF_INET = 2
Type LingerType
l_onoff As Integer
l_linger As Integer
End Type
' Windows Sockets definitions of regular Microsoft C error constants
Global Const WSAEINTR = 10004
Global Const WSAEBADF = 10009
Global Const WSAEACCES = 10013
Global Const WSAEFAULT = 10014
Global Const WSAEINVAL = 10022
Global Const WSAEMFILE = 10024
' Windows Sockets definitions of regular Berkeley error constants
Global Const WSAEWOULDBLOCK = 10035
Global Const WSAEINPROGRESS = 10036
Global Const WSAEALREADY = 10037
Global Const WSAENOTSOCK = 10038
Global Const WSAEDESTADDRREQ = 10039
Global Const WSAEMSGSIZE = 10040
Global Const WSAEPROTOTYPE = 10041
Global Const WSAENOPROTOOPT = 10042
Global Const WSAEPROTONOSUPPORT = 10043
Global Const WSAESOCKTNOSUPPORT = 10044
Global Const WSAEOPNOTSUPP = 10045
Global Const WSAEPFNOSUPPORT = 10046
Global Const WSAEAFNOSUPPORT = 10047
Global Const WSAEADDRINUSE = 10048
Global Const WSAEADDRNOTAVAIL = 10049
Global Const WSAENETDOWN = 10050
Global Const WSAENETUNREACH = 10051
Global Const WSAENETRESET = 10052
Global Const WSAECONNABORTED = 10053
Global Const WSAECONNRESET = 10054
Global Const WSAENOBUFS = 10055
Global Const WSAEISCONN = 10056
Global Const WSAENOTCONN = 10057
Global Const WSAESHUTDOWN = 10058
Global Const WSAETOOMANYREFS = 10059
Global Const WSAETIMEDOUT = 10060
Global Const WSAECONNREFUSED = 10061
Global Const WSAELOOP = 10062
Global Const WSAENAMETOOLONG = 10063
Global Const WSAEHOSTDOWN = 10064
Global Const WSAEHOSTUNREACH = 10065
Global Const WSAENOTEMPTY = 10066
Global Const WSAEPROCLIM = 10067
Global Const WSAEUSERS = 10068
Global Const WSAEDQUOT = 10069
Global Const WSAESTALE = 10070
Global Const WSAEREMOTE = 10071
' Extended Windows Sockets error constant definitions
Global Const WSASYSNOTREADY = 10091
Global Const WSAVERNOTSUPPORTED = 10092
Global Const WSANOTINITIALISED = 10093
Global Const WSAHOST_NOT_FOUND = 11001
Global Const WSATRY_AGAIN = 11002
Global Const WSANO_RECOVERY = 11003
Global Const WSANO_DATA = 11004
Global Const WSANO_ADDRESS = 11004
'—ioctl Constants
Public Const FIONREAD = &H8004667F
Public Const FIONBIO = &H8004667E
Public Const FIOASYNC = &H8004667D
#If Win16 Then
'—Windows System functions
Public Declare Function PostMessage Lib “User” (ByVal hWnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, lParam As Any) As Integer
Public Declare Sub MemCopy Lib “Kernel” Alias “hmemcpy” (Dest As Any, Src As Any, ByVal cb&)
Public Declare Function lstrlen Lib “Kernel” (ByVal lpString As Any) As Integer
'—async notification constants
Public Const SOL_SOCKET = &HFFFF
Public Const SO_LINGER = &H80
Public Const FD_READ = &H1
Public Const FD_WRITE = &H2
Public Const FD_OOB = &H4
Public Const FD_ACCEPT = &H8
Public Const FD_CONNECT = &H10
Public Const FD_CLOSE = &H20
'—SOCKET FUNCTIONS
Public Declare Function accept Lib “Winsock.dll” (ByVal s As Integer, addr As sockaddr, addrlen As Integer) As Integer
Public Declare Function bind Lib “Winsock.dll” (ByVal s As Integer, addr As sockaddr, ByVal namelen As Integer) As Integer
Public Declare Function closesocket Lib “Winsock.dll” (ByVal s As Integer) As Integer
Public Declare Function connect Lib “Winsock.dll” (ByVal s As Integer, addr As sockaddr, ByVal namelen As Integer) As Integer
Public Declare Function ioctlsocket Lib “Winsock.dll” (ByVal s As Integer, ByVal cmd As Long, argp As Long) As Integer
Public Declare Function getpeername Lib “Winsock.dll” (ByVal s As Integer, sName As sockaddr, namelen As Integer) As Integer
Public Declare Function getsockname Lib “Winsock.dll” (ByVal s As Integer, sName As sockaddr, namelen As Integer) As Integer
Public Declare Function getsockopt Lib “Winsock.dll” (ByVal s As Integer, ByVal level As Integer, ByVal optname As Integer, optval As Any, optlen As Integer) As Integer
Public Declare Function htonl Lib “Winsock.dll” (ByVal hostlong As Long) As Long
Public Declare Function htons Lib “Winsock.dll” (ByVal hostshort As Integer) As Integer
Public Declare Function inet_addr Lib “Winsock.dll” (ByVal cp As String) As Long
Public Declare Function inet_ntoa Lib “Winsock.dll” (ByVal inn As Long) As Long
Public Declare Function listen Lib “Winsock.dll” (ByVal s As Integer, ByVal backlog As Integer) As Integer
Public Declare Function ntohl Lib “Winsock.dll” (ByVal netlong As Long) As Long
Public Declare Function ntohs Lib “Winsock.dll” (ByVal netshort As Integer) As Integer
Public Declare Function recv Lib “Winsock.dll” (ByVal s As Integer, ByVal buf As Any, ByVal buflen As Integer, ByVal flags As Integer) As Integer
Public Declare Function recvfrom Lib “Winsock.dll” (ByVal s As Integer, buf As Any, ByVal buflen As Integer, ByVal flags As Integer, from As sockaddr, fromlen As Integer) As Integer
Public Declare Function ws_select Lib “Winsock.dll” Alias “select” (ByVal nfds As Integer, readfds As Any, writefds As Any, exceptfds As Any, timeout As timeval) As Integer
Public Declare Function send Lib “Winsock.dll” (ByVal s As Integer, buf As Any, ByVal buflen As Integer, ByVal flags As Integer) As Integer
Public Declare Function sendto Lib “Winsock.dll” (ByVal s As Integer, buf As Any, ByVal buflen As Integer, ByVal flags As Integer, to_addr As sockaddr, ByVal tolen As Integer) As Integer
Public Declare Function setsockopt Lib “Winsock.dll” (ByVal s As Integer, ByVal level As Integer, ByVal optname As Integer, optval As Any, ByVal optlen As Integer) As Integer
Public Declare Function ShutDown Lib “Winsock.dll” Alias “shutdown” (ByVal s As Integer, ByVal how As Integer) As Integer
Public Declare Function socket Lib “Winsock.dll” (ByVal af As Integer, ByVal s_type As Integer, ByVal protocol As Integer) As Integer
'—DATABASE FUNCTIONS
Public Declare Function gethostbyaddr Lib “Winsock.dll” (addr As Long, ByVal addr_len As Integer, ByVal addr_type As Integer) As Long
Public Declare Function gethostbyname Lib “Winsock.dll” (ByVal host_name As String) As Long
Public Declare Function gethostname Lib “Winsock.dll” (ByVal host_name As String, ByVal namelen As Integer) As Integer
Public Declare Function getservbyport Lib “Winsock.dll” (ByVal Port As Integer, ByVal proto As String) As Long
Public Declare Function getservbyname Lib “Winsock.dll” (ByVal serv_name As String, ByVal proto As String) As Long
Public Declare Function getprotobynumber Lib “Winsock.dll” (ByVal proto As Integer) As Long
Public Declare Function getprotobyname Lib “Winsock.dll” (ByVal proto_name As String) As Long
'—WINDOWS EXTENSIONS
Public Declare Function WSAStartup Lib “Winsock.dll” (ByVal wVR As Integer, lpWSAD As WSADataType) As Integer
Public Declare Function WSACleanup Lib “Winsock.dll” () As Integer
Public Declare Sub WSASetLastError Lib “Winsock.dll” (ByVal iError As Integer)
Public Declare Function WSAGetLastError Lib “Winsock.dll” () As Integer
Public Declare Function WSAIsBlocking Lib “Winsock.dll” () As Integer
Public Declare Function WSAUnhookBlockingHook Lib “Winsock.dll” () As Integer
Public Declare Function WSASetBlockingHook Lib “Winsock.dll” (ByVal lpBlockFunc As Long) As Long
Public Declare Function WSACancelBlockingCall Lib “Winsock.dll” () As Integer
Public Declare Function WSAAsyncGetServByName Lib “Winsock.dll” (ByVal hWnd As Integer, ByVal wMsg As Integer, ByVal serv_name As String, ByVal proto As String, buf As Any, ByVal buflen As Integer) As Integer
Public Declare Function WSAAsyncGetServByPort Lib “Winsock.dll” (ByVal hWnd As Integer, ByVal wMsg As Integer, ByVal Port As Integer, ByVal proto As String, buf As Any, ByVal buflen As Integer) As Integer
Public Declare Function WSAAsyncGetProtoByName Lib “Winsock.dll” (ByVal hWnd As Integer, ByVal wMsg As Integer, ByVal proto_name As String, buf As Any, ByVal buflen As Integer) As Integer
Public Declare Function WSAAsyncGetProtoByNumber Lib “Winsock.dll” (ByVal hWnd As Integer, ByVal wMsg As Integer, ByVal number As Integer, buf As Any, ByVal buflen As Integer) As Integer
Public Declare Function WSAAsyncGetHostByName Lib “Winsock.dll” (ByVal hWnd As Integer, ByVal wMsg As Integer, ByVal host_name As String, buf As Any, ByVal buflen As Integer) As Integer
Public Declare Function WSAAsyncGetHostByAddr Lib “Winsock.dll” (ByVal hWnd As Integer, ByVal wMsg As Integer, addr As Long, ByVal addr_len As Integer, ByVal addr_type As Integer, buf As Any, ByVal buflen As Integer) As Integer
Public Declare Function WSACancelAsyncRequest Lib “Winsock.dll” (ByVal hAsyncTaskHandle As Integer) As Integer
Public Declare Function WSAAsyncSelect Lib “Winsock.dll” (ByVal s As Integer, ByVal hWnd As Integer, ByVal wMsg As Integer, ByVal lEvent As Long) As Integer
Public Declare Function WSARecvEx Lib “Winsock.dll” (ByVal s As Integer, buf As Any, ByVal buflen As Integer, ByVal flags As Integer) As Integer
#ElseIf Win32 Then
'—Windows System Functions
Public Declare Function PostMessage Lib “user32” Alias “PostMessageA” (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Public Declare Sub MemCopy Lib “kernel32” Alias “RtlMoveMemory” (Dest As Any, Src As Any, ByVal cb&)
Public Declare Function lstrlen Lib “kernel32” Alias “lstrlenA” (ByVal lpString As Any) As Long
'—async notification constants
Public Const SOL_SOCKET = &HFFFF&
Public Const SO_LINGER = &H80&
Public Const FD_READ = &H1&
Public Const FD_WRITE = &H2&
Public Const FD_OOB = &H4&
Public Const FD_ACCEPT = &H8&
Public Const FD_CONNECT = &H10&
Public Const FD_CLOSE = &H20&
'—SOCKET FUNCTIONS
Public Declare Function accept Lib “wsock32.dll” (ByVal s As Long, addr As sockaddr, addrlen As Long) As Long
Public Declare Function bind Lib “wsock32.dll” (ByVal s As Long, addr As sockaddr, ByVal namelen As Long) As Long
Public Declare Function closesocket Lib “wsock32.dll” (ByVal s As Long) As Long
Public Declare Function connect Lib “wsock32.dll” (ByVal s As Long, addr As sockaddr, ByVal namelen As Long) As Long
Public Declare Function ioctlsocket Lib “wsock32.dll” (ByVal s As Long, ByVal cmd As Long, argp As Long) As Long
Public Declare Function getpeername Lib “wsock32.dll” (ByVal s As Long, sName As sockaddr, namelen As Long) As Long
Public Declare Function getsockname Lib “wsock32.dll” (ByVal s As Long, sName As sockaddr, namelen As Long) As Long
Public Declare Function getsockopt Lib “wsock32.dll” (ByVal s As Long, ByVal level As Long, ByVal optname As Long, optval As Any, optlen As Long) As Long
Public Declare Function htonl Lib “wsock32.dll” (ByVal hostlong As Long) As Long
Public Declare Function htons Lib “wsock32.dll” (ByVal hostshort As Long) As Integer
Public Declare Function inet_addr Lib “wsock32.dll” (ByVal cp As String) As Long
Public Declare Function inet_ntoa Lib “wsock32.dll” (ByVal inn As Long) As Long
Public Declare Function listen Lib “wsock32.dll” (ByVal s As Long, ByVal backlog As Long) As Long
Public Declare Function ntohl Lib “wsock32.dll” (ByVal netlong As Long) As Long
Public Declare Function ntohs Lib “wsock32.dll” (ByVal netshort As Long) As Integer
Public Declare Function recv Lib “wsock32.dll” (ByVal s As Long, ByVal buf As Any, ByVal buflen As Long, ByVal flags As Long) As Long
Public Declare Function recvfrom Lib “wsock32.dll” (ByVal s As Long, buf As Any, ByVal buflen As Long, ByVal flags As Long, from As sockaddr, fromlen As Long) As Long
Public Declare Function ws_select Lib “wsock32.dll” Alias “select” (ByVal nfds As Long, readfds As fd_set, writefds As fd_set, exceptfds As fd_set, timeout As timeval) As Long
Public Declare Function send Lib “wsock32.dll” (ByVal s As Long, buf As Any, ByVal buflen As Long, ByVal flags As Long) As Long
Public Declare Function sendto Lib “wsock32.dll” (ByVal s As Long, buf As Any, ByVal buflen As Long, ByVal flags As Long, to_addr As sockaddr, ByVal tolen As Long) As Long
Public Declare Function setsockopt Lib “wsock32.dll” (ByVal s As Long, ByVal level As Long, ByVal optname As Long, optval As Any, ByVal optlen As Long) As Long
Public Declare Function ShutDown Lib “wsock32.dll” Alias “shutdown” (ByVal s As Long, ByVal how As Long) As Long
Public Declare Function socket Lib “wsock32.dll” (ByVal af As Long, ByVal s_type As Long, ByVal protocol As Long) As Long
'—DATABASE FUNCTIONS
Public Declare Function gethostbyaddr Lib “wsock32.dll” (addr As Long, ByVal addr_len As Long, ByVal addr_type As Long) As Long
Public Declare Function gethostbyname Lib “wsock32.dll” (ByVal host_name As String) As Long
Public Declare Function gethostname Lib “wsock32.dll” (ByVal host_name As String, ByVal namelen As Long) As Long
Public Declare Function getservbyport Lib “wsock32.dll” (ByVal Port As Long, ByVal proto As String) As Long
Public Declare Function getservbyname Lib “wsock32.dll” (ByVal serv_name As String, ByVal proto As String) As Long
Public Declare Function getprotobynumber Lib “wsock32.dll” (ByVal proto As Long) As Long
Public Declare Function getprotobyname Lib “wsock32.dll” (ByVal proto_name As String) As Long
'—WINDOWS EXTENSIONS
Public Declare Function WSAStartup Lib “wsock32.dll” (ByVal wVR As Long, lpWSAD As WSADataType) As Long
Public Declare Function WSACleanup Lib “wsock32.dll” () As Long
Public Declare Sub WSASetLastError Lib “wsock32.dll” (ByVal iError As Long)
Public Declare Function WSAGetLastError Lib “wsock32.dll” () As Long
Public Declare Function WSAIsBlocking Lib “wsock32.dll” () As Long
Public Declare Function WSAUnhookBlockingHook Lib “wsock32.dll” () As Long
Public Declare Function WSASetBlockingHook Lib “wsock32.dll” (ByVal lpBlockFunc As Long) As Long
Public Declare Function WSACancelBlockingCall Lib “wsock32.dll” () As Long
Public Declare Function WSAAsyncGetServByName Lib “wsock32.dll” (ByVal hWnd As Long, ByVal wMsg As Long, ByVal serv_name As String, ByVal proto As String, buf As Any, ByVal buflen As Long) As Long
Public Declare Function WSAAsyncGetServByPort Lib “wsock32.dll” (ByVal hWnd As Long, ByVal wMsg As Long, ByVal Port As Long, ByVal proto As String, buf As Any, ByVal buflen As Long) As Long
Public Declare Function WSAAsyncGetProtoByName Lib “wsock32.dll” (ByVal hWnd As Long, ByVal wMsg As Long, ByVal proto_name As String, buf As Any, ByVal buflen As Long) As Long
Public Declare Function WSAAsyncGetProtoByNumber Lib “wsock32.dll” (ByVal hWnd As Long, ByVal wMsg As Long, ByVal number As Long, buf As Any, ByVal buflen As Long) As Long
Public Declare Function WSAAsyncGetHostByName Lib “wsock32.dll” (ByVal hWnd As Long, ByVal wMsg As Long, ByVal host_name As String, buf As Any, ByVal buflen As Long) As Long
Public Declare Function WSAAsyncGetHostByAddr Lib “wsock32.dll” (ByVal hWnd As Long, ByVal wMsg As Long, addr As Long, ByVal addr_len As Long, ByVal addr_type As Long, buf As Any, ByVal buflen As Long) As Long
Public Declare Function WSACancelAsyncRequest Lib “wsock32.dll” (ByVal hAsyncTaskHandle As Long) As Long
Public Declare Function WSAAsyncSelect Lib “wsock32.dll” (ByVal s As Long, ByVal hWnd As Long, ByVal wMsg As Long, ByVal lEvent As Long) As Long
Public Declare Function WSARecvEx Lib “wsock32.dll” (ByVal s As Long, buf As Any, ByVal buflen As Long, ByVal flags As Long) As Long
#End If
'SOME STUFF I ADDED
Public MySocket%
Public SockReadBuffer$
Public Const WSA_NoName = “Unknown”
Public WSAStartedUp As Boolean 'Flag to keep track of whether winsock WSAStartup wascalled
Public Function WSAGetAsyncBufLen(ByVal lParam As Long) As Long
If (lParam And &HFFFF&) > &H7FFF Then
WSAGetAsyncBufLen = (lParam And &HFFFF&) – &H10000
Else
WSAGetAsyncBufLen = lParam And &HFFFF&
End If
End Function
Public Function WSAGetSelectEvent(ByVal lParam As Long) As Integer
If (lParam And &HFFFF&) > &H7FFF Then
WSAGetSelectEvent = (lParam And &HFFFF&) – &H10000
Else
WSAGetSelectEvent = lParam And &HFFFF&
End If
End Function
Public Function WSAGetAsyncError(ByVal lParam As Long) As Integer
WSAGetAsyncError = (lParam And &HFFFF0000) \ &H10000
End Function
Public Function AddrToIP(ByVal AddrOrIP$) As String
AddrToIP$ = GetAscIP(GetHostByNameAlias(AddrOrIP$))
End Function
'this function should work on 16 and 32 bit systems
#If Win16 Then
Function ConnectSock(ByVal Host$, ByVal Port%, retIpPort$, ByVal HWndToMsg%, ByVal Async%) As Integer
Dim s%, SelectOps%, dummy%
#ElseIf Win32 Then
Function ConnectSock(ByVal Host$, ByVal Port&, retIpPort$, ByVal HWndToMsg&, ByVal Async%) As Long
Dim s&, SelectOps&, dummy&
#End If
Dim sockin As sockaddr
SockReadBuffer$ = “”
sockin = saZero
sockin.sin_family = AF_INET
sockin.sin_port = htons(Port)
If sockin.sin_port = INVALID_SOCKET Then
ConnectSock = INVALID_SOCKET
Exit Function
End If
sockin.sin_addr = GetHostByNameAlias(Host$)
If sockin.sin_addr = INADDR_NONE Then
ConnectSock = INVALID_SOCKET
Exit Function
End If
retIpPort$ = GetAscIP$(sockin.sin_addr) & “:” & ntohs(sockin.sin_port)
s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)
If s < 0 Then ConnectSock = INVALID_SOCKET Exit Function End If If SetSockLinger(s, 1, 0) = SOCKET_ERROR Then If s > 0 Then
dummy = closesocket(s)
End If
ConnectSock = INVALID_SOCKET
Exit Function
End If
If Not Async Then
If Not connect(s, sockin, sockaddr_size) = 0 Then
If s > 0 Then
dummy = closesocket(s)
End If
ConnectSock = INVALID_SOCKET
Exit Function
End If
If HWndToMsg <> 0 Then
SelectOps = FD_READ Or FD_WRITE Or FD_CONNECT Or FD_CLOSE
If WSAAsyncSelect(s, HWndToMsg, ByVal 1025, ByVal SelectOps) Then
If s > 0 Then
dummy = closesocket(s)
End If
ConnectSock = INVALID_SOCKET
Exit Function
End If
End If
Else
SelectOps = FD_READ Or FD_WRITE Or FD_CONNECT Or FD_CLOSE
If WSAAsyncSelect(s, HWndToMsg, ByVal 1025, ByVal SelectOps) Then
If s > 0 Then
dummy = closesocket(s)
End If
ConnectSock = INVALID_SOCKET
Exit Function
End If
If connect(s, sockin, sockaddr_size) <> -1 Then
If s > 0 Then
dummy = closesocket(s)
End If
ConnectSock = INVALID_SOCKET
Exit Function
End If
End If
ConnectSock = s
End Function
#If Win32 Then
Public Function SetSockLinger(ByVal SockNum&, ByVal OnOff%, ByVal LingerTime%) As Long
#Else
Public Function SetSockLinger(ByVal SockNum%, ByVal OnOff%, ByVal LingerTime%) As Integer
#End If
Dim Linger As LingerType
Linger.l_onoff = OnOff
Linger.l_linger = LingerTime
If setsockopt(SockNum, SOL_SOCKET, SO_LINGER, Linger, 4) Then
Debug.Print “Error setting linger info: ” & WSAGetLastError()
SetSockLinger = SOCKET_ERROR
Else
If getsockopt(SockNum, SOL_SOCKET, SO_LINGER, Linger, 4) Then
Debug.Print “Error getting linger info: ” & WSAGetLastError()
SetSockLinger = SOCKET_ERROR
Else
Debug.Print “Linger is on if nonzero: “; Linger.l_onoff
Debug.Print “Linger time if linger is on: “; Linger.l_linger
End If
End If
End Function
Sub EndWinsock()
Dim ret&
If WSAIsBlocking() Then
ret = WSACancelBlockingCall()
End If
ret = WSACleanup()
WSAStartedUp = False
End Sub
' not know if fd_set and timeval are properly defined.
' (2) Alot of the functions are declared with “buf as any”, when calling these
' functions you may either pass strings, byte arrays or UDT's. For 32bit I
' I recommend Byte arrays and the use of memcopy to copy the data back out
' (3) The async functions (wsaAsync*) require the use of a message hook or
' message window control to capture messages sent by the winsock stack. This
' is not to be confused with a CallBack control, The only function that uses
' callbacks is WSASetBlockingHook()
' (4) Alot of “helper” functions are provided in the file for various things
' before attempting to figure out how to call a function, look and see if
' there is already a helper function for it.
' (5) Data types (hostent etc) have kept there 16bit definitions, even under 32bit
' windows due to the problem of them not working when redfined following the
' suggested rules.
' (1) I have never used WS_SELECT (select), therefore I must warn that I do
' not know if fd_set and timeval are properly defined.
' (2) Alot of the functions are declared with “buf as any”, when calling these
' functions you may either pass strings, byte arrays or UDT's. For 32bit I
' I recommend Byte arrays and the use of memcopy to copy the data back out
' (3) The async functions (wsaAsync*) require the use of a message hook or
' message window control to capture messages sent by the winsock stack. This
' is not to be confused with a CallBack control, The only function that uses
' callbacks is WSASetBlockingHook()
' (4) Alot of “helper” functions are provided in the file for various things
' before attempting to figure out how to call a function, look and see if
' there is already a helper function for it.
' (5) Data types (hostent etc) have kept there 16bit definitions, even under 32bit
' windows due to the problem of them not working when redfined following the
' suggested rules.
Option Explicit
Public Const FD_SETSIZE = 64
Type fd_set
fd_count As Integer
fd_array(FD_SETSIZE) As Integer
End Type
Type timeval
tv_sec As Long
tv_usec As Long
End Type
Type HostEnt
h_name As Long
h_aliases As Long
h_addrtype As Integer
h_length As Integer
h_addr_list As Long
End Type
Public Const hostent_size = 16
Type servent
s_name As Long
s_aliases As Long
s_port As Integer
s_proto As Long
End Type
Public Const servent_size = 14
Type protoent
p_name As Long
p_aliases As Long
p_proto As Integer
End Type
Public Const protoent_size = 10
Public Const IPPROTO_TCP = 6
Public Const IPPROTO_UDP = 17
Public Const INADDR_NONE = &HFFFFFFFF
Public Const INADDR_ANY = &H0
Type sockaddr
sin_family As Integer
sin_port As Integer
sin_addr As Long
sin_zero As String * 8
End Type
Public Const sockaddr_size = 16
Public saZero As sockaddr
Public Const WSA_DESCRIPTIONLEN = 256
Public Const WSA_DescriptionSize = WSA_DESCRIPTIONLEN + 1
Public Const WSA_SYS_STATUS_LEN = 128
Public Const WSA_SysStatusSize = WSA_SYS_STATUS_LEN + 1
Type WSADataType
wVersion As Integer
wHighVersion As Integer
szDescription As String * WSA_DescriptionSize
szSystemStatus As String * WSA_SysStatusSize
iMaxSockets As Integer
iMaxUdpDg As Integer
lpVendorInfo As Long
End Type
Public Const INVALID_SOCKET = -1
Public Const SOCKET_ERROR = -1
Public Const SOCK_STREAM = 1
Public Const SOCK_DGRAM = 2
Public Const MAXGETHOSTSTRUCT = 1024
Public Const AF_INET = 2
Public Const PF_INET = 2
Type LingerType
l_onoff As Integer
l_linger As Integer
End Type
' Windows Sockets definitions of regular Microsoft C error constants
Global Const WSAEINTR = 10004
Global Const WSAEBADF = 10009
Global Const WSAEACCES = 10013
Global Const WSAEFAULT = 10014
Global Const WSAEINVAL = 10022
Global Const WSAEMFILE = 10024
' Windows Sockets definitions of regular Berkeley error constants
Global Const WSAEWOULDBLOCK = 10035
Global Const WSAEINPROGRESS = 10036
Global Const WSAEALREADY = 10037
Global Const WSAENOTSOCK = 10038
Global Const WSAEDESTADDRREQ = 10039
Global Const WSAEMSGSIZE = 10040
Global Const WSAEPROTOTYPE = 10041
Global Const WSAENOPROTOOPT = 10042
Global Const WSAEPROTONOSUPPORT = 10043
Global Const WSAESOCKTNOSUPPORT = 10044
Global Const WSAEOPNOTSUPP = 10045
Global Const WSAEPFNOSUPPORT = 10046
Global Const WSAEAFNOSUPPORT = 10047
Global Const WSAEADDRINUSE = 10048
Global Const WSAEADDRNOTAVAIL = 10049
Global Const WSAENETDOWN = 10050
Global Const WSAENETUNREACH = 10051
Global Const WSAENETRESET = 10052
Global Const WSAECONNABORTED = 10053
Global Const WSAECONNRESET = 10054
Global Const WSAENOBUFS = 10055
Global Const WSAEISCONN = 10056
Global Const WSAENOTCONN = 10057
Global Const WSAESHUTDOWN = 10058
Global Const WSAETOOMANYREFS = 10059
Global Const WSAETIMEDOUT = 10060
Global Const WSAECONNREFUSED = 10061
Global Const WSAELOOP = 10062
Global Const WSAENAMETOOLONG = 10063
Global Const WSAEHOSTDOWN = 10064
Global Const WSAEHOSTUNREACH = 10065
Global Const WSAENOTEMPTY = 10066
Global Const WSAEPROCLIM = 10067
Global Const WSAEUSERS = 10068
Global Const WSAEDQUOT = 10069
Global Const WSAESTALE = 10070
Global Const WSAEREMOTE = 10071
' Extended Windows Sockets error constant definitions
Global Const WSASYSNOTREADY = 10091
Global Const WSAVERNOTSUPPORTED = 10092
Global Const WSANOTINITIALISED = 10093
Global Const WSAHOST_NOT_FOUND = 11001
Global Const WSATRY_AGAIN = 11002
Global Const WSANO_RECOVERY = 11003
Global Const WSANO_DATA = 11004
Global Const WSANO_ADDRESS = 11004
'—ioctl Constants
Public Const FIONREAD = &H8004667F
Public Const FIONBIO = &H8004667E
Public Const FIOASYNC = &H8004667D
#If Win16 Then
'—Windows System functions
Public Declare Function PostMessage Lib “User” (ByVal hWnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, lParam As Any) As Integer
Public Declare Sub MemCopy Lib “Kernel” Alias “hmemcpy” (Dest As Any, Src As Any, ByVal cb&)
Public Declare Function lstrlen Lib “Kernel” (ByVal lpString As Any) As Integer
'—async notification constants
Public Const SOL_SOCKET = &HFFFF
Public Const SO_LINGER = &H80
Public Const FD_READ = &H1
Public Const FD_WRITE = &H2
Public Const FD_OOB = &H4
Public Const FD_ACCEPT = &H8
Public Const FD_CONNECT = &H10
Public Const FD_CLOSE = &H20
'—SOCKET FUNCTIONS
Public Declare Function accept Lib “Winsock.dll” (ByVal s As Integer, addr As sockaddr, addrlen As Integer) As Integer
Public Declare Function bind Lib “Winsock.dll” (ByVal s As Integer, addr As sockaddr, ByVal namelen As Integer) As Integer
Public Declare Function closesocket Lib “Winsock.dll” (ByVal s As Integer) As Integer
Public Declare Function connect Lib “Winsock.dll” (ByVal s As Integer, addr As sockaddr, ByVal namelen As Integer) As Integer
Public Declare Function ioctlsocket Lib “Winsock.dll” (ByVal s As Integer, ByVal cmd As Long, argp As Long) As Integer
Public Declare Function getpeername Lib “Winsock.dll” (ByVal s As Integer, sName As sockaddr, namelen As Integer) As Integer
Public Declare Function getsockname Lib “Winsock.dll” (ByVal s As Integer, sName As sockaddr, namelen As Integer) As Integer
Public Declare Function getsockopt Lib “Winsock.dll” (ByVal s As Integer, ByVal level As Integer, ByVal optname As Integer, optval As Any, optlen As Integer) As Integer
Public Declare Function htonl Lib “Winsock.dll” (ByVal hostlong As Long) As Long
Public Declare Function htons Lib “Winsock.dll” (ByVal hostshort As Integer) As Integer
Public Declare Function inet_addr Lib “Winsock.dll” (ByVal cp As String) As Long
Public Declare Function inet_ntoa Lib “Winsock.dll” (ByVal inn As Long) As Long
Public Declare Function listen Lib “Winsock.dll” (ByVal s As Integer, ByVal backlog As Integer) As Integer
Public Declare Function ntohl Lib “Winsock.dll” (ByVal netlong As Long) As Long
Public Declare Function ntohs Lib “Winsock.dll” (ByVal netshort As Integer) As Integer
Public Declare Function recv Lib “Winsock.dll” (ByVal s As Integer, ByVal buf As Any, ByVal buflen As Integer, ByVal flags As Integer) As Integer
Public Declare Function recvfrom Lib “Winsock.dll” (ByVal s As Integer, buf As Any, ByVal buflen As Integer, ByVal flags As Integer, from As sockaddr, fromlen As Integer) As Integer
Public Declare Function ws_select Lib “Winsock.dll” Alias “select” (ByVal nfds As Integer, readfds As Any, writefds As Any, exceptfds As Any, timeout As timeval) As Integer
Public Declare Function send Lib “Winsock.dll” (ByVal s As Integer, buf As Any, ByVal buflen As Integer, ByVal flags As Integer) As Integer
Public Declare Function sendto Lib “Winsock.dll” (ByVal s As Integer, buf As Any, ByVal buflen As Integer, ByVal flags As Integer, to_addr As sockaddr, ByVal tolen As Integer) As Integer
Public Declare Function setsockopt Lib “Winsock.dll” (ByVal s As Integer, ByVal level As Integer, ByVal optname As Integer, optval As Any, ByVal optlen As Integer) As Integer
Public Declare Function ShutDown Lib “Winsock.dll” Alias “shutdown” (ByVal s As Integer, ByVal how As Integer) As Integer
Public Declare Function socket Lib “Winsock.dll” (ByVal af As Integer, ByVal s_type As Integer, ByVal protocol As Integer) As Integer
'—DATABASE FUNCTIONS
Public Declare Function gethostbyaddr Lib “Winsock.dll” (addr As Long, ByVal addr_len As Integer, ByVal addr_type As Integer) As Long
Public Declare Function gethostbyname Lib “Winsock.dll” (ByVal host_name As String) As Long
Public Declare Function gethostname Lib “Winsock.dll” (ByVal host_name As String, ByVal namelen As Integer) As Integer
Public Declare Function getservbyport Lib “Winsock.dll” (ByVal Port As Integer, ByVal proto As String) As Long
Public Declare Function getservbyname Lib “Winsock.dll” (ByVal serv_name As String, ByVal proto As String) As Long
Public Declare Function getprotobynumber Lib “Winsock.dll” (ByVal proto As Integer) As Long
Public Declare Function getprotobyname Lib “Winsock.dll” (ByVal proto_name As String) As Long
'—WINDOWS EXTENSIONS
Public Declare Function WSAStartup Lib “Winsock.dll” (ByVal wVR As Integer, lpWSAD As WSADataType) As Integer
Public Declare Function WSACleanup Lib “Winsock.dll” () As Integer
Public Declare Sub WSASetLastError Lib “Winsock.dll” (ByVal iError As Integer)
Public Declare Function WSAGetLastError Lib “Winsock.dll” () As Integer
Public Declare Function WSAIsBlocking Lib “Winsock.dll” () As Integer
Public Declare Function WSAUnhookBlockingHook Lib “Winsock.dll” () As Integer
Public Declare Function WSASetBlockingHook Lib “Winsock.dll” (ByVal lpBlockFunc As Long) As Long
Public Declare Function WSACancelBlockingCall Lib “Winsock.dll” () As Integer
Public Declare Function WSAAsyncGetServByName Lib “Winsock.dll” (ByVal hWnd As Integer, ByVal wMsg As Integer, ByVal serv_name As String, ByVal proto As String, buf As Any, ByVal buflen As Integer) As Integer
Public Declare Function WSAAsyncGetServByPort Lib “Winsock.dll” (ByVal hWnd As Integer, ByVal wMsg As Integer, ByVal Port As Integer, ByVal proto As String, buf As Any, ByVal buflen As Integer) As Integer
Public Declare Function WSAAsyncGetProtoByName Lib “Winsock.dll” (ByVal hWnd As Integer, ByVal wMsg As Integer, ByVal proto_name As String, buf As Any, ByVal buflen As Integer) As Integer
Public Declare Function WSAAsyncGetProtoByNumber Lib “Winsock.dll” (ByVal hWnd As Integer, ByVal wMsg As Integer, ByVal number As Integer, buf As Any, ByVal buflen As Integer) As Integer
Public Declare Function WSAAsyncGetHostByName Lib “Winsock.dll” (ByVal hWnd As Integer, ByVal wMsg As Integer, ByVal host_name As String, buf As Any, ByVal buflen As Integer) As Integer
Public Declare Function WSAAsyncGetHostByAddr Lib “Winsock.dll” (ByVal hWnd As Integer, ByVal wMsg As Integer, addr As Long, ByVal addr_len As Integer, ByVal addr_type As Integer, buf As Any, ByVal buflen As Integer) As Integer
Public Declare Function WSACancelAsyncRequest Lib “Winsock.dll” (ByVal hAsyncTaskHandle As Integer) As Integer
Public Declare Function WSAAsyncSelect Lib “Winsock.dll” (ByVal s As Integer, ByVal hWnd As Integer, ByVal wMsg As Integer, ByVal lEvent As Long) As Integer
Public Declare Function WSARecvEx Lib “Winsock.dll” (ByVal s As Integer, buf As Any, ByVal buflen As Integer, ByVal flags As Integer) As Integer
#ElseIf Win32 Then
'—Windows System Functions
Public Declare Function PostMessage Lib “user32” Alias “PostMessageA” (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Public Declare Sub MemCopy Lib “kernel32” Alias “RtlMoveMemory” (Dest As Any, Src As Any, ByVal cb&)
Public Declare Function lstrlen Lib “kernel32” Alias “lstrlenA” (ByVal lpString As Any) As Long
'—async notification constants
Public Const SOL_SOCKET = &HFFFF&
Public Const SO_LINGER = &H80&
Public Const FD_READ = &H1&
Public Const FD_WRITE = &H2&
Public Const FD_OOB = &H4&
Public Const FD_ACCEPT = &H8&
Public Const FD_CONNECT = &H10&
Public Const FD_CLOSE = &H20&
'—SOCKET FUNCTIONS
Public Declare Function accept Lib “wsock32.dll” (ByVal s As Long, addr As sockaddr, addrlen As Long) As Long
Public Declare Function bind Lib “wsock32.dll” (ByVal s As Long, addr As sockaddr, ByVal namelen As Long) As Long
Public Declare Function closesocket Lib “wsock32.dll” (ByVal s As Long) As Long
Public Declare Function connect Lib “wsock32.dll” (ByVal s As Long, addr As sockaddr, ByVal namelen As Long) As Long
Public Declare Function ioctlsocket Lib “wsock32.dll” (ByVal s As Long, ByVal cmd As Long, argp As Long) As Long
Public Declare Function getpeername Lib “wsock32.dll” (ByVal s As Long, sName As sockaddr, namelen As Long) As Long
Public Declare Function getsockname Lib “wsock32.dll” (ByVal s As Long, sName As sockaddr, namelen As Long) As Long
Public Declare Function getsockopt Lib “wsock32.dll” (ByVal s As Long, ByVal level As Long, ByVal optname As Long, optval As Any, optlen As Long) As Long
Public Declare Function htonl Lib “wsock32.dll” (ByVal hostlong As Long) As Long
Public Declare Function htons Lib “wsock32.dll” (ByVal hostshort As Long) As Integer
Public Declare Function inet_addr Lib “wsock32.dll” (ByVal cp As String) As Long
Public Declare Function inet_ntoa Lib “wsock32.dll” (ByVal inn As Long) As Long
Public Declare Function listen Lib “wsock32.dll” (ByVal s As Long, ByVal backlog As Long) As Long
Public Declare Function ntohl Lib “wsock32.dll” (ByVal netlong As Long) As Long
Public Declare Function ntohs Lib “wsock32.dll” (ByVal netshort As Long) As Integer
Public Declare Function recv Lib “wsock32.dll” (ByVal s As Long, ByVal buf As Any, ByVal buflen As Long, ByVal flags As Long) As Long
Public Declare Function recvfrom Lib “wsock32.dll” (ByVal s As Long, buf As Any, ByVal buflen As Long, ByVal flags As Long, from As sockaddr, fromlen As Long) As Long
Public Declare Function ws_select Lib “wsock32.dll” Alias “select” (ByVal nfds As Long, readfds As fd_set, writefds As fd_set, exceptfds As fd_set, timeout As timeval) As Long
Public Declare Function send Lib “wsock32.dll” (ByVal s As Long, buf As Any, ByVal buflen As Long, ByVal flags As Long) As Long
Public Declare Function sendto Lib “wsock32.dll” (ByVal s As Long, buf As Any, ByVal buflen As Long, ByVal flags As Long, to_addr As sockaddr, ByVal tolen As Long) As Long
Public Declare Function setsockopt Lib “wsock32.dll” (ByVal s As Long, ByVal level As Long, ByVal optname As Long, optval As Any, ByVal optlen As Long) As Long
Public Declare Function ShutDown Lib “wsock32.dll” Alias “shutdown” (ByVal s As Long, ByVal how As Long) As Long
Public Declare Function socket Lib “wsock32.dll” (ByVal af As Long, ByVal s_type As Long, ByVal protocol As Long) As Long
'—DATABASE FUNCTIONS
Public Declare Function gethostbyaddr Lib “wsock32.dll” (addr As Long, ByVal addr_len As Long, ByVal addr_type As Long) As Long
Public Declare Function gethostbyname Lib “wsock32.dll” (ByVal host_name As String) As Long
Public Declare Function gethostname Lib “wsock32.dll” (ByVal host_name As String, ByVal namelen As Long) As Long
Public Declare Function getservbyport Lib “wsock32.dll” (ByVal Port As Long, ByVal proto As String) As Long
Public Declare Function getservbyname Lib “wsock32.dll” (ByVal serv_name As String, ByVal proto As String) As Long
Public Declare Function getprotobynumber Lib “wsock32.dll” (ByVal proto As Long) As Long
Public Declare Function getprotobyname Lib “wsock32.dll” (ByVal proto_name As String) As Long
'—WINDOWS EXTENSIONS
Public Declare Function WSAStartup Lib “wsock32.dll” (ByVal wVR As Long, lpWSAD As WSADataType) As Long
Public Declare Function WSACleanup Lib “wsock32.dll” () As Long
Public Declare Sub WSASetLastError Lib “wsock32.dll” (ByVal iError As Long)
Public Declare Function WSAGetLastError Lib “wsock32.dll” () As Long
Public Declare Function WSAIsBlocking Lib “wsock32.dll” () As Long
Public Declare Function WSAUnhookBlockingHook Lib “wsock32.dll” () As Long
Public Declare Function WSASetBlockingHook Lib “wsock32.dll” (ByVal lpBlockFunc As Long) As Long
Public Declare Function WSACancelBlockingCall Lib “wsock32.dll” () As Long
Public Declare Function WSAAsyncGetServByName Lib “wsock32.dll” (ByVal hWnd As Long, ByVal wMsg As Long, ByVal serv_name As String, ByVal proto As String, buf As Any, ByVal buflen As Long) As Long
Public Declare Function WSAAsyncGetServByPort Lib “wsock32.dll” (ByVal hWnd As Long, ByVal wMsg As Long, ByVal Port As Long, ByVal proto As String, buf As Any, ByVal buflen As Long) As Long
Public Declare Function WSAAsyncGetProtoByName Lib “wsock32.dll” (ByVal hWnd As Long, ByVal wMsg As Long, ByVal proto_name As String, buf As Any, ByVal buflen As Long) As Long
Public Declare Function WSAAsyncGetProtoByNumber Lib “wsock32.dll” (ByVal hWnd As Long, ByVal wMsg As Long, ByVal number As Long, buf As Any, ByVal buflen As Long) As Long
Public Declare Function WSAAsyncGetHostByName Lib “wsock32.dll” (ByVal hWnd As Long, ByVal wMsg As Long, ByVal host_name As String, buf As Any, ByVal buflen As Long) As Long
Public Declare Function WSAAsyncGetHostByAddr Lib “wsock32.dll” (ByVal hWnd As Long, ByVal wMsg As Long, addr As Long, ByVal addr_len As Long, ByVal addr_type As Long, buf As Any, ByVal buflen As Long) As Long
Public Declare Function WSACancelAsyncRequest Lib “wsock32.dll” (ByVal hAsyncTaskHandle As Long) As Long
Public Declare Function WSAAsyncSelect Lib “wsock32.dll” (ByVal s As Long, ByVal hWnd As Long, ByVal wMsg As Long, ByVal lEvent As Long) As Long
Public Declare Function WSARecvEx Lib “wsock32.dll” (ByVal s As Long, buf As Any, ByVal buflen As Long, ByVal flags As Long) As Long
#End If
'SOME STUFF I ADDED
Public MySocket%
Public SockReadBuffer$
Public Const WSA_NoName = “Unknown”
Public WSAStartedUp As Boolean 'Flag to keep track of whether winsock WSAStartup wascalled
Public Function WSAGetAsyncBufLen(ByVal lParam As Long) As Long
If (lParam And &HFFFF&) > &H7FFF Then
WSAGetAsyncBufLen = (lParam And &HFFFF&) – &H10000
Else
WSAGetAsyncBufLen = lParam And &HFFFF&
End If
End Function
Public Function WSAGetSelectEvent(ByVal lParam As Long) As Integer
If (lParam And &HFFFF&) > &H7FFF Then
WSAGetSelectEvent = (lParam And &HFFFF&) – &H10000
Else
WSAGetSelectEvent = lParam And &HFFFF&
End If
End Function
Public Function WSAGetAsyncError(ByVal lParam As Long) As Integer
WSAGetAsyncError = (lParam And &HFFFF0000) \ &H10000
End Function
Public Function AddrToIP(ByVal AddrOrIP$) As String
AddrToIP$ = GetAscIP(GetHostByNameAlias(AddrOrIP$))
End Function
'this function should work on 16 and 32 bit systems
#If Win16 Then
Function ConnectSock(ByVal Host$, ByVal Port%, retIpPort$, ByVal HWndToMsg%, ByVal Async%) As Integer
Dim s%, SelectOps%, dummy%
#ElseIf Win32 Then
Function ConnectSock(ByVal Host$, ByVal Port&, retIpPort$, ByVal HWndToMsg&, ByVal Async%) As Long
Dim s&, SelectOps&, dummy&
#End If
Dim sockin As sockaddr
SockReadBuffer$ = “”
sockin = saZero
sockin.sin_family = AF_INET
sockin.sin_port = htons(Port)
If sockin.sin_port = INVALID_SOCKET Then
ConnectSock = INVALID_SOCKET
Exit Function
End If
sockin.sin_addr = GetHostByNameAlias(Host$)
If sockin.sin_addr = INADDR_NONE Then
ConnectSock = INVALID_SOCKET
Exit Function
End If
retIpPort$ = GetAscIP$(sockin.sin_addr) & “:” & ntohs(sockin.sin_port)
s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)
If s < 0 Then ConnectSock = INVALID_SOCKET Exit Function End If If SetSockLinger(s, 1, 0) = SOCKET_ERROR Then If s > 0 Then
dummy = closesocket(s)
End If
ConnectSock = INVALID_SOCKET
Exit Function
End If
If Not Async Then
If Not connect(s, sockin, sockaddr_size) = 0 Then
If s > 0 Then
dummy = closesocket(s)
End If
ConnectSock = INVALID_SOCKET
Exit Function
End If
If HWndToMsg <> 0 Then
SelectOps = FD_READ Or FD_WRITE Or FD_CONNECT Or FD_CLOSE
If WSAAsyncSelect(s, HWndToMsg, ByVal 1025, ByVal SelectOps) Then
If s > 0 Then
dummy = closesocket(s)
End If
ConnectSock = INVALID_SOCKET
Exit Function
End If
End If
Else
SelectOps = FD_READ Or FD_WRITE Or FD_CONNECT Or FD_CLOSE
If WSAAsyncSelect(s, HWndToMsg, ByVal 1025, ByVal SelectOps) Then
If s > 0 Then
dummy = closesocket(s)
End If
ConnectSock = INVALID_SOCKET
Exit Function
End If
If connect(s, sockin, sockaddr_size) <> -1 Then
If s > 0 Then
dummy = closesocket(s)
End If
ConnectSock = INVALID_SOCKET
Exit Function
End If
End If
ConnectSock = s
End Function
#If Win32 Then
Public Function SetSockLinger(ByVal SockNum&, ByVal OnOff%, ByVal LingerTime%) As Long
#Else
Public Function SetSockLinger(ByVal SockNum%, ByVal OnOff%, ByVal LingerTime%) As Integer
#End If
Dim Linger As LingerType
Linger.l_onoff = OnOff
Linger.l_linger = LingerTime
If setsockopt(SockNum, SOL_SOCKET, SO_LINGER, Linger, 4) Then
Debug.Print “Error setting linger info: ” & WSAGetLastError()
SetSockLinger = SOCKET_ERROR
Else
If getsockopt(SockNum, SOL_SOCKET, SO_LINGER, Linger, 4) Then
Debug.Print “Error getting linger info: ” & WSAGetLastError()
SetSockLinger = SOCKET_ERROR
Else
Debug.Print “Linger is on if nonzero: “; Linger.l_onoff
Debug.Print “Linger time if linger is on: “; Linger.l_linger
End If
End If
End Function
Sub EndWinsock()
Dim ret&
If WSAIsBlocking() Then
ret = WSACancelBlockingCall()
End If
ret = WSACleanup()
WSAStartedUp = False
End Sub