Winsock - Get LocalIP for Internet and not LAN

Posted on 2004-10-20
Last Modified: 2013-11-13
When i call Winsock1.LocalIP it returns the ip address of my computer's ethernet card ( even though im connected to the internet.

How do i get my computers internet ip address using winsock control or any api function - NOT another Control (dont want too many dependencies) ?

How would I know which is the internet IP address.
Question by:kurian2z5
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Author Comment

ID: 12360303
i would prefer to use the winsock control
LVL 26

Expert Comment

ID: 12360572
what do you get form ipconfig /all

Author Comment

ID: 12365290
Ethernet Adapter 0 is my internet connection and it shows my internet ip address.
Ethernet Adapter 1 is my Lan card and it shows my Lan ip address.

I need it done automatically so that my program is automated. How does it know which is the internet IP address.
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

LVL 32

Expert Comment

ID: 12365810
"This article illustrates how to programmatically retrieve IP configuration information similar to the IPCONFIG.EXE utility. It demonstrates how to use the IP Helper APIs GetNetworkParams() and GetAdaptersInfo() from Visual Basic.";en-us;223025

Author Comment

ID: 12366609
this is really compicated and i dont want all this code in my program. More over how will I know which is the Internet IP address other than the fact that it is usually a PPP adapter.

Author Comment

ID: 12366612
is there any way to do this using the winsock control ?

Expert Comment

ID: 12366616
The client's applications creates socket and assigns to him the random number of TCP/IP port it will work with. But it does not happen at the server machine. The server applications, as a rule, work with the early-defined TCP/IP ports. For example, the number of TCP/IP port for FTP server is 21, and another one for Web server is 80. There is a "global arrangement" for widely used services. It defines the number of ports the clients could send their requests by default.

Protocol Port Description
SMTP    25    Simple Mail Transfer Protocol  
POP3    110    Post Office Protocol
NNTP    119    Network News Transfer Protocol
FTP      21       File Transfer Protocol
HTTP    80       Hyper Text Transfer Protocol  

The client starts the network sessions with server via one of the network protocols. The client creates socket and forces it to connect to the remote server. The socket gets the address of server and the number of port to which it has to connect to. As for Winsock control then you should invoke the Connect method. Look at the example below. It shows how to connect to FTP server.

Winsock1.Connect "", 21


Author Comment

ID: 12366632
how do i get the Internet IP address of my computer instead of the Lan ip address using this ?

Author Comment

ID: 12366655
I found out how to get the Internet IP using the winsock control.

When you just create a winsock control, the LocalIP property returns the Lan ip address of the computer.

But when you connect to any other computer on the internet using that same winsock control , LocalIP returns the IP address of the adapter through which it connected.

Private Sub Form_Load()

MsgBox Winsock1.LocalIP 'returns Lan IP address
Winsock1.RemoteHost = ""
Winsock1.RemotePort = 80

End Sub

Private Sub Winsock1_Connect()

MsgBox Winsock1.LocalIP 'returns Internet IP address

End Sub

Accepted Solution

ee_ai_construct earned 0 total points
ID: 12405448
Question answered by asker or dialog valuable.
Closed, 50 points refunded.
ee_ai_construct (replacement part #xm34)
Community Support Admin

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

724 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question