USING WINSOCK - CODE EXAMPLE BY BRENT THORN

Home

API File Search
Filedialogs
The FILES statement
The FILES statement in action
Open Source Editor for WinXP
Spotlight on John Fisher
Using Winsock
Winsock API Reference

- USED BY PERMISSION
- FOR LIBERTY BASIC 3

' IP Getter using Winsock
     ' Based on code at http://www.vbapi.com/ref/g/gethostbyname.html.
     ' By Brent Thorn, March 2002
     ' mailto:lbgui@aol.com
 Titlebar "IP Getter"
 Open "Kernel32" For DLL As #kernel
     Open "WSock32" For DLL As #wsock
 Domain$ = "www.yahoo.com"
     Prompt "Enter an Internet domain."; Domain$
 If Domain$ <> "" Then
     Struct sockinfo, _
     wVersion As word, _
     wHighVersion As word, _
     szDescription As char[257], _
     szSystemStatus As char[129], _
     iMaxSockets As long, _
     iMaxUdpDg As long, _
     lpVenderInfo As long
     Struct hostinfo, _
     hname As long, _
     haliases As long, _
     haddrtype As word, _
     hlength As word, _
     haddrlist As long
     Struct sIPAddress, _
     number As ulong
 ' Open up a Winsock session, using version 2.2.
     wVersionRequested = MAKEWORD(2, 2)
     CallDLL #wsock, "WSAStartup",_
     wVersionRequested As word,_
     sockinfo As struct, result As long
 If retval <> 0 Then
     Print "ERROR: Attempt to open Winsock failed: error"; retval
     GoTo [End]
     End If
 ' Get information about the domain specified in Domain$.
     pHostInfo = gethostbyname(Domain$)
 If pHostInfo = 0 Then
     Print "Unable to resolve domain name."
     Else
     ' Copy the data into a HOSTENT structure.
     length = Len(hostinfo.struct)
     CallDLL #kernel, "RtlMoveMemory",_
     hostinfo As struct, pHostInfo As ulong,_
     length As ulong, result As void
 If hostinfo.haddrtype.struct <> 2 Then
     Print "A non-IP address was returned."
     Else
     ' Copy the pointer to the first (and probably only)
     ' IP address in the structure.
     source = hostinfo.haddrlist.struct
     CallDLL #kernel, "RtlMoveMemory",_
     sIPAddress As struct, source As ulong,_
     4 As ulong, result As void
 ' Copy the actual IP address.
     source = sIPAddress.number.struct
     CallDLL #kernel, "RtlMoveMemory",_
     sIPAddress As struct, source As ulong,_
     4 As ulong, result As void
 ' Convert the IP address into a human-readable string.
     ipAddress = sIPAddress.number.struct
     pIPString = inet.ntoa(ipAddress)
 ' Copy the result into a string variable.
     ipString$ = WinString(pIPString)
 ' Print the result: a human-readable IP address.
     Print ipString$
     End If
     End If
 retval = WSACleanup()
     End If
[End]
     Close #kernel
     Close #wsock
 End
     '*** Winsock Wrapper Functions ***
Function WSACleanup()
     CallDLL #wsock, "WSACleanup",_
     WSACleanup As long
     End Function
Function gethostbyname( name$ )
     CallDLL #wsock, "gethostbyname",_
     name$ As ptr, gethostbyname As long
     End Function
Function inet.ntoa( inaddr )
     CallDLL #wsock, "inet_ntoa",_
     inaddr As long, inet.ntoa As long
     End Function
'*** General-purpose Routines ***
'Make a word from two bytes (0 to 255).
     Function MAKEWORD( bLow, bHigh )
     MAKEWORD = bLow + 256 * bHigh
     End Function

Home

API File Search
Filedialogs
The FILES statement
The FILES statement in action
Open Source Editor for WinXP
Spotlight on John Fisher
Using Winsock
Winsock API Reference