Link to home
Start Free TrialLog in
Avatar of klopex
klopex

asked on

Getting Local IP Address

How do I get my local IP address in VB?  

I work in a school district where we have Win95 - WinXP machines.  I need code that will work on all.  I want to write it in VB6 so it will work on the Win95 machines.  If the code is different for various versions of Windows, then please provide code to handle that.

What I am trying to do is build an a client-server pair of applications.  The client runs at boot on machines in our network.  It will send the Computer name and IP address to the server software and then exit.  If the server is not found then exit.  The server will replace records in a database when they are updated.  We can then look at the list from the server to know what IP address to type in when we use VNC (a GNU-freeware type of PCAnywhere).

If you can help me with the whole project, I will increase the points.  So far, I have the ability to get the computer name.  I'm just getting started.

Thanks!
Avatar of lbertacco
lbertacco

Why do you need to know/send the ip address? The server software, when it will receive a packet from the client, will also know the IP address of the sender (all IP packets contain the IP address of both sender and recipient).
Anyway the problem in retrieving the local IP address is that in general it depends on the destination address. That's because you usually have more than one interface (at least the local one 127.0.0.1 and an external one, and possibly others). So you should enumerate all local IPs and determine which one is used when connecting to a given destination. So, it's better to avoid this problem at all if what I wrote in the previous comment solves your problem.
ASKER CERTIFIED SOLUTION
Avatar of graye
graye
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Just out of curiosity, you said you want to connect to these computers through VNC is it?

if yes, VNC can connect by name also, so if you need to connect to pc1, you can just type pc1 and connect, you do not necessarily need an IP address.

-Dee
deemehtani, probably he is using dhcp to assign IP to machines (otherwise he could build the name->ip table statically). In this case he will be able to specify directly hostnames (in VNC ) only if he has a properly configured DynamicDNS server.
Hi lbertacco,
to get the local IP u can either fire a ipconfig/winipconfig and view the output. just remember in case u are using win95 machines the winsock is different than in 98/2K and 95

Regards,
Bhagyesh
Hi bhagyesht,
Yes I know that. However Klopex wanted to do it from VB. And ofcourse also ipconfig/winipconfig reports one or more IPs per interface, so just looking at IPs you won't know what IP will be used when contacting a remote machine. You also need to take into considertion the netmasks and routing tables. I think this is just nonsense in this context.
Hi lbertacco,
Sorry the comment earlier was intended for klopex.

Regards,
Bhagyesh
Hi klopex,
a simple way is to use the winsock control.
Winsock1.LocalIP gives the local ip. this should solve ur problem.

Regards,
Bhagyesh
Avatar of klopex

ASKER

Thank you all for your responses.  I will probably work on this some more on Thursday, so be patient with me.  We do use DHCP.  Some of our technicians have tested VNC and they were saying that sthe user will have to put their mouse ofer the tray icon and then tell us their IP so we can log into their machine.  That's not reasonable because there are numerous schools with numerous classrooms with at least 2 computers in each.  No telephones.  I might as well drive to the school as to expect people to run and grap IP addresses for me.  With this tool, I will be able to have a list with names.  If VNC can give me windows names over a router, then this program is not needed.  I have not tested VNC yet.

Anyhow, thanks again for the help.

Yes, I think you are right that the server can just glean the IP address from the packet.  I thought of that after looking for IP information from VB.  That is probably the better way to get the right interface.  These machines only have one NIC.

 If anyone has a simple VB app that provides the ability to pass a string from machine A to machine B, That would save me a lot of work.
I'm confused by that last statement...   do you really need to know the IP address of a remote PC?
Avatar of klopex

ASKER

I am still interested to see how to do it in VB.  However, It is probably wiser to just glean it from the packet when it is sent to the server.  I will experiment with the solutions provided this Thursday.  If anyone can give me code for the client server relationship, I will increase the points to reward them.  I will reward the existing 250 points to the one(s) who answer the original question.  Sorry for the confusion!!

Happy once de mayo!
The next step in the evolution of a programmer is to dabble in a true Client/Server application.  It's really not that difficult, but it does seem to be a "right of passage" for most programmers.   Some programmers get left behind... others can make it through.  (OK, OK.. I'm getting a wee bit melodramatic, but you get the point)

If you're interesting in doing a true "client server" application, I'd seriously consider moving up to .Net   The folks at Microsoft have made it sooo much easier to do with .Net (even VB.Net).

Examples are gonna be hard to come by, since they are either trivial or way too big to post here.  There are some pretty good examples with the VB.Net (MSDN library) documentation.  I'd also consider the Wrox book "Professional VB.NET 2003" (if you're going with VB)
Avatar of klopex

ASKER

Thank You for the help!  I am sorry it took me so long to return to the forum.  On the thursday that I said I would be able to work on it, I had a Hard Drive Hardware failure.  Bought a new one from Dell and found that they are much more interested in getting parts to people under warranty than getting parts to people who purchase them! (My warranty has expired).  I have reinstalled everything and now I am ready to work.

I was informed by the team working on the VNC rollout that they finally got VNC to connect by computer names across the WAN.  This program is not necessary now, but thanks for teaching me!

klopex