• 0

External IP address


Question

Hello,

I'm using c# .net and I would like to know how can I get my external IP. I was looking over the Internet and couldn't find a solution :wacko: Maybe somebody here knows how to do it? I can also try to convert a VB .NET code so if you know how to do it with VB that's OK.

Thanks in advance for your answers :) I really appreciate this.

Link to comment
https://www.neowin.net/forum/topic/225737-external-ip-address/
Share on other sites

Recommended Posts

  • 0

I've found this code, but I can't make it work :( Can someone tell me how to use it with c#?

Here it is:

// Before calling AddIPAddress we use GetIpAddrTable to get
// an adapter to which we can add the IP.
PMIB_IPADDRTABLE pIPAddrTable;
DWORD dwSize = 0;

pIPAddrTable = (MIB_IPADDRTABLE*) malloc( sizeof( MIB_IPADDRTABLE) );

// Make an initial call to GetIpAddrTable to get the
// necessary size into the dwSize variable
if (GetIpAddrTable(pIPAddrTable, &dwSize, 0) == ERROR_INSUFFICIENT_BUFFER) {
  GlobalFree( pIPAddrTable );
  pIPAddrTable = (MIB_IPADDRTABLE *) malloc ( dwSize );
}

// Make a second call to GetIpAddrTable to get the
// actual data we want
if ( (dwRetVal = GetIpAddrTable( pIPAddrTable, &dwSize, 0 )) == NO_ERROR ) { 
  printf("\tAddress: %ld\n", pIPAddrTable->table[0].dwAddr);
}

It is from MSDN site.

  • 0
u could make a HTTP file downloaded from www.whatismyip.com!!!

Then u can check the HTML code for the IP address...

Simple :p lol

Is it legal ? And even if it is - it will take a long time for dial up modem users to load it. I want a faster way to get the IP address - even if your'e not connected or if you have a dial up modem.

BTW - check this: http://dotnet247.com/247reference/msgs/10/53244.aspx. I think I need something similar.

Edited by yyy
  • 0

Well, I've figured out how to do it in C# :laugh: (I use the code that I've found in the site)

HOWEVER, I know only how to use it as a console application. I want to use it inside a form. Can you tell me how I can use this code inside a form so it will show the text in a label ?

Here is the code as a console application:

using System;
using System.Net;
using System.Runtime.InteropServices;

struct MIB_IPADDRROW
{
public int dwAddr;
public int dwIndex;
public int dwMask;
public int dwBCastAddr;
public int dwReasmSize;
public short unused1;
public short unused2;
}

class C
{
[DllImport("iphlpapi.dll")]
static extern int GetIpAddrTable(
IntPtr pIpAddrTable,
ref int pdwSize,
bool bOrder);

static string IPToString(int ipaddr)
{
return String.Format( "{0}.{1}.{2}.{3}",
(ipaddr >> 24) & 0xFF, (ipaddr >> 16) & 0xFF,
(ipaddr >> 8) & 0xFF, ipaddr & 0xFF);
}

static void Main()
{
IntPtr pBuf = IntPtr.Zero;
int nBufSize = 0;

GetIpAddrTable( IntPtr.Zero, ref nBufSize, false );
try {
pBuf = Marshal.AllocHGlobal( nBufSize );
int r = GetIpAddrTable( pBuf, ref nBufSize, false );
if ( r != 0 )
throw new System.ComponentModel.Win32Exception( r );

#if USE_UNSAFE_CODE

unsafe {
int nNumRows = *((int*)(void*)pBuf);

MIB_IPADDRROW* pRow =
(MIB_IPADDRROW*)(void*)(IntPtr)((int)pBuf + sizeof(int));
while ( nNumRows-- > 0 ) {
Console.WriteLine( IPToString( IPAddress.NetworkToHostOrder(
pRow->dwAddr ) ) );
pRow++;
}
}


}
catch (Exception ex) {
Console.WriteLine( ex.Message );
}
finally {
if ( pBuf != IntPtr.Zero )
Marshal.FreeHGlobal( pBuf );
}
}

}

Please tell me how to use it in a form.

  • 0
Is it legal ? And even if it is - it will take a long time for dial up modem users to load it. I want a faster way to get the IP address - even if your'e not connected or if you have a dial up modem.

BTW - check this: http://dotnet247.com/247reference/msgs/10/53244.aspx. I think I need something similar.

Legal depends on whether whatismyip.com allow you to do it. It wouldn't take very long for dial-up users anyway.

If you're not connected to the Internet then you do not have an external IP address (or you'll have one in the 169 range which is useless anyway.) Getting the IP address from the computer itself isn't a good idea anyway since a lot of people use devices like routers where the computer is not assigned the external IP address as it is assigned to the router which creates a network between itself and the PC on an internal range of IPs.

  • 0
Legal depends on whether whatismyip.com allow you to do it. It wouldn't take very long for dial-up users anyway.

If you're not connected to the Internet then you do not have an external IP address (or you'll have one in the 169 range which is useless anyway.) Getting the IP address from the computer itself isn't a good idea anyway since a lot of people use devices like routers where the computer is not assigned the external IP address as it is assigned to the router which creates a network between itself and the PC on an internal range of IPs.

I once used http://checkip.dyndns.org/ to do this (I thought it is legal) and a dial up user of my program told me that it take him about a minute (which is a lot) to load the program. Finally I asked the site's webmaster if it is legal and he said it isn't.

But look at the code above - can't this code get the external IP always ? I think it does.

  • 0

Requirements (from MSDN):

Client: Requires Windows XP, Windows 2000 Professional, Windows NT Workstation 4.0 SP4 and later, Windows Me, or Windows 98.

Server: Requires Windows Server 2003, Windows 2000 Server, or Windows NT Server 4.0 SP4 and later.

So it won't work on win95. What a shame ;)

  • 0
Requirements (from MSDN):

Client: Requires Windows XP, Windows 2000 Professional, Windows NT Workstation 4.0 SP4 and later, Windows Me, or Windows 98.

Server: Requires Windows Server 2003, Windows 2000 Server, or Windows NT Server 4.0 SP4 and later.

So it won't work on win95. What a shame ;)

What won't work ? The code above? I don't mind. It is being done as a plug in for a program that doesn't work on Win 95 anyway :) So I don't mind.

But plese - I want to show the information inside a label - how can I do it?

BTW, the code was written by:

Mattias Sj?gren

[email protected]

http://www.msjogren.net

Edited by yyy
  • 0
Is it legal ? And even if it is - it will take a long time for dial up modem users to load it. I want a faster way to get the IP address - even if your'e not connected or if you have a dial up modem.

BTW - check this: http://dotnet247.com/247reference/msgs/10/53244.aspx. I think I need something similar.

How can you have an external IP address if you're not connected to the Internet? The only way that I know of to get your public IP address is to ask another machine on the Internet for your IP address. You won't be able to obtain it from some C++ or C# function that I'm aware of!

jafo

  • 0
How can you have an external IP address if you're not connected to the Internet?
Yes I know about this now.
The only way that I know of to get your public IP address is to ask another machine on the Internet for your IP address.  You won't be able to obtain it from some C++ or C# function that I'm aware of!

Oh really? :( Too bad you didn't tell me sooner. So I'll probably have to use a site. I saw once that there's a way to get the external IP but I didn't understand it.

Thanks anyway to everyone. If someone still finds a code soon I'll like to see it.

  • 0
<%@ Page Language="C#" %>
<%@ Import Namespace="System.IO" %>

<%

string ClientIP;
StreamWriter objStreamWriter;


ClientIP = Request.UserHostAddress.ToString();

IP.Text = ClientIP;


objStreamWriter = File.AppendText( MapPath( "beta_ip.txt" ) );
objStreamWriter.WriteLine( (ClientIP) );
objStreamWriter.Close();


%>

<html>
<body>


<form runat="server">
  <p>
<asp:Label id="IP" runat="server" />
  </p>
</form>
</body>
</html>

  • 0
<%@ Page Language="C#" %>
<%@ Import Namespace="System.IO" %>

<%

string ClientIP;
StreamWriter objStreamWriter;


ClientIP = Request.UserHostAddress.ToString();

IP.Text = ClientIP;


objStreamWriter = File.AppendText( MapPath( "beta_ip.txt" ) );
objStreamWriter.WriteLine( (ClientIP) );
objStreamWriter.Close();


%>

<html>
<body>


<form runat="server">
 ?<p>
<asp:Label id="IP" runat="server" />
 ?</p>
</form>
</body>
</html>

Thanks for the code:):) But I don't have a site. I need it for my application. Thanks anyway.

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • Wow, imagine you dump hundreds of hours into completing things and unlocking stuff and you lose it all. Back in the day when cheats were built into games, you could at least unlock things again that way without spending hundreds of hours again. But those days are long gone for some reason as no one builds cheats into games anymore. So it's even more painful that studio that's on its 6th installment **** it up so badly.
    • Spotify finally removes the disco ball app icon in the latest update by Ivan Jenic Image: Spotify Spotify has just released an update that removes its now infamous disco ball icon. The update reverts the app icon to the familiar flat green logo after weeks of mixed reactions online. The icon arrived on May 13 as part of the company's 20th anniversary celebration and was always intended to be temporary, though Spotify only confirmed that after the backlash started. The disco ball took the internet by storm, as the reception was split. A vocal group of users called it ugly and disorienting, with some iOS users noting that the 3D glowing effect made the app look like it was stuck mid-update. On the other end, the icon picked up a following of its own. Its retro, three-dimensional look immediately stood out against the flat, minimalist aesthetic that has dominated app design for years. It even started a small movement, spawning what people started calling "discomorphism," a mashup of disco and skeuomorphism. Other brands started posting disco ball versions of their own logos, probably in an effort to ride the wave of memes that flooded the internet during late May. Spotify has had a turbulent relationship with its user base lately. Besides the disco ball icon, which certainly wasn't appreciated by everyone, the company has also received backlash for its willingness to include AI-generated music on its platform. On May 17, Spotify promised the old icon would return “in a few weeks.” And now it looks like that time has finally arrived. So, whether you liked the disco ball or it made you uncomfortable, it’s now gone for good. The next time you update the Spotify app on your phone, the old, flat-design icon will return.
  • Recent Achievements

    • One Year In
      slackerzz earned a badge
      One Year In
    • One Year In
      highriskpaym earned a badge
      One Year In
    • One Month Later
      highriskpaym earned a badge
      One Month Later
    • Week One Done
      highriskpaym earned a badge
      Week One Done
    • Week One Done
      FBSPL earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      520
    2. 2
      PsYcHoKiLLa
      197
    3. 3
      +Edouard
      157
    4. 4
      Steven P.
      84
    5. 5
      ATLien_0
      75
  • Tell a friend

    Love Neowin? Tell a friend!