当前位置: 编程技术>c/c++/嵌入式
linux c 获取本机公网IP的实现方法
来源: 互联网 发布时间:2014-10-16
本文导语: 1、linux c代码实现 代码如下:#include #include #include #include #include #include #include #include typedef enum {false,true}bool;int main() { bool flag; int sock; char **pptr = NULL; struct sockaddr_in destAddr; struct hostent *ptr = NULL...
1、linux c代码实现
#include
#include
#include
#include
#include
#include
#include
#include
typedef enum {false,true}bool;
int main()
{
bool flag;
int sock;
char **pptr = NULL;
struct sockaddr_in destAddr;
struct hostent *ptr = NULL;
char destIP[128];
char szBuffer[] = {"GET /ip2city.asp HTTP/1.1rnHost:www.ip138.comrnConnection:Closernrn"};
char res[1024];
//初始化socket
sock = socket(AF_INET,SOCK_STREAM,0);
if( -1 == sock ){
perror("creat socket failed");
exit(0);
}
bzero((void *)&destAddr,sizeof(destAddr));
destAddr.sin_family = AF_INET;
destAddr.sin_port = htons(80);
//首先是获取www.ip138.com的IP地址
ptr = gethostbyname("www.ip138.com");
if(NULL == ptr){
perror("gethostbyname error");
exit(0);
}
//对于www.ip138.com的每个IP进行尝试连接,直到有一个连接成功为止,或者无法连接,则退出程序
for(flag=false,pptr=ptr->h_addr_list ; NULL != *pptr ; ++pptr){
inet_ntop(ptr->h_addrtype,*pptr,destIP,sizeof(destIP));
printf("addr:%sn",destIP);
destAddr.sin_addr.s_addr = inet_addr(destIP);
if(-1!=connect(sock,(struct sockaddr *)&destAddr,sizeof(struct sockaddr))){
flag = true;
break;
}
}
if(false == flag){
perror("connect failed");
}
//发送获取IP的数据包给www.ip138.com
if(strlen(szBuffer) != send(sock,szBuffer,strlen(szBuffer),0)){
perror("send error");
exit(0);
}
//接收数据包
if(-1 == recv(sock,res,1024,0)){
perror("recv error");
exit(0);
}
printf("res:n%sn",res);
return 0;
}
2、将上述代码保存getip.c,并使用如下命令编译
gcc -o getip getip.c
3、运行程序
./getip
代码如下:
#include
#include
#include
#include
#include
#include
#include
#include
typedef enum {false,true}bool;
int main()
{
bool flag;
int sock;
char **pptr = NULL;
struct sockaddr_in destAddr;
struct hostent *ptr = NULL;
char destIP[128];
char szBuffer[] = {"GET /ip2city.asp HTTP/1.1rnHost:www.ip138.comrnConnection:Closernrn"};
char res[1024];
//初始化socket
sock = socket(AF_INET,SOCK_STREAM,0);
if( -1 == sock ){
perror("creat socket failed");
exit(0);
}
bzero((void *)&destAddr,sizeof(destAddr));
destAddr.sin_family = AF_INET;
destAddr.sin_port = htons(80);
//首先是获取www.ip138.com的IP地址
ptr = gethostbyname("www.ip138.com");
if(NULL == ptr){
perror("gethostbyname error");
exit(0);
}
//对于www.ip138.com的每个IP进行尝试连接,直到有一个连接成功为止,或者无法连接,则退出程序
for(flag=false,pptr=ptr->h_addr_list ; NULL != *pptr ; ++pptr){
inet_ntop(ptr->h_addrtype,*pptr,destIP,sizeof(destIP));
printf("addr:%sn",destIP);
destAddr.sin_addr.s_addr = inet_addr(destIP);
if(-1!=connect(sock,(struct sockaddr *)&destAddr,sizeof(struct sockaddr))){
flag = true;
break;
}
}
if(false == flag){
perror("connect failed");
}
//发送获取IP的数据包给www.ip138.com
if(strlen(szBuffer) != send(sock,szBuffer,strlen(szBuffer),0)){
perror("send error");
exit(0);
}
//接收数据包
if(-1 == recv(sock,res,1024,0)){
perror("recv error");
exit(0);
}
printf("res:n%sn",res);
return 0;
}
2、将上述代码保存getip.c,并使用如下命令编译
代码如下:
gcc -o getip getip.c
3、运行程序
代码如下:
./getip