Go Back  Xtreme Visual Basic Talk > Legacy Visual Basic (VB 4/5/6) > Communications > Enumerate Computers (+IP's)


Reply
 
Thread Tools Display Modes
  #1  
Old 04-06-2007, 08:16 AM
Stalemate Stalemate is offline
Contributor

* Expert *
 
Join Date: Mar 2004
Posts: 614
Question Enumerate Computers (+IP's)


how can you enumerate the computers connected to the home network?
Reply With Quote
  #2  
Old 04-06-2007, 03:49 PM
loquin's Avatar
loquin loquin is offline
Google Hound

Retired Moderator
* Guru *
 
Join Date: Nov 2001
Location: Arizona, USA
Posts: 12,378
Default

A quick google search found this code.
__________________
Lou
"I have my standards. They may be low, but I have them!" ~ Bette Middler
"It's a book about a Spanish guy called Manual. You should read it." ~ Dilbert
"To understand recursion, you must first understand recursion." ~ unknown
Reply With Quote
  #3  
Old 04-07-2007, 05:38 PM
Cerian Knight's Avatar
Cerian Knight Cerian Knight is offline
Multi-Technologist

Super Moderator
* Expert *
 
Join Date: May 2004
Location: Michigan
Posts: 3,737
Default

This 'Enum Resources' example near the bottom of the page seems to work better:
http://allapi.mentalis.org/apilist/WNetOpenEnum.shtml
It enumerates everything. The computers are identified as 'Server...'.
Reply With Quote
  #4  
Old 04-09-2007, 05:24 AM
Stalemate Stalemate is offline
Contributor

* Expert *
 
Join Date: Mar 2004
Posts: 614
Default

hey, I appreciate your help

@Cerian Knight, that was a good example and it was easy to understand.
however, it doesn't include IP's...

@loquin, that code doesn't seem to work for me although it uses the same basic API calls.


the main thing I am interested in are the IP's available at the home net.
Reply With Quote
  #5  
Old 04-09-2007, 07:40 AM
Stalemate Stalemate is offline
Contributor

* Expert *
 
Join Date: Mar 2004
Posts: 614
Default

gethostbyname is the solution offered by msdn.
*gethostname can be replaced by any host name found using the previous method.

it seems to be working nicely, but I can't help but wonder if
there isn't a more direct method to do this (without CopyMemory)

also, msdn says here that gethostbyname is outdated and should not be used,
but I couldn't find any VB related info on the suggested alternative - getaddrinfo
Reply With Quote
  #6  
Old 04-09-2007, 10:35 AM
loquin's Avatar
loquin loquin is offline
Google Hound

Retired Moderator
* Guru *
 
Join Date: Nov 2001
Location: Arizona, USA
Posts: 12,378
Default

here's an example of pinging a server(PC) name by name and returning the IP address & ping time.

You'll have to ping each of the computer names in the names enumeration, from above.
Attached Files
File Type: zip ping.zip (3.8 KB, 37 views)
__________________
Lou
"I have my standards. They may be low, but I have them!" ~ Bette Middler
"It's a book about a Spanish guy called Manual. You should read it." ~ Dilbert
"To understand recursion, you must first understand recursion." ~ unknown
Reply With Quote
  #7  
Old 04-09-2007, 12:02 PM
Cerian Knight's Avatar
Cerian Knight Cerian Knight is offline
Multi-Technologist

Super Moderator
* Expert *
 
Join Date: May 2004
Location: Michigan
Posts: 3,737
Default

There may be some case where ping is blocked by firewall. There may be some other options here:
http://www.google.com/search?hl=en&s...isual+basic%22
Reply With Quote
  #8  
Old 04-10-2007, 01:18 PM
Stalemate Stalemate is offline
Contributor

* Expert *
 
Join Date: Mar 2004
Posts: 614
Default

ok, here's something I put together using your help
it's a very simple chat app between the pc's at the home net

some odd things occur when a user is leaving and rejoining the chat,
like he won't get messages from certain users...

I'd like to hear your thoughts on the efficiency of the code.
*improvement suggestions (such as adding private messages) are of
course welcome, but for now I'm not looking to add more features.


thank you!
Attached Files
File Type: zip My Home Chat.zip (9.1 KB, 17 views)
Reply With Quote
  #9  
Old 04-15-2007, 02:52 AM
Stalemate Stalemate is offline
Contributor

* Expert *
 
Join Date: Mar 2004
Posts: 614
Default

there's a problem with my app and i can't figure out why

for some reason, one user doesn't get messages from another user.
but the realy weird part is it's always a different user pair
my home net consists of three pc's, sometimes pc1 doesn't get messages
from pc2, sometimes pc2 doesnt get from pc3, sometimes pc1 doesn't get
from pc3

could someone please take a look at my project, and tell me what's wrong.
the modules are working fine, so it's gotta be something in the DataArrival process or the way I send the messages... (i think )
Attached Files
File Type: zip My Home Chat.zip (9.1 KB, 14 views)
Reply With Quote
  #10  
Old 04-22-2007, 04:53 PM
Cerian Knight's Avatar
Cerian Knight Cerian Knight is offline
Multi-Technologist

Super Moderator
* Expert *
 
Join Date: May 2004
Location: Michigan
Posts: 3,737
Default

Read deeply through this thread and see if your suspicions of the DataArrival process are correct: http://www.xtremevbtalk.com/showthread.php?t=281959
It seems you have no way to verify that the completed message has arrived.
Reply With Quote
  #11  
Old 04-22-2007, 10:09 PM
DougT's Avatar
DougT DougT is offline
Ultimate Antique

Administrator
* Expert *
 
Join Date: Sep 2005
Location: Maldon,Essex, UK
Posts: 3,939
Default

You may also find Post #3 here http://www.xtremevbtalk.com/showthre...ght=re-entrant of interest.
__________________
semel insanivimus omnes
S Data in context = Information, S Information in context = Knowledge, S Knowledge in context = Experience
S Experience in context = Wisdom= Data
Reply With Quote
  #12  
Old 04-28-2007, 08:32 AM
Stalemate Stalemate is offline
Contributor

* Expert *
 
Join Date: Mar 2004
Posts: 614
Default

hello, sorry for the delay.

i read both refferences and they don't seem to address my issue.
here's my DataArrival event process:
Code:
Private Sub Sock_DataArrival(Index As Integer, ByVal bytesTotal As Long) Dim sData As String ' txtStatus is a textbox i use for some notifications txtStatus.Text = txtStatus.Text & vbCrLf & CStr(Now) & ": Message received from " & _ sServerIP(Index).RemoteName & "(" & sServerIP(Index).UserName & ")" ' get the text and put in the chat window Sock(Index).GetData sData ' rtbChat is a rich text box used to display the messages rtbChat.Text = rtbChat.Text & vbCrLf & sData End Sub
here's how the message is being sent:
Code:
Private Sub cmdSend_Click() Dim i As Long ' disable cmdSend & txtOut while data is being sent txtOut.Enabled = False cmdSend.Enabled = False ' add your text to the chat window rtbChat.Text = rtbChat.Text & vbCrLf & sMyName & " > " & txtOut.Text ' run through all available servers For i = 1 To Sock.UBound ' check if connected If SockConnectEx(Sock(i)) Then ' send the text Sock(i).SendData sMyName & " > " & txtOut.Text ' notify that the message was sent txtStatus.Text = txtStatus.Text & vbCrLf & CStr(Now) & ": Message sent to " & _ sServerIP(i).RemoteName & "(" & sServerIP(i).UserName & ")" Else ' unable to connect txtStatus.Text = txtStatus.Text & vbCrLf & CStr(Now) & ": Error connecting to " & _ sServerIP(i).RemoteName & "(" & sServerIP(i).UserName & ")" End If Next i ' empty the text box txtOut.Text = "" txtOut.Enabled = True txtOut.SetFocus End Sub
what's happening is sometimes one user won't get messages from a certain user.
disconnecting some random user and reconnecting him has a weird result:
it could "fix" the situation or it might simply change the duo of users that one
of them can't recieve messages from the other.
when i say a user didn't recieve a message it means the DataArrival event didn't even fire...
plus, it doesn't always happen which makes it more difficult to understand.


what are your thoughts?

thanks for taking the time to help me.
Reply With Quote
  #13  
Old 05-09-2007, 02:57 AM
Stalemate Stalemate is offline
Contributor

* Expert *
 
Join Date: Mar 2004
Posts: 614
Default

hello?
Reply With Quote
  #14  
Old 05-09-2007, 05:02 AM
b0b b0b is offline
Contributor
 
Join Date: Jul 2003
Posts: 741
Default

What exactly are you trying to do?
Quote:
how can you enumerate the computers connected to the home network?
Are you trying to do this as some thing new to learn or do you just want a home chat program?

I've looked at your code and it looks over complicated for just a home chat. I cant even get it to run, get a subscript out of range error in Initialize_Servers.
Reply With Quote
  #15  
Old 05-09-2007, 07:27 AM
Stalemate Stalemate is offline
Contributor

* Expert *
 
Join Date: Mar 2004
Posts: 614
Default

ok, i'll try to explain:
the Initialize_Servers procedure is used to retrieve all the computers connected to the network.
(IPs and computer names)
over here it works perfectly, it gets the IPs and pc names of the 3 pc's on my home net.
i don't know why you get an error, but let's ignore that part of the code for now.

after getting the IPs the socks are loaded, and when first trying to send a message the sock makes the connection.
the connections are successful but for some reason a strange thing occurs.
the one i've described in posts #9 and #12.
and i have no clue why it is happening (or not happening sometimes...)

Quote:
Originally Posted by b0b View Post
I've looked at your code and it looks over complicated for just a home chat.
I cant even get it to run, get a subscript out of range error in Initialize_Servers.
i agree, the code is way too complicated for something that appears to be so simple.
none the less, these are the only methods i was able to find that perform these functions...
if you or anyone else has an idea on how to simplify the code, please share.

thanks!
Reply With Quote
  #16  
Old 05-09-2007, 08:06 AM
b0b b0b is offline
Contributor
 
Join Date: Jul 2003
Posts: 741
Default

Well for a simple chat you will need a server that can handle multiple connections, and some way for the clients to locate the server.

Here's one way to handle multiple connections, need one socket on the form which is a control array(set its index to 0 to make it a array). This socket keeps listening an passes connections to other sockets in the array.
Code:
Private Sub Winsock1_ConnectionRequest(Index As Integer, ByVal requestID As Long) 'see if you can re-use a socket For i = 1 To Winsock1.ubound If (Winsock1(i).State = sckClosed) Or (Wsock(i).State = sckClosing) Then With Winsock1(i) .Close .LocalPort = 0 .Accept requestID End With Exit Sub End If Next i 'cant re-use a socket then load a new one Call Load(Winsock1(Winsock1.ubound + 1)) With Winsock1(Winsock1.ubound) .Close .LocalPort = 0 .Accept requestID End With End Sub

Search the forum for more examples of multiple connections (this should work but i cut it out of a larger program and didn't test it).

To solve the problem of the clients not knowing where the server is and vice versa you can use a UDP broadcast.

UDP broadcast should be used sparingly as not to create excess network traffic.

To do a UDP broadcast simply send data to the IP address 255.255.255.255, this is a very large scale broadcast and not really neccessary for a small network so you can scale it down a bit. If the computers are all in the same subnet e.g. the IP addresses are something like 192.168.1.xxx, you can just send data to 192.168.1.255

The simpleset way to handle the broadcast would be;
The server sends out its IP address and port at regular intervals (say every 30 sec), when the clients receive it they know where the server is and try and connect.

Hope that makes sense
Reply With Quote
  #17  
Old 05-09-2007, 01:38 PM
loquin's Avatar
loquin loquin is offline
Google Hound

Retired Moderator
* Guru *
 
Join Date: Nov 2001
Location: Arizona, USA
Posts: 12,378
Default

and, as a side note. Assuming you broadcast the server's IP address every 30 seconds, what will happen the first time the server starts up? Every client will try to connect at the same time!

In the client software, have each client delay by a random amount of time before attempting to connect. That way, you won't flood the server with simultaneous connection requests. In the client main form load event, or in sub main, call randomize once to seed the pseudo-random number generator. (randomize should be called only once in the life of your application)

Then, in your app's broadcast listener, sleep for up to two seconds... (The following snippet assumes that you've declared the sleep api function)

Code:
sleep clng(random(2000)) ConnectToServer (IP_Address)
__________________
Lou
"I have my standards. They may be low, but I have them!" ~ Bette Middler
"It's a book about a Spanish guy called Manual. You should read it." ~ Dilbert
"To understand recursion, you must first understand recursion." ~ unknown
Reply With Quote
  #18  
Old 05-09-2007, 02:18 PM
Stalemate Stalemate is offline
Contributor

* Expert *
 
Join Date: Mar 2004
Posts: 614
Default

@b0b :
the first part was already implemented in my app.
now about the second part, the UDP broadcast, I understand the concept and it sounds like a good one.
but could you post some example code or specify the procedure that does this broadcast?

@loquin :
thanks for the tips

Last edited by Stalemate; 05-09-2007 at 02:23 PM.
Reply With Quote
  #19  
Old 05-09-2007, 02:50 PM
dilettante's Avatar
dilettante dilettante is offline
Underclocked lifestyle

Forum Leader
* Guru *
 
Join Date: Feb 2005
Location: Michigan, USA
Posts: 4,184
Default

A listening TCP port has a backlog queue for incoming connections to prevent loss. On the desktop OSs this is normally 5 however, so perhaps the idea of a random-length pause makes some sense.

For a small home chat application though you could just go with a peer-to-peer approach using UDP broadcasts. On a small LAN there is little overhead or downside, and broadcasts won't cross a router boundary without some advanced functionality in the router so there is no security concern.

This would seem to eliminate most of your headaches. I wouldn't try transferring files over such a scheme, since plain old file sharing would be a lot more efficient - especially in terms of your time and effort. If you really wanted to do it though a pair of peers could just open a TCP connection using a second socket for sending a file.

I've attached a tiny demo so you can see what I'm getting at.
Attached Files
File Type: zip UDPCastChat.zip (2.3 KB, 18 views)
Reply With Quote
  #20  
Old 05-09-2007, 11:09 PM
DougT's Avatar
DougT DougT is offline
Ultimate Antique

Administrator
* Expert *
 
Join Date: Sep 2005
Location: Maldon,Essex, UK
Posts: 3,939
Default

From what I can make out from the code posted earlier, the design is not 'traditional' in as much as there doesn't seem to be one server to which everyone else connects. Rather, each instance of the progam enumerates the IP addresses of every other computer in the network and listens for a connection. When something is to be sent, the program iterates through the list of IP addresses and checks if they are connected. If they are, the message is sent, if not then a connection request is made and the message sent. If the connection is unsuccessful then it moves onto the next IP Address for the machine in question. Thus, a connection to a given machine is established when the first message is sent.

On first inspection, I suspected that since there was no buffer management in the DataArrival event, messages could be lost. However, I would also have expected some messages to be corrupted since the whole of the Winsock Buffer is appended to the textbox, so if there were partial or multiple messages they would show up, and you are not reporting that as a problem. Perhaps you will when you've overcome the current problem.

I suspect that the problem has something to do with Function SockConnectEx and / or Subroutine SockListen_ConnectionRequet both of which may close a socket. If that socket was in the process of sending, the close would abort the send and the message would not be sent. Perhaps you could test the theory by judicious use of the SendComplete event and do nothing on the socket until the previous send has completed.

EDIT:b0b.. I wasn't as lucky as you. VB6 just aborted when i tried to run the app. (Something to do with ME no doubt)
__________________
semel insanivimus omnes
S Data in context = Information, S Information in context = Knowledge, S Knowledge in context = Experience
S Experience in context = Wisdom= Data

Last edited by DougT; 05-10-2007 at 12:03 AM.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->