📄 wsksock.bas
字号:
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
Public Function GetAscIP(ByVal inn As Long) As String
#If Win32 Then
Dim nStr&
#Else
Dim nStr%
#End If
Dim lpStr&
Dim retString$
retString = String(32, 0)
lpStr = inet_ntoa(inn)
If lpStr Then
nStr = lstrlen(lpStr)
If nStr > 32 Then nStr = 32
MemCopy ByVal retString, ByVal lpStr, nStr
retString = Left(retString, nStr)
GetAscIP = retString
Else
GetAscIP = "255.255.255.255"
End If
End Function
Public Function GetHostByAddress(ByVal addr As Long) As String
Dim phe&, ret&
Dim heDestHost As HostEnt
Dim HostName$
phe = gethostbyaddr(addr, 4, PF_INET)
If phe Then
MemCopy heDestHost, ByVal phe, hostent_size
HostName = String(256, 0)
MemCopy ByVal HostName, ByVal heDestHost.h_name, 256
GetHostByAddress = Left(HostName, InStr(HostName, Chr(0)) - 1)
Else
GetHostByAddress = WSA_NoName
End If
End Function
'returns IP as long, in network byte order
Public Function GetHostByNameAlias(ByVal HostName$) As Long
'Return IP address as a long, in network byte order
Dim phe&
Dim heDestHost As HostEnt
Dim addrList&
Dim retIP&
retIP = inet_addr(HostName$)
If retIP = INADDR_NONE Then
phe = gethostbyname(HostName$)
If phe <> 0 Then
MemCopy heDestHost, ByVal phe, hostent_size
MemCopy addrList, ByVal heDestHost.h_addr_list, 4
MemCopy retIP, ByVal addrList, heDestHost.h_length
Else
retIP = INADDR_NONE
End If
End If
GetHostByNameAlias = retIP
End Function
'returns your local machines name
Public Function GetLocalHostName() As String
Dim sName$
sName = String(256, 0)
If gethostname(sName, 256) Then
sName = WSA_NoName
Else
If InStr(sName, Chr(0)) Then
sName = Left(sName, InStr(sName, Chr(0)) - 1)
End If
End If
GetLocalHostName = sName
End Function
#If Win16 Then
Public Function GetPeerAddress(ByVal s%) As String
Dim addrlen%
#ElseIf Win32 Then
Public Function GetPeerAddress(ByVal s&) As String
Dim addrlen&
#End If
Dim sa As sockaddr
addrlen = sockaddr_size
If getpeername(s, sa, addrlen) Then
GetPeerAddress = ""
Else
GetPeerAddress = SockAddressToString(sa)
End If
End Function
#If Win16 Then
Public Function GetPortFromString(ByVal PortStr$) As Integer
#ElseIf Win32 Then
Public Function GetPortFromString(ByVal PortStr$) As Long
#End If
'sometimes users provide ports outside the range of a VB
'integer, so this function returns an integer for a string
'just to keep an error from happening, it converts the
'number to a negative if needed
If Val(PortStr$) > 32767 Then
GetPortFromString = CInt(Val(PortStr$) - &H10000)
Else
GetPortFromString = Val(PortStr$)
End If
If Err Then GetPortFromString = 0
End Function
#If Win16 Then
Function GetProtocolByName(ByVal protocol$) As Integer
Dim tmpShort%
#ElseIf Win32 Then
Function GetProtocolByName(ByVal protocol$) As Long
Dim tmpShort&
#End If
Dim ppe&
Dim peDestProt As protoent
ppe = getprotobyname(protocol)
If ppe Then
MemCopy peDestProt, ByVal ppe, protoent_size
GetProtocolByName = peDestProt.p_proto
Else
tmpShort = Val(protocol)
If tmpShort Then
GetProtocolByName = htons(tmpShort)
Else
GetProtocolByName = SOCKET_ERROR
End If
End If
End Function
#If Win16 Then
Function GetServiceByName(ByVal service$, ByVal protocol$) As Integer
Dim serv%
#ElseIf Win32 Then
Function GetServiceByName(ByVal service$, ByVal protocol$) As Long
Dim serv&
#End If
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -