Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Winsock help? 1

Status
Not open for further replies.

BiggerBrother

Technical User
Joined
Sep 9, 2003
Messages
702
Location
GB
I have an array of the winsock controls, used to communicate with other instances of my application across a network. Obviously you have to specify the ip address and the port number before you can send any data.

What I need to be able to do is:
1. Run the server application on any PC, regardless of the name or IP address, and then communicate with all of the clients.
2. Not have to change the details on all of the client app instances when the ip of the server changes.
3. When the server starts, on any pc, I what to check if there is already an instance of the server running on the network.
4. Also, and finally, I think, I want to be able to run any instance of the client that is on the network when the server application starts.

I know this is a big wish list, but any pointers would be a great help.

BB
 
discovering which machine is the server should be quite simple. If your clients listen on a particular port the server could send a message telling the clients that it is there.

You can obtain a list of macines on the network, then send the message to each one in turn.

I did a similar thing. do a search for NetServerEnum API on the web, that is where I found my help.

Alternativly you can broadcast the information, I think it can be done using UDP but I wouldn't know too much about that part.

Hope this helps a bit.

Matt

If you can keep your head while those around you are losing theirs, you obviously haven't grasped the seriousness of the situation
 
I'm sorry, but I still cna't find what I'm after. Did a search on the above, and found a couple of articles, but not what I wanted. How can I return a list of IP addresses on my network? Once I have these, I can then connect to them using the winsock control. Is there a way to find all listening winsock controls? Sorry, but very new to the winsock control.

BB
 
ok paste the following into a form. on the form have a winsock called winsock1 and a list box called list1 and a command button called command1.

This is code that I found then played around with, I cant remember where I got it from, but it was a while ago. This was my test app to get the names.

Matt
========================================

Option Explicit

Private Const MAX_PREFERRED_LENGTH As Long = -1
Private Const NERR_SUCCESS As Long = 0&
Private Const ERROR_MORE_DATA As Long = 234&
Private Const SV_TYPE_WORKSTATION As Long = &H1
Private Const SV_TYPE_SERVER As Long = &H2
Private Const SV_TYPE_SQLSERVER As Long = &H4
Private Const SV_TYPE_DOMAIN_CTRL As Long = &H8
Private Const SV_TYPE_DOMAIN_BAKCTRL As Long = &H10
Private Const SV_TYPE_TIME_SOURCE As Long = &H20
Private Const SV_TYPE_AFP As Long = &H40
Private Const SV_TYPE_NOVELL As Long = &H80
Private Const SV_TYPE_DOMAIN_MEMBER As Long = &H100
Private Const SV_TYPE_PRINTQ_SERVER As Long = &H200
Private Const SV_TYPE_DIALIN_SERVER As Long = &H400
Private Const SV_TYPE_XENIX_SERVER As Long = &H800
Private Const SV_TYPE_SERVER_UNIX As Long = SV_TYPE_XENIX_SERVER
Private Const SV_TYPE_NT As Long = &H1000
Private Const SV_TYPE_WFW As Long = &H2000
Private Const SV_TYPE_SERVER_MFPN As Long = &H4000
Private Const SV_TYPE_SERVER_NT As Long = &H8000
Private Const SV_TYPE_POTENTIAL_BROWSER As Long = &H10000
Private Const SV_TYPE_BACKUP_BROWSER As Long = &H20000
Private Const SV_TYPE_MASTER_BROWSER As Long = &H40000
Private Const SV_TYPE_DOMAIN_MASTER As Long = &H80000
Private Const SV_TYPE_SERVER_OSF As Long = &H100000
Private Const SV_TYPE_SERVER_VMS As Long = &H200000
Private Const SV_TYPE_WINDOWS As Long = &H400000 'Windows95 and above
Private Const SV_TYPE_DFS As Long = &H800000 'Root of a DFS tree
Private Const SV_TYPE_CLUSTER_NT As Long = &H1000000 'NT Cluster
Private Const SV_TYPE_TERMINALSERVER As Long = &H2000000 'Terminal Server
Private Const SV_TYPE_DCE As Long = &H10000000 'IBM DSS
Private Const SV_TYPE_ALTERNATE_XPORT As Long = &H20000000 'rtn alternate transport
Private Const SV_TYPE_LOCAL_LIST_ONLY As Long = &H40000000 'rtn local only
Private Const SV_TYPE_DOMAIN_ENUM As Long = &H80000000
Private Const SV_TYPE_ALL As Long = &HFFFFFFFF
Private Const SV_PLATFORM_ID_OS2 As Long = 400
Private Const SV_PLATFORM_ID_NT As Long = 500
Private Const MAJOR_VERSION_MASK As Long = &HF

Private Type SERVER_INFO_100
sv100_platform_id As Long
sv100_name As Long
End Type

Private Declare Function NetServerEnum Lib "netapi32" _
(ByVal servername As Long, _
ByVal level As Long, _
buf As Any, _
ByVal prefmaxlen As Long, _
entriesread As Long, _
totalentries As Long, _
ByVal servertype As Long, _
ByVal domain As Long, _
resume_handle As Long) As Long

Private Declare Function NetApiBufferFree Lib "netapi32" _
(ByVal Buffer As Long) As Long

Private Declare Sub CopyMemory Lib "kernel32" _
Alias "RtlMoveMemory" _
(pTo As Any, uFrom As Any, _
ByVal lSize As Long)

Private Declare Function lstrlenW Lib "kernel32" _
(ByVal lpString As Long) As Long

Private Declare Function gethostbyname Lib "WSOCK32" (ByVal szHost As String) As Long

Private Sub Command1_Click()
GetServers vbNullString
End Sub


Private Function GetServers(sDomain As String) As Long

'lists all servers of the specified type
'that are visible in a domain.

Dim bufptr As Long
Dim dwEntriesread As Long
Dim dwTotalentries As Long
Dim dwResumehandle As Long
Dim se100 As SERVER_INFO_100
Dim success As Long
Dim nStructSize As Long
Dim cnt As Long

nStructSize = LenB(se100)

'Call passing MAX_PREFERRED_LENGTH to have the
'API allocate required memory for the return values.
'
'The call is enumerating all machines on the
'network (SV_TYPE_ALL); however, by Or'ing
'specific bit masks for defined types you can
'customize the returned data. For example, a
'value of 0x00000003 combines the bit masks for
'SV_TYPE_WORKSTATION (0x00000001) and
'SV_TYPE_SERVER (0x00000002).
'
'dwServerName must be Null. The level parameter
'(100 here) specifies the data structure being
'used (in this case a SERVER_INFO_100 structure).
'
'The domain member is passed as Null, indicating
'machines on the primary domain are to be retrieved.
'If you decide to use this member, pass
'StrPtr("domain name"), not the string itself.
success = NetServerEnum(0&, _
100, _
bufptr, _
MAX_PREFERRED_LENGTH, _
dwEntriesread, _
dwTotalentries, _
SV_TYPE_ALL, _
0&, _
dwResumehandle)

'if all goes well
If success = NERR_SUCCESS And _
success <> ERROR_MORE_DATA Then

'loop through the returned data, adding each
'machine to the list
For cnt = 0 To dwEntriesread - 1

'get one chunk of data and cast
'into an SERVER_INFO_100 struct
'in order to add the name to a list
CopyMemory se100, ByVal bufptr + (nStructSize * cnt), nStructSize

List1.AddItem GetPointerToByteStringW(se100.sv100_name) & &quot;,&quot; & GetIPFromHostName(GetPointerToByteStringW(se100.sv100_name))
Debug.Print List1.List(cnt)
Next

End If

'clean up regardless of success
Call NetApiBufferFree(bufptr)

'return entries as sign of success
GetServers = dwEntriesread

End Function


Public Function GetPointerToByteStringW(ByVal dwData As Long) As String

Dim tmp() As Byte
Dim tmplen As Long

If dwData <> 0 Then

tmplen = lstrlenW(dwData) * 2

If tmplen <> 0 Then

ReDim tmp(0 To (tmplen - 1)) As Byte
CopyMemory tmp(0), ByVal dwData, tmplen
GetPointerToByteStringW = tmp

End If

End If

End Function

Public Function GetIPFromHostName(ByVal sHostName As String) As String

'converts a host name to an IP address.

Dim nbytes As Long
Dim ptrHosent As Long 'address of hostent structure
Dim ptrName As Long 'address of name pointer
Dim ptrAddress As Long 'address of address pointer
Dim ptrIPAddress As Long
Dim sAddress As String

sAddress = Space$(4)

ptrHosent = gethostbyname(sHostName & vbNullChar)

If ptrHosent <> 0 Then

'assign pointer addresses and offset

'ptrName is the official name of the host (PC).
'If using the DNS or similar resolution system,
'it is the Fully Qualified Domain Name (FQDN)
'that caused the server to return a reply.
'If using a local hosts file, it is the first
'entry after the IP address.
ptrName = ptrHosent

'Null-terminated list of addresses for the host.
'The Address is offset 12 bytes from the start of
'the HOSENT structure. Note: Here we are retrieving
'only the first address returned. To return more than
'one, define sAddress as a string array and loop through
'the 4-byte ptrIPAddress members returned. The last
'item is a terminating null. All addresses are returned
'in network byte order.
ptrAddress = ptrHosent + 12

'get the IP address
CopyMemory ptrName, ByVal ptrName, 4
CopyMemory ptrAddress, ByVal ptrAddress, 4
CopyMemory ptrIPAddress, ByVal ptrAddress, 4
CopyMemory ByVal sAddress, ByVal ptrIPAddress, 4

GetIPFromHostName = IPToText(sAddress)

End If

End Function

Private Function IPToText(ByVal IPAddress As String) As String

IPToText = CStr(Asc(IPAddress)) & &quot;.&quot; & _
CStr(Asc(Mid$(IPAddress, 2, 1))) & &quot;.&quot; & _
CStr(Asc(Mid$(IPAddress, 3, 1))) & &quot;.&quot; & _
CStr(Asc(Mid$(IPAddress, 4, 1)))

End Function





If you can keep your head while those around you are losing theirs, you obviously haven't grasped the seriousness of the situation
 
That is exactly what I was after. Thank you.

*

BB
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top