| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 647 人关注过本帖, 1 人收藏
标题:获取本机内、外网IP
只看楼主 加入收藏
sych
Rank: 7Rank: 7Rank: 7
等 级:黑侠
威 望:7
帖 子:333
专家分:538
注 册:2019-10-11
结帖率:100%
收藏(1)
已结贴  问题点数:20 回复次数:11 
获取本机内、外网IP
代码都是网上抄来的,不知道有坑没有,请大家指教,同事也请大家分享一下自己掌握的简单易用的方法获取内外网IP的代码
IPSocket=CreateObject("MSWinsock.Winsock")
?"内网IP",IPSocket.LocalIP

test=CREATEOBJECT("WinHttp.WinHttpRequest.5.1")
test.OPEN("get","http://www.,.F.)
test.SEND
?"外网IP",test.responsetext

搜索更多相关主题的帖子: 本机 获取 test IP 外网 
2024-05-28 11:51
csyx
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:火星
等 级:版主
威 望:37
帖 子:716
专家分:2882
注 册:2018-3-13
收藏
得分:7 
谢谢,不过我这用 www. 返回的是 ipv6 地址

这家伙很懒,啥也没留下
2024-05-28 12:05
liuxingang28
Rank: 11Rank: 11Rank: 11Rank: 11
来 自:山东济南
等 级:贵宾
威 望:47
帖 子:658
专家分:2180
注 册:2014-2-7
收藏
得分:7 
若本机有多个 IP,推荐采用下述方法:

LOCAL ;
    oWMI AS OBJECT,;
    oLocal AS OBJECT,;
    oHARDWARE AS OBJECT,;
    object1 AS OBJECT,;
    cIP,;
    cMAC
oWMI = CREATEOBJECT("WbemScripting.SWbemLocator")
oLocal = oWMI.ConnectServer(".", "root\cimv2")
oHARDWARE = oLocal.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration Where IPEnabled=1")
FOR EACH object1 IN oHARDWARE
    cIP = object1.Properties_('IPAddress').VALUE
    cMAC = object1.Properties_('MACAddress').VALUE
    EXIT
ENDFOR
? 'IP地址:',cIP
? 'MAC地址:',cMAC

泉城飞狐
2024-05-28 13:29
sych
Rank: 7Rank: 7Rank: 7
等 级:黑侠
威 望:7
帖 子:333
专家分:538
注 册:2019-10-11
收藏
得分:0 
回复 3楼 liuxingang28
一个程序对外通信,对应的IP是唯一的
2024-05-28 14:44
sych
Rank: 7Rank: 7Rank: 7
等 级:黑侠
威 望:7
帖 子:333
专家分:538
注 册:2019-10-11
收藏
得分:0 
回复 2楼 csyx
通过路由器界面查询的也是IPV6?
这倒是个新情况
2024-05-28 14:47
nbwww
Rank: 8Rank: 8
等 级:贵宾
威 望:11
帖 子:339
专家分:826
注 册:2021-1-9
收藏
得分:7 
公网IP查询可以用这个地址:http://

LOCAL zdrole1
zdrole1 = createObject('Microsoft.XMLHTTP')
zdrole1.open('GET' , 'http://' , .f.)
zdrole1.send()
resu_zjb_url =STREXTRACT(zdrole1.responseText,'<h2>','</h2>')
?resu_zjb_url
2024-05-28 16:15
sych
Rank: 7Rank: 7Rank: 7
等 级:黑侠
威 望:7
帖 子:333
专家分:538
注 册:2019-10-11
收藏
得分:0 
获取程序使用的内网IP地址
PROCEDURE getip
LOCAL dwSize, stTable, nTableCount,    dwLocalAddr, dwOwningPid,szLocalAddr,CurrentProcessId,ii
store "0.0.0.0" TO dwLocalAddr
Declare Integer GetCurrentProcessId In kernel32.Dll
DECLARE LONG GetExtendedTcpTable IN "Iphlpapi.dll" STRING@, LONG@, LONG, LONG, LONG, LONG
DECLARE LONG inet_ntoa IN "Ws2_32" LONG
DECLARE LONG ntohs IN "Ws2_32" LONG
DECLARE INTEGER CloseHandle IN kernel32 INTEGER hObject
DECLARE Long GetModuleFileNameEx IN PsApi  Long hProcess, Long hModule, String @ lpExename, Long dwSize
DECLARE Long OpenProcess IN WIN32API Long dwDesiredAccess, Long bInheritHandle, Long dwProcessId
CurrentProcessId=GetCurrentProcessId()
dwSize=0
GetExtendedTcpTable(NULL, @dwSize, 1, 2, 5, 0) && TCP_TABLE_OWNER_PID_ALL
IF m.dwSize > 0
    stTable = REPLICATE(0h0, m.dwSize) && 分配数据空间
    GetExtendedTcpTable(@stTable, @dwSize, 1, 2, 5, 0)
    nTableCount = CTOBIN(SUBSTR(m.stTable, 1, 4), "4Rs")
    IF m.nTableCount > 0
        stTable = STUFF(m.stTable, 1, 4, "")
        FOR ii = 1 TO m.nTableCount
            szLocalAddr = CTOBIN(SUBSTR(m.stTable, 5, 4), "4R")+2^31 && 本地 IP地址
            szLocalAddr = SYS(2600, inet_ntoa(m.szLocalAddr), 16)
            szLocalAddr = LEFT(szLocalAddr, AT(0h00, m.szLocalAddr)-1)
            dwOwningPid = CTOBIN(SUBSTR(m.stTable, 21, 4), "4R")+2^31 && 关联的进程ID
            IF m.CurrentProcessId=m.dwOwningPid
                dwLocalAddr=m.szLocalAddr
                exit
            endif
            stTable = STUFF(m.stTable, 1, 24, "")
        NEXT
    ENDIF
ENDIF
retu m.dwLocalAddr
2024-05-29 09:59
sam_jiang
Rank: 9Rank: 9Rank: 9
等 级:贵宾
威 望:14
帖 子:842
专家分:1296
注 册:2021-10-13
收藏
得分:0 
公网获取ip地址返回ipv6的原因是因为我们的电脑同时支持ipv6,和ipv4,而且优先ipv6。

这里有简单获得本地IP地址,和外网ip地址的两个程序。
使用ipconfig命令获得本地ip地址,运行时会有个黑屏一闪而过,这个好像不能解决
程序代码:
CLEAR
lcTempFile = "D:\TEMP\temp.txt"
IF FILE(lcTempFile)
    ERASE (lcTempFile)
ENDIF

lcCommand = 'CMD /C "ipconfig > ' + lcTempFile + '"'
RUN &lcCommand

cstr=FILETOSTR(lcTempFile)
n=ALINES(atemp,cstr,CHR(13)+CHR(10))

FOR i=1 TO n
    IF "ipv4" $ LOWER(atemp[i])
        ?ALLTRIM(STREXTRACT(atemp[i],":",""))
        EXIT 
    ELSE 
        LOOP 
    ENDIF
ENDFOR


获得公网ip地址的程序如下:
程序代码:
CLEAR 
LOCAL lcURL, lcResult, lcIP

* 定义一个获取外网IP的URL
lcURL = "https:///ip" 
* 备用网址 https:[color=#808080]//api./jsonip[/color]
* 备用网址 https:[color=#808080]//www./ip &&需要关闭ipv6,否则显示ipv6格式的ip地址[/color]

* 使用WinHTTP对象发送HTTP GET请求
lcResult = GetHTTP(lcURL)

* 解析返回的JSON数据
IF !EMPTY(lcResult)
    *lcIP = STREXTRACT(lcResult, '"origin":"', '"')
    lcIP = STREXTRACT(lcResult, ["],["],3)
    ? "外网IP地址: " + lcIP
ELSE
    ? "无法获取外网IP地址"
ENDIF

FUNCTION GetHTTP(lcURL)
    LOCAL loHTTP, lcResponse
    loHTTP = CREATEOBJECT("WinHttp.WinHttpRequest.5.1")
    loHTTP.Open("GET", lcURL, .F.)
    loHTTP.Send()
    WAIT "正在查询ip地址。。。" WINDOW AT 35,130 TIMEOUT 3
    lcResponse = loHTTP.ResponseText
    RETURN lcResponse
ENDFUNC
2025-01-12 20:29
sych
Rank: 7Rank: 7Rank: 7
等 级:黑侠
威 望:7
帖 子:333
专家分:538
注 册:2019-10-11
收藏
得分:0 
*!*  参考一片空白,吹水佬
DECLARE long CreateProcess       IN kernel32 as apiCreateProcess       string@,string@,string@,string@,long,long,string@,string@,string@,string@
DECLARE long CloseHandle         IN kernel32 as apiCloseHandle         long
DECLARE long WaitForSingleObject IN kernel32 as apiWaitForSingleObject long,long
DECLARE long CreateFileW         IN kernel32 as apiCreateFileW         string,long,long,string@,long,long,long
DECLARE long GetFileSize         IN kernel32 as apiGetFileSize         long,long@
DECLARE long CloseHandle         IN kernel32 as apiCloseHandle         long
DECLARE long ReadFile            IN kernel32 as apiReadFile            long,string@,long,long@,string@
DECLARE long WriteFile           IN kernel32 as apiWriteFile           long,string@,long,long@,string@
Declare long CreatePipe          IN kernel32 as apiCreatePipe           Long@,Long@,string@,Long
#define CREATE_NO_WINDOW        0x08000000
#define GENERIC_READ            0x80000000
#define GENERIC_WRITE           0x40000000
#define FILE_SHARE_READ         1
#define FILE_SHARE_WRITE        2
#define FILE_ATTRIBUTE_NORMAL   0x80
#define CREATE_ALWAYS           2
#define OPEN_EXISTING           3
#define INVALID_HANDLE_VALUE    -1

Execmd="ipconfig"
cstr=Docmd(Execmd)
n=ALINES(atemp,cstr,CHR(13)+CHR(10))
FOR i=1 TO n
    IF "ipv4" $ LOWER(atemp[i])
        ?ALLTRIM(STREXTRACT(atemp[i],":",""))
        EXIT
    ELSE
        LOOP
    ENDIF
ENDFOR
RETURN

FUNCTION DoCmd(cmd)
    LOCAL si, pi,sa,h_read ,h_write,cBuffers,cBuffer,bytes_read
    STORE 0 TO m.h_read,m.h_write,m.bytes_read
    sa=BINTOC(12,"4rs")+BINTOC(0,"4rs")+BINTOC(1,"4rs")
    IF 0==apiCreatePipe(@m.h_read ,@m.h_write ,@sa ,0)
      RETURN ""
    ENDIF
    si = BINTOC(68, "4RS") + REPLICATE(0h00,40)+BINTOC(257,"4RS")+REPLICATE(0h00,12)+BINTOC(m.h_write,"4rs")+REPLICATE(0h00,4)
    pi = REPLICATE(0h00, 16)                        
    IF apiCreateProcess(null,cmd,NULL,NULL,1,CREATE_NO_WINDOW,NULL,null,@si,@pi) == 0
        apiCloseHandle(m.h_write)
        apiCloseHandle(m.h_read)
        RETURN ""
    ENDIF
   
    LOCAL hProcess, hThread
    hProcess = CTOBIN(SUBSTR(pi, 1, 4), "4rs")
    hThread  = CTOBIN(SUBSTR(pi, 5, 4), "4rs")
    apiCloseHandle(m.h_write)
    apiCloseHandle(hProcess)
    apiCloseHandle(hThread)
    m.cBuffers=""
    DO WHILE .t.
        m.cBuffer=REPLICATE(CHR(0),2048)
        m.bytes_read=0
        IF apiReadFile(m.h_read ,@m.cbuffer,2048 ,@m.bytes_read ,0) = 0
           EXIT
        ELSE
           m.cBuffers=m.cBuffers+LEFT(m.cBuffer,m.bytes_read)
        ENDIF
    ENDDO
    apiCloseHandle(m.h_read)
    RETURN m.cBuffers
ENDFUNC
2025-01-13 10:48
easyppt
Rank: 8Rank: 8
等 级:蝙蝠侠
威 望:1
帖 子:323
专家分:786
注 册:2021-11-24
收藏
得分:0 
获取本地IP地址,上面的 代码 都得不到正确的IP

比如:笔记本 WIFI联网的
7 天前 14:38
快速回复:获取本机内、外网IP
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.021020 second(s), 10 queries.
Copyright©2004-2025, BCCN.NET, All Rights Reserved