怎样得到自己机子在广域网的IP地址?

圣骑士 2005-08-03 07:21:00
用Winsock1控件的属性只能得到类似10.0.0.3或者192.168.0.3之类的内网地址,如何得到在广域网的地址阿。
...全文
1980 22 打赏 收藏 转发到动态 举报
写回复
用AI写文章
22 条回复
切换为时间正序
请发表友善的回复…
发表回复
圣骑士 2005-08-26
  • 打赏
  • 举报
回复
看来只能通过访问网站了,哎,谢谢大家
daisy8675 2005-08-14
  • 打赏
  • 举报
回复
to jxgzay(jxgzay):

202.96.134.133是深圳的DNS,这不是局域网的IP

寒一个,基本知识不牢固啊

看来你是在深圳了~~~~
daisy8675 2005-08-14
  • 打赏
  • 举报
回复
Obtaining a Machine's Public IP Address from Behind a Router
==================
Private Const ERROR_SUCCESS As Long = 0
Private Const MAX_ADAPTER_NAME_LENGTH As Long = 256
Private Const MAX_ADAPTER_DESCRIPTION_LENGTH As Long = 128
Private Const MAX_ADAPTER_ADDRESS_LENGTH As Long = 8

Private Type IP_ADDRESS_STRING
IpAddr(0 To 15) As Byte
End Type

Private Type IP_MASK_STRING
IpMask(0 To 15) As Byte
End Type

Private Type IP_ADDR_STRING
dwNext As Long
IpAddress As IP_ADDRESS_STRING
IpMask As IP_MASK_STRING
dwContext As Long
End Type

Private Type IP_ADAPTER_INFO
dwNext As Long
ComboIndex As Long 'reserved
sAdapterName(0 To (MAX_ADAPTER_NAME_LENGTH + 3)) As Byte
sDescription(0 To (MAX_ADAPTER_DESCRIPTION_LENGTH + 3)) As Byte
dwAddressLength As Long
sIPAddress(0 To (MAX_ADAPTER_ADDRESS_LENGTH - 1)) As Byte
dwIndex As Long
uType As Long
uDhcpEnabled As Long
CurrentIpAddress As Long
IpAddressList As IP_ADDR_STRING
GatewayList As IP_ADDR_STRING
DhcpServer As IP_ADDR_STRING
bHaveWins As Long
PrimaryWinsServer As IP_ADDR_STRING
SecondaryWinsServer As IP_ADDR_STRING
LeaseObtained As Long
LeaseExpires As Long
End Type

Private Declare Function GetAdaptersInfo Lib "iphlpapi.dll" _
(pTcpTable As Any, _
pdwSize As Long) As Long

Private Declare Sub CopyMemory Lib "kernel32" _
Alias "RtlMoveMemory" _
(dst As Any, _
src As Any, _
ByVal bcount As Long)

Private Declare Function URLDownloadToFile Lib "urlmon" _
Alias "URLDownloadToFileA" _
(ByVal pCaller As Long, _
ByVal szURL As String, _
ByVal szFileName As String, _
ByVal dwReserved As Long, _
ByVal lpfnCB As Long) As Long

Private Declare Function DeleteUrlCacheEntry Lib "Wininet.dll" _
Alias "DeleteUrlCacheEntryA" _
(ByVal lpszUrlName As String) As Long

Private Declare Function lstrlenW Lib "kernel32" _
(ByVal lpString As Long) As Long



Private Sub Form_Load()

Command1.Caption = "Get Public IP"

Text1.Text = LocalIPAddress()
Text2.Text = ""

End Sub


Private Sub Command1_Click()

Text2.Text = GetPublicIP()

End Sub


Private Function GetPublicIP()

Dim sSourceUrl As String
Dim sLocalFile As String
Dim hfile As Long
Dim buff As String
Dim pos1 As Long
Dim pos2 As Long

'site returning IP address
sSourceUrl = "http://vbnet.mvps.org/resources/tools/getpublicip.shtml"
sLocalFile = "c:\ip.txt"

'ensure this file does not exist in the cache
Call DeleteUrlCacheEntry(sSourceUrl)

'download the public IP file,
'read into a buffer and delete
If DownloadFile(sSourceUrl, sLocalFile) Then

hfile = FreeFile
Open sLocalFile For Input As #hfile
buff = Input$(LOF(hfile), hfile)
Close #hfile


'look for the IP line
pos1 = InStr(buff, "var ip =")

'if found,
If pos1 Then

'get position of first and last single
'quotes around address (e.g. '11.22.33.44')
pos1 = InStr(pos1 + 1, buff, "'", vbTextCompare) + 1
pos2 = InStr(pos1 + 1, buff, "'", vbTextCompare) '- 1

'return the IP address
GetPublicIP = Mid$(buff, pos1, pos2 - pos1)

Else

GetPublicIP = "(unable to parse IP)"

End If 'pos1

Kill sLocalFile

Else

GetPublicIP = "(unable to access shtml page)"

End If 'DownloadFile

End Function


Private Function DownloadFile(ByVal sURL As String, _
ByVal sLocalFile As String) As Boolean

DownloadFile = URLDownloadToFile(0, sURL, sLocalFile, 0, 0) = ERROR_SUCCESS

End Function


Private Function LocalIPAddress() As String

Dim cbRequired As Long
Dim buff() As Byte
Dim ptr1 As Long
Dim sIPAddr As String
Dim Adapter As IP_ADAPTER_INFO

Call GetAdaptersInfo(ByVal 0&, cbRequired)

If cbRequired > 0 Then

ReDim buff(0 To cbRequired - 1) As Byte

If GetAdaptersInfo(buff(0), cbRequired) = ERROR_SUCCESS Then

'get a pointer to the data stored in buff()
ptr1 = VarPtr(buff(0))

Do While (ptr1 <> 0)

'copy the data from the pointer to the
'first adapter into the IP_ADAPTER_INFO type
CopyMemory Adapter, ByVal ptr1, LenB(Adapter)

With Adapter

'the DHCP IP address is in the
'IpAddress.IpAddr member

sIPAddr = TrimNull(StrConv(.IpAddressList.IpAddress.IpAddr, vbUnicode))

If Len(sIPAddr) > 0 Then Exit Do

ptr1 = .dwNext

End With 'With Adapter

'ptr1 is 0 when (no more adapters)
Loop 'Do While (ptr1 <> 0)

End If 'If GetAdaptersInfo
End If 'If cbRequired > 0

'return any string found
LocalIPAddress = sIPAddr

End Function


Private Function TrimNull(startstr As String) As String

TrimNull = Left$(startstr, lstrlenW(StrPtr(startstr)))

End Function

jxgzay 2005-08-14
  • 打赏
  • 举报
回复
Private Sub Command1_Click()
'引用excel9.0,一个commandBox,两个TextBox,一个winsocks

Dim xlsapp As New Excel.Application
Dim xlsbook As Excel.Workbook
Dim xlssheet As Excel.Worksheet
xlsapp.DisplayAlerts = False

Set xlsbook = xlsapp.Workbooks.Open("http://dheart.51.net/ip/")
Set xlssheet = xlsbook.Worksheets(1)
Text1.Text = "本地IP:" & Winsock1.LocalIP
Text2.Text = "广域网IP:" & xlssheet.Cells(7, 1)
Set xlssheet = Nothing
Set xslbook = Nothing
Set xlsapp = Nothing

End Sub

Private Sub Form_Load()
Command1.Caption = "GetPublicIP"
End Sub
jxgzay 2005-08-14
  • 打赏
  • 举报
回复
Private Sub Command1_Click()
'引用excel9.0,一个commandBox,两个TextBox,一个winsocks

Dim xlsapp As New Excel.Application
Dim xlsbook As Excel.Workbook
Dim xlssheet As Excel.Worksheet
Set xlsbook = xlsapp.Workbooks.Open("http://dheart.51.net/ip/")
Set xlssheet = xlsbook.Worksheets(1)
xlsapp.DisplayAlerts = False
Text1.Text = Winsock1.LocalIP
Text2.Text = xlssheet.Cells(7, 1)
Set xlssheet = Nothing
Set xslbook = Nothing
Set xlsapp = Nothing

End Sub

Private Sub Form_Load()
Command1.Caption = "GetPublicIP"
End Sub

代码已测试,但因为读网页,要等待十秒以上。

DooDu 2005-08-14
  • 打赏
  • 举报
回复
访问的是

http://vbnet.mvps.org/resources/tools/getpublicip.shtml

不还是那个办法么
province_ 2005-08-14
  • 打赏
  • 举报
回复
他还不是需要从被访问机获得自己的外网IP,所以我没说错,单纯靠自己是不可以的。
jxgzay 2005-08-14
  • 打赏
  • 举报
回复
to daisy8675(莫依):
打错字了,更正如下:

你的代码可正确获取PublicIP,名符其实的好斑竹,双星斑竹,佩服。
我基础知识是不牢,谢谢指出。

202.96.134.133 依然是DNS服务器的地址,不是我的广域网IP。
我在广州郊区,来广州可以找我,好点的快餐招待:)。

jxgzay 2005-08-14
  • 打赏
  • 举报
回复
to daisy8675(莫依):
你的代码可正区饿获取PublicIP,名符其实的好斑竹,双星斑竹,佩服。
我基础知识是不牢,谢谢指出。

202.96.134.133 依然是DNS服务器的地址,不是我的广域网IP。
我在广州郊区,来广州可以找我,好点的快餐招待:)。





VBDN 2005-08-13
  • 打赏
  • 举报
回复
请看如下网址:
http://www.ztsjckb.com/test.asp
圣骑士 2005-08-13
  • 打赏
  • 举报
回复
难道真不能直接得到外网IP吗?有没有方法通过代码直接访问局域网的代理服务器,或者是访问路由器得到IP呢?
crycoming 2005-08-11
  • 打赏
  • 举报
回复
楼上的什么逻辑啊?
province_ 2005-08-10
  • 打赏
  • 举报
回复
因为大家用的都是宽带上网,所以网卡上绑的其实是内网IP,所以单纯从本机取IP是取不到外网地址的,只能象上面说的那样借助被访问机器来获取自己的IP。
DooDu 2005-08-08
  • 打赏
  • 举报
回复
to 楼上,ipconfig.exe /all好象不能看到本机的广域网ip
jxgzay 2005-08-08
  • 打赏
  • 举报
回复
利用ipconfig.exe /all可以显示广域网IP
1、通过VB代码,自动建立一个批处理BAT,内容是:利用ipconfig.exe /all的重定向,将显示信息重定到文本(自动完成)
2、shell "bat文件",vbhide
3、通过VB代码,读取文本文件,经过分析判断,筛选出广域网IP
DooDu 2005-08-08
  • 打赏
  • 举报
回复
hoho,一直不敢说解决办法,看来果然是老大这个办法~读能显示ip的asp网页
TechnoFantasy 2005-08-08
  • 打赏
  • 举报
回复
202.96.128.68
61.144.56.101
202.96.134.133

都是DNS Server,不是外网地址。
jxgzay 2005-08-08
  • 打赏
  • 举报
回复
to 楼上,ipconfig.exe /all好象不能看到本机的广域网ip

//以下的202.96.134.133不知是不是 :)
Windows 2000 IP Configuration



Host Name . . . . . . . . . . . . : kjgs-372c143446
Primary DNS Suffix . . . . . . . :
Node Type . . . . . . . . . . . . : Broadcast

IP Routing Enabled. . . . . . . . : No

WINS Proxy Enabled. . . . . . . . : No

DNS Suffix Search List. . . . . . : com

Ethernet adapter 本地连接 2:



Media State . . . . . . . . . . . : Cable Disconnected

Description . . . . . . . . . . . : Efficient Networks Enternet P.P.P.o.E Adapter
Physical Address. . . . . . . . . : 44-45-53-54-77-77


Ethernet adapter 本地连接:



Connection-specific DNS Suffix . : com
Description . . . . . . . . . . . : Intel(R) 82559 Fast Ethernet LAN on Motherboard
Physical Address. . . . . . . . . : 00-02-55-21-24-BA

DHCP Enabled. . . . . . . . . . . : Yes

Autoconfiguration Enabled . . . . : Yes

IP Address. . . . . . . . . . . . : 192.168.1.100

Subnet Mask . . . . . . . . . . . : 255.255.255.0

Default Gateway . . . . . . . . . : 192.168.1.1

DHCP Server . . . . . . . . . . . : 192.168.1.1

DNS Servers . . . . . . . . . . . : 202.96.128.68
61.144.56.101
202.96.134.133
192.168.1.1
Lease Obtained. . . . . . . . . . : 2005年8月8日 15:32:35

Lease Expires . . . . . . . . . . : 2005年8月8日 17:32:35

jxgzay 2005-08-08
  • 打赏
  • 举报
回复

to 楼上,ipconfig.exe /all好象不能看到本机的广域网ip


//我的电脑提取的信息,202.96.134.133不知是不是 :)


Windows 2000 IP Configuration



Host Name . . . . . . . . . . . . : kjgs-372c143446
Primary DNS Suffix . . . . . . . :
Node Type . . . . . . . . . . . . : Broadcast

IP Routing Enabled. . . . . . . . : No

WINS Proxy Enabled. . . . . . . . : No

DNS Suffix Search List. . . . . . : com

Ethernet adapter 本地连接 2:



Media State . . . . . . . . . . . : Cable Disconnected

Description . . . . . . . . . . . : Efficient Networks Enternet P.P.P.o.E Adapter
Physical Address. . . . . . . . . : 44-45-53-54-77-77


Ethernet adapter 本地连接:



Connection-specific DNS Suffix . : com
Description . . . . . . . . . . . : Intel(R) 82559 Fast Ethernet LAN on Motherboard
Physical Address. . . . . . . . . : 00-02-55-21-24-BA

DHCP Enabled. . . . . . . . . . . : Yes

Autoconfiguration Enabled . . . . : Yes

IP Address. . . . . . . . . . . . : 192.168.1.100

Subnet Mask . . . . . . . . . . . : 255.255.255.0

Default Gateway . . . . . . . . . : 192.168.1.1

DHCP Server . . . . . . . . . . . : 192.168.1.1

DNS Servers . . . . . . . . . . . : 202.96.128.68
61.144.56.101
202.96.134.133
192.168.1.1
Lease Obtained. . . . . . . . . . : 2005年8月8日 15:32:35

Lease Expires . . . . . . . . . . : 2005年8月8日 17:32:35


TechnoFantasy 2005-08-08
  • 打赏
  • 举报
回复
参考:
http://community.csdn.net/Expert/topic/3766/3766782.xml?temp=7.030666E-03
http://community.csdn.net/Expert/topic/4130/4130289.xml?temp=.6670648
加载更多回复(2)

1,486

社区成员

发帖
与我相关
我的任务
社区描述
VB API
社区管理员
  • API
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧