c++builder 2010 怎样获取本机的外网IP ?

rickys2035 China  2011-07-22 12:25:30
c++builder 2010 怎样获取本机的外网IP ? 本机连在了局域网下。
...全文
435 点赞 收藏 25
写回复
25 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
rickys2035 2011-08-02
初步解决了:
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "IdBaseComponent"
#pragma link "IdComponent"
#pragma link "IdUDPBase"
#pragma link "IdUDPServer"
#pragma link "IdUDPClient"
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
AnsiString ip = Edit1->Text;
int port = StrToInt(Edit2->Text);
TBytes MsgStr;
for (int i = 0; i < 5; i++)
{MsgStr = Memo2->Text.BytesOf();}
IdUDPClient1->SendBuffer(ip, port, MsgStr);
}
// ---------------------------------------------------------------------------
void __fastcall TForm1::IdUDPServer1UDPRead(TIdUDPListenerThread *AThread, TBytes AData,
TIdSocketHandle *ABinding)
{
AnsiString RcvData,RcvIP,RcvPort;
RcvIP = ABinding->PeerIP;
RcvPort = IntToStr(ABinding->Port);
Edit3->Text = RcvIP;
Edit4->Text = RcvPort;

int RcvData_len = AData.Length ;
for(int i =0 ;i<RcvData_len ;i++)
{RcvData = RcvData + (char)AData[i] ;}
Memo1->Lines->Add(RcvData);
}
// ---------------------------------------------------------------------------
回复
dami1023 2011-08-02
感觉好复杂啊,学习一下。
回复
rickys2035 2011-08-01
[Quote=引用 20 楼 txxxxxxzzzzzz 的回复:]

最好10条语句之内就可以解决的,比如调用某个函数,可能2、3句就够了
[/Quote]
您给个例子?
回复
txxxxxxzzzzzz 2011-08-01
最好10条语句之内就可以解决的,比如调用某个函数,可能2、3句就够了
回复
txxxxxxzzzzzz 2011-08-01
感觉要依赖头文件,还有其它一些东西,好像不独立,可能不太好用
回复
svcce 2011-08-01
戒了吧!
回复
rickys2035 2011-08-01
[Quote=引用 22 楼 txxxxxxzzzzzz 的回复:]

我用的是cb2010,没有ws2_32.lib这个文件
[/Quote]
我也用2010,ws2_32.lib路径在X:\Program Files\Embarcadero\RAD Studio\7.0\lib
回复
txxxxxxzzzzzz 2011-08-01
我用的是cb2010,没有ws2_32.lib这个文件
回复
rickys2035 2011-07-30
[Quote=引用 16 楼 bosman 的回复:]

delete 只和new 对应.
把c/c++基础学好吧.
[/Quote]
哦,谢谢!
回复
Bosman 2011-07-30
delete 只和new 对应.
把c/c++基础学好吧.
回复
Bosman 2011-07-30
把下面两行注释掉就行了啊.
//if(P_Host != NULL)
// {delete P_Host;}//这里有时会导致运行错误,弹出说读的地址是0x00之类的
回复
fengskill 2011-07-28
mark
回复
rickys2035 2011-07-27
Bosman:
那怎么处理呢?
回复
Bosman 2011-07-27
有时,下面的代码
//if(P_Host != NULL)
// {delete P_Host;}//这里有时会导致运行错误,弹出说读的地址是0x00之类的
P_Host = NULL;
错误的原因是什么呢?有时又运行正常

原因是不用delete
回复
rickys2035 2011-07-25
有时,下面的代码
if(P_Host != NULL)
{delete P_Host;}//这里有时会导致运行错误,弹出说读的地址是0x00之类的
P_Host = NULL;
错误的原因是什么呢?有时又运行正常
回复
rickys2035 2011-07-23
获取公网IP与获取本机IP:
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
#include "urlmon.h" //syradd
#include "stdio.h" //syradd
#include "winsock2.h" //syradd
#include <wchar.h>


#pragma link "D:\\Program Files\\Borland\\CBuilder6\\Lib\\Psdk\\urlmon.lib" //syradd
#pragma link "D:\\Program Files\\Borland\\CBuilder6\\Lib\\Psdk\\ws2_32.lib" //syradd
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button1Click(TObject *Sender)
{//获取公网IP
char Buf_File_Read[MAX_PATH] = {0};
char IP_Temp[128];
char IP_Host[64];
URLDownloadToFile(0,"http://www.ip138.com/ip2city.asp","IPinTheWorld.ini",0,NULL);
FILE *fp_Download_URL_File=NULL;
if ((fp_Download_URL_File=fopen( "IPinTheWorld.ini","r")) == NULL)
{
MessageBox(NULL, "网络故障,无法接入Internet","提示",0);
return ;
}

if ( fp_Download_URL_File != NULL )
{
fseek(fp_Download_URL_File, 0, SEEK_SET);
fread(Buf_File_Read,1,256,fp_Download_URL_File);
fclose(fp_Download_URL_File);
char* Start_IP = strstr(Buf_File_Read,"[");
if (Start_IP)
{
sprintf( IP_Temp,"%s",Start_IP);
int Buf_Length = strlen(IP_Temp);
for (int i =0; i<Buf_Length; i++)
{
IP_Host[i] = IP_Temp[i+1];
if (IP_Temp[i] == ']')
{
IP_Host[i-1] = '\0';
DeleteFile("IPinTheWorld.ini");
continue;
}

}
}

}
AnsiString IP_World = IP_Host;
Memo1->Text = Memo1->Text + "\r\n公网IP:" + IP_World;
//MessageBox(NULL, chIP,chIP,0);
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button2Click(TObject *Sender)
{//获取本机IP
WSADATA Data_WSA;
int Result_WSAStartup = 1;

hostent *P_Host = NULL;
char Computer_Name[128] = "";
Result_WSAStartup = WSAStartup(MAKEWORD(2, 2), &Data_WSA);
if (Result_WSAStartup != 0)//无错返回0
{
MessageBox(NULL, "本机故障,无法启动网络服务","提示",0);
return ;
}
gethostname(Computer_Name, 128);//获取指定计算机的名字
if(Computer_Name[0] == '\0')
{
MessageBox(NULL, "本机网络故障,无法接入局域网","提示",0);
return ;
}
//Memo1->Text = s;
P_Host = gethostbyname(Computer_Name);
if(P_Host == NULL)
{
//Memo1->Text = "p NULL ";
//MessageBox(NULL, "本机网络故障,无法接入局域网","提示",0);
return ;
}
AnsiString IP_Host = inet_ntoa(*((in_addr *)P_Host->h_addr));//获取指定计算机的IP地址

if(P_Host != NULL)
{delete P_Host;}
P_Host = NULL;
WSACleanup();//完成对WSAStartup()的调用
Memo1->Text = Memo1->Text + "\r\n局域网IP:" + IP_Host;
//MessageBox(NULL, HostIP,HostIP,0);
}
//---------------------------------------------------------------------------

代码如有bug,欢迎指正,+++分哦^_^
回复
rickys2035 2011-07-23
[Quote=引用 8 楼 qq752923276 的回复:]

引用 5 楼 syrchina 的回复:

C/C++ code
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop

...
void __fastcall TForm1::Button1Click(TObje……
[/Quote]

呵呵,从VC移植过来忘了改了,不好意思。。。
回复
rickys2035 2011-07-22
看到了一个VC下的例子:http://blog.csdn.net/hacksouls/article/details/3511762
#include "stdafx.h"
#include <stdio.h>
#include<urlmon.h>
#pragma comment (lib,"Urlmon.lib")
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
char buf[MAX_PATH] = {0};
char chTempIp[128];
char chIP[64];
URLDownloadToFile(0,"http://www.ip138.com/ip2city.asp","c://1.ini",0,NULL);
FILE *fp=fopen( "c://1.ini", "r" );

if ( fp != NULL ) {
fseek(fp, 0, SEEK_SET);
fread(buf,1,256,fp);
fclose(fp);
char* iIndex = strstr(buf,"[");
if (iIndex) {
sprintf( chTempIp,"%s",iIndex);
int nBuflen = strlen(chTempIp);
for(int i =0; i<nBuflen; i++){
chIP[i] = chTempIp[i+1];
if(chTempIp[i] == ']') {
chIP[i-1] = '/0';
DeleteFile("c://1.ini");
continue;
}

}
}

}
MessageBox(NULL, chIP,chIP,0);
return 0;
}
回复
__lhy 2011-07-22
[Quote=引用 5 楼 syrchina 的回复:]

C/C++ code
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop

...
void __fastcall TForm1::Button1Click(TObject *Sender)
{
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow);
{
[/Quote]
表示惊讶
回复
ccrun.com 2011-07-22
[Quote=引用 6 楼 syrchina 的回复:]
引用 2 楼 ccrun 的回复:

一般都是通过访问互联网上的一个脚本文件,在脚本文件中输出来访者的IP。

妖哥,还有个问题请教:如何获取本机在局域网的IP呢?
[/Quote]

局域网的IP就好办了,用netbios函数直接获取网卡IP就行。
回复
加载更多回复
相关推荐
发帖
网络及通讯开发
创建于2007-08-02

1300

社区成员

C++ Builder 网络及通讯开发
申请成为版主
帖子事件
创建了帖子
2011-07-22 12:25
社区公告
暂无公告