• 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.
  • Popular Now

  • Posts

    • All it does is use the CPU more efficiently during boot to speed up boot times. That's it. Yawn....
    • It's not a one or the other kind of thing. Software should run efficiently, and the operating system should appropriately manage the CPU clocks. You could have the best most optimized software on earth, and it will still run faster if the CPU does a better job of boosting as needed. All this is doing is pre-boosting the CPU based on user actions, instead of waiting for the normal detection mechanism to kick in. If the OS knows it is about to need more CPU, why shouldn't it use that knowledge? It's the same idea of downshifting before you try to pass someone, instead of just burying your foot into the peddle and waiting for the transmission to figure out what you want to do.
    • Audacity 3.7.8 by Razvan Serea Audacity is a free, open source digital audio editor and recording application. Edit your sounds using cut, copy, and paste features (with unlimited undo functionality), mix tracks, or apply effects to your recordings. The program also has a built-in amplitude-envelope editor, a customizable spectrogram mode, and a frequency-analysis window for audio-analysis applications. Built-in effects include bass boost, wah wah, and noise removal, and the program also supports VST plug-in effects. You can use Audacity to: Record live audio. Record computer playback on any Windows Vista or later machine. Convert tapes and records into digital recordings or CDs. Edit WAV, AIFF, FLAC, MP2, MP3 or Ogg Vorbis sound files. AC3, M4A/M4R (AAC), WMA and other formats supported using optional libraries. Cut, copy, splice or mix sounds together. Numerous effects including change the speed or pitch of a recording. Write your own plug-in effects with Nyquist. And more! See the complete list of features. Audacity 3.7.8 changelog: #10688 Fixed an exception thrown when pasting into a newly-created track (Thanks, David Bailes (@DavidBailes)!) #10870, #10884, #10775, #10629 Fixed tone generation, waveform-scale setting, SetClip Name parameter, and clip-boundary command names for scripting and macros (Thank you, David Bailes (@DavidBailes)!) #11106 Fixed the loading of presets for the Distortion effect (A million thanks, David Bailes (@DavidBailes)!) #10947 Fixed paste into an empty audio track not preserving the source sample rate (Thanks, Juan Gabriel Colonna (@juancolonna)!) #10776 Allowed AltGr modifier in label and clip name editing (Thanks, Davide Peressoni (@DPDmancul)!) #9938 Added options to choose where silence is truncated (start/middle/end) (Thanks, Noah Rosenfield (@nosenfield)!) #9935 Added Podcast 2.0 chapters JSON export for label tracks (Thanks, Noah Rosenfield (@nosenfield)!) #10103 Improve UI on HiDPI displays on Linux/wxGTK (Thanks, Ivan A. Melnikov (@iv-m)!) #10099 Fixed MixerBoard Mute and Solo button display (Thanks, Ivan A. Melnikov (@iv-m)!) #10681 Fixed multichannel FLAC import #10999 Fixed envelope being broken after joining clips Download: Audacity 64-bit | Standalone ~20.0 MB (Open Source) Download: Audacity 32-bit | Standalone Download: Audacity ARM64 | Standalone View: Audacity Home Page | Screenshot Get alerted to all of our Software updates on Twitter at @NeowinSoftware
    • There really isn't anything magical about the low latency profile, other OS's do this as well. All they're doing is using your CPUs boost clock options in a more smarter way.
    • So we shouldn't have the option because of people using their laptops on battery? OK? LOL
  • Recent Achievements

    • One Month Later
      Star Processing earned a badge
      One Month Later
    • Week One Done
      Star Processing earned a badge
      Week One Done
    • One Year In
      Star Processing earned a badge
      One Year In
    • Week One Done
      FBSPL earned a badge
      Week One Done
    • One Year In
      Jim Dugan earned a badge
      One Year In
  • Popular Contributors

    1. 1
      +primortal
      494
    2. 2
      PsYcHoKiLLa
      198
    3. 3
      +Edouard
      155
    4. 4
      Steven P.
      84
    5. 5
      ATLien_0
      69
  • Tell a friend

    Love Neowin? Tell a friend!