📄 winsockbas.bas
字号:
MemCopy peDestProt, ByVal ppe, protoent_size
GetProtocolByName = peDestProt.p_proto
End If
If Err Then GetProtocolByName = SOCKET_ERROR
End Function
'this function should work on 16 and 32 bit systems
#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
On Error Resume Next
Dim pse&
Dim seDestServ As servent
pse = getservbyname(service, protocol)
If pse <> 0 Then
MemCopy seDestServ, ByVal pse, servent_size
GetServiceByName = seDestServ.s_port
Else
serv = Val(service)
If serv <> 0 Then
GetServiceByName = htons(serv)
Else
GetServiceByName = INVALID_SOCKET
End If
End If
If Err Then GetServiceByName = INVALID_SOCKET
End Function
'this function DOES work on 16 and 32 bit systems
#If Win16 Then
Function GetSockAddress(ByVal s%) As String
Dim addrlen%
Dim ret%
#ElseIf Win32 Then
Function GetSockAddress(ByVal s&) As String
Dim addrlen&
Dim ret&
#End If
On Error Resume Next
Dim sa As sockaddr
Dim szRet$
szRet = String(32, 0)
addrlen = sockaddr_size
ret = getsockname(s, sa, addrlen)
If ret = 0 Then
GetSockAddress = SockAddressToString(sa)
Else
GetSockAddress = ""
End If
If Err Then GetSockAddress = ""
End Function
'this function should work on 16 and 32 bit systems
Function GetWSAErrorString(ByVal errnum&) As String
On Error Resume Next
Select Case errnum
Case 10004: GetWSAErrorString = "Interrupted system call."
Case 10009: GetWSAErrorString = "Bad file number."
Case 10013: GetWSAErrorString = "Permission Denied."
Case 10014: GetWSAErrorString = "Bad Address."
Case 10022: GetWSAErrorString = "Invalid Argument."
Case 10024: GetWSAErrorString = "Too many open files."
Case 10035: GetWSAErrorString = "Operation would block."
Case 10036: GetWSAErrorString = "Operation now in progress."
Case 10037: GetWSAErrorString = "Operation already in progress."
Case 10038: GetWSAErrorString = "Socket operation on nonsocket."
Case 10039: GetWSAErrorString = "Destination address required."
Case 10040: GetWSAErrorString = "Message too long."
Case 10041: GetWSAErrorString = "Protocol wrong type for socket."
Case 10042: GetWSAErrorString = "Protocol not available."
Case 10043: GetWSAErrorString = "Protocol not supported."
Case 10044: GetWSAErrorString = "Socket type not supported."
Case 10045: GetWSAErrorString = "Operation not supported on socket."
Case 10046: GetWSAErrorString = "Protocol family not supported."
Case 10047: GetWSAErrorString = "Address family not supported by protocol family."
Case 10048: GetWSAErrorString = "Address already in use."
Case 10049: GetWSAErrorString = "Can't assign requested address."
Case 10050: GetWSAErrorString = "Network is down."
Case 10051: GetWSAErrorString = "Network is unreachable."
Case 10052: GetWSAErrorString = "Network dropped connection."
Case 10053: GetWSAErrorString = "Software caused connection abort."
Case 10054: GetWSAErrorString = "Connection reset by peer."
Case 10055: GetWSAErrorString = "No buffer space available."
Case 10056: GetWSAErrorString = "Socket is already connected."
Case 10057: GetWSAErrorString = "Socket is not connected."
Case 10058: GetWSAErrorString = "Can't send after socket shutdown."
Case 10059: GetWSAErrorString = "Too many references: can't splice."
Case 10060: GetWSAErrorString = "Connection timed out."
Case 10061: GetWSAErrorString = "Connection refused."
Case 10062: GetWSAErrorString = "Too many levels of symbolic links."
Case 10063: GetWSAErrorString = "File name too long."
Case 10064: GetWSAErrorString = "Host is down."
Case 10065: GetWSAErrorString = "No route to host."
Case 10066: GetWSAErrorString = "Directory not empty."
Case 10067: GetWSAErrorString = "Too many processes."
Case 10068: GetWSAErrorString = "Too many users."
Case 10069: GetWSAErrorString = "Disk quota exceeded."
Case 10070: GetWSAErrorString = "Stale NFS file handle."
Case 10071: GetWSAErrorString = "Too many levels of remote in path."
Case 10091: GetWSAErrorString = "Network subsystem is unusable."
Case 10092: GetWSAErrorString = "Winsock DLL cannot support this application."
Case 10093: GetWSAErrorString = "Winsock not initialized."
Case 10101: GetWSAErrorString = "Disconnect."
Case 11001: GetWSAErrorString = "Host not found."
Case 11002: GetWSAErrorString = "Nonauthoritative host not found."
Case 11003: GetWSAErrorString = "Nonrecoverable error."
Case 11004: GetWSAErrorString = "Valid name, no data record of requested type."
Case Else:
End Select
End Function
'this function DOES work on 16 and 32 bit systems
Function IpToAddr(ByVal AddrOrIP$) As String
On Error Resume Next
IpToAddr = GetHostByAddress(GetHostByNameAlias(AddrOrIP$))
If Err Then IpToAddr = WSA_NoName
End Function
'this function DOES work on 16 and 32 bit systems
Function IrcGetAscIp(ByVal IPL$) As String
'this function is IRC specific, it expects a long ip stored in Network byte order, in a string
'the kind that would be parsed out of a DCC command string
On Error GoTo IrcGetAscIPError:
Dim lpStr&
#If Win16 Then
Dim nStr%
#ElseIf Win32 Then
Dim nStr&
#End If
Dim retString$
Dim inn&
If Val(IPL) > 2147483647 Then
inn = Val(IPL) - 4294967296#
Else
inn = Val(IPL)
End If
inn = ntohl(inn)
retString = String(32, 0)
lpStr = inet_ntoa(inn)
If lpStr = 0 Then
IrcGetAscIp = "0.0.0.0"
Exit Function
End If
nStr = lstrlen(lpStr)
If nStr > 32 Then nStr = 32
MemCopy ByVal retString, ByVal lpStr, nStr
retString = Left(retString, nStr)
IrcGetAscIp = retString
Exit Function
IrcGetAscIPError:
IrcGetAscIp = "0.0.0.0"
Exit Function
Resume
End Function
'this function DOES work on 16 and 32 bit systems
Function IrcGetLongIp(ByVal AscIp$) As String
'this function converts an ascii ip string into a long ip in network byte order
'and stick it in a string suitable for use in a DCC command.
On Error GoTo IrcGetLongIpError:
Dim inn&
inn = inet_addr(AscIp)
inn = htonl(inn)
If inn < 0 Then
IrcGetLongIp = CVar(inn + 4294967296#)
Exit Function
Else
IrcGetLongIp = CVar(inn)
Exit Function
End If
Exit Function
IrcGetLongIpError:
IrcGetLongIp = "0"
Exit Function
Resume
End Function
'this function should work on 16 and 32 bit systems
#If Win16 Then
Public Function ListenForConnect(ByVal Port%, ByVal HWndToMsg%) As Integer
Dim s%, dummy%
Dim SelectOps%
#ElseIf Win32 Then
Public Function ListenForConnect(ByVal Port&, ByVal HWndToMsg&) As Long
Dim s&, dummy&
Dim SelectOps&
#End If
Dim sockin As sockaddr
sockin = saZero 'zero out the structure
sockin.sin_family = AF_INET
sockin.sin_port = htons(Port)
If sockin.sin_port = INVALID_SOCKET Then
ListenForConnect = INVALID_SOCKET
Exit Function
End If
sockin.sin_addr = htonl(INADDR_ANY)
If sockin.sin_addr = INADDR_NONE Then
ListenForConnect = INVALID_SOCKET
Exit Function
End If
s = socket(PF_INET, SOCK_STREAM, 0)
If s < 0 Then
ListenForConnect = INVALID_SOCKET
Exit Function
End If
If bind(s, sockin, sockaddr_size) Then
If s > 0 Then
dummy = closesocket(s)
End If
ListenForConnect = INVALID_SOCKET
Exit Function
End If
SelectOps = FD_READ Or FD_WRITE Or FD_CLOSE Or FD_ACCEPT
If WSAAsyncSelect(s, HWndToMsg, ByVal 1025, ByVal SelectOps) Then
If s > 0 Then
dummy = closesocket(s)
End If
ListenForConnect = SOCKET_ERROR
Exit Function
End If
If listen(s, 1) Then
If s > 0 Then
dummy = closesocket(s)
End If
ListenForConnect = INVALID_SOCKET
Exit Function
End If
ListenForConnect = s
End Function
'this function should work on 16 and 32 bit systems
#If Win16 Then
Public Function SendData(ByVal s%, vMessage As Variant) As Integer
#ElseIf Win32 Then
Public Function SendData(ByVal s&, vMessage As Variant) As Long
#End If
Dim TheMsg() As Byte, sTemp$
TheMsg = ""
Select Case VarType(vMessage)
Case 8209 'byte array
sTemp = vMessage
TheMsg = sTemp
Case 8 'string, if we recieve a string, its assumed we are linemode
#If Win32 Then
sTemp = StrConv(vMessage, vbFromUnicode)
#Else
sTemp = vMessage
#End If
Case Else
sTemp = CStr(vMessage)
#If Win32 Then
sTemp = StrConv(vMessage, vbFromUnicode)
#Else
sTemp = vMessage
#End If
End Select
TheMsg = sTemp
If UBound(TheMsg) > -1 Then
SendData = send(s, TheMsg(0), (UBound(TheMsg) - LBound(TheMsg) + 1), 0)
End If
End Function
Public Function SockAddressToString(sa As sockaddr) As String
SockAddressToString = getascip(sa.sin_addr) & ":" & ntohs(sa.sin_port)
End Function
Public Function StartWinsock(sDescription As String) As Boolean
Dim StartupData As WSADataType
If Not WSAStartedUp Then
If Not WSAStartup(&H101, StartupData) Then
WSAStartedUp = True
Debug.Print "wVersion="; StartupData.wVersion, "wHighVersion="; StartupData.wHighVersion
Debug.Print "If wVersion == 257 then everything is kewl"
Debug.Print "szDescription="; StartupData.szDescription
Debug.Print "szSystemStatus="; StartupData.szSystemStatus
Debug.Print "iMaxSockets="; StartupData.iMaxSockets, "iMaxUdpDg="; StartupData.iMaxUdpDg
sDescription = StartupData.szDescription
Else
WSAStartedUp = False
End If
End If
StartWinsock = WSAStartedUp
End Function
Public Function WSAMakeSelectReply(TheEvent%, TheError%) As Long
WSAMakeSelectReply = (TheError * &H10000) + (TheEvent And &HFFFF&)
End Function
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -