读书人

linux中如何访问一个URL并将自己的I

发布时间: 2013-08-24 13:17:55 作者: rapoo

linux中怎么访问一个URL,并将自己的IP传过去。


#include <iostream>
#include <stdio.h>
#include <windows.h>
#include <wininet.h>

#define MAXSIZE 1024
#pragma comment(lib, "Wininet.lib")
#pragma comment( lib, "ws2_32.lib" )

using namespace std;


char * GetIpList()
{
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD( 2, 2 );
err = WSAStartup( wVersionRequested, &wsaData );

if ( err != 0 )
{
cout<<"WSAStartup failed !"<<endl;
return false;
}

char szhn[256];
int nStatus = gethostname(szhn, sizeof(szhn));
if (nStatus == SOCKET_ERROR )
{
cout<<"gethostname failed, Error code: "<<WSAGetLastError()<<endl;
return false;
}

HOSTENT *host = gethostbyname(szhn);
char * ipaddress =NULL;

if (host != NULL)
{
ipaddress = inet_ntoa( *(IN_ADDR*)host->h_addr_list[0]);
}

WSACleanup();
return ipaddress;

}


int main(){
//Get ip address
char * ip_address = NULL;
ip_address = GetIpList();
cout<<ip_address<<endl;

//Request and send ip_address
char url[100] = "http://www.baidu.com?ip=";
strcat(url, ip_address);
HINTERNET hSession = InternetOpen("UrlTest", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
if(hSession != NULL)


{
HINTERNET hHttp = InternetOpenUrl(hSession, url, NULL, 0, INTERNET_FLAG_DONT_CACHE, 0);

if (hHttp != NULL)
{
BYTE Temp[MAXSIZE];
ULONG Number = 1;
while (Number > 0)
{
InternetReadFile(hHttp, Temp, MAXSIZE - 1, &Number);
Temp[Number] = '\0';
//cout<<Temp<<endl;
}
InternetCloseHandle(hHttp);
hHttp = NULL;
}
InternetCloseHandle(hSession);
hSession = NULL;
}
cout<<url<<endl;

return 0;
}




这是我在windows下,把自己的IP通过URL GET到www.baidu.com。

如果在linux还是使用这一份代码,通过程序自动识别系统,然后加载不同的库,实现功能呢?

比如:

if(windows){
#include <windows.h>
viod main(){
.....Request url...
}
}else if(linux){
#include <linux.h>
viod main(){
.....Request url...
}
}

然后我在linux下用GCC编译就可用,在window上用vc编绎就可用了。
这样的思路可以吗?本人搞其它语言的,C/C++才接触,求大神帮忙!

[解决办法]
你的代码里用到的windows api,linux里面没有。
你只能把相应的api换成linux中存在的函数。你会发现几乎要重写了。
[解决办法]
你可以找找C++网络编程或者是纯c的网络编程,这种函数能够跨平台。但是能够写出完全不依赖平台的代码,不好说。
很有可能内部利用宏定义,进行平台判断,然后调用了不同的方法。
[解决办法]

引用:
Quote: 引用:

你的代码里用到的windows api,linux里面没有。
你只能把相应的api换成linux中存在的函数。你会发现几乎要重写了。


有没有通用的函数或方法呢,我只想达到一个目的,将本机的IP传给一个URL,支持LINUX和WIN,并且代码只有一份。
如果没有的话,重写也不存在,只要是把代码放在一个文件里,维护起来方便就行,我的思路能行吗?还有LINUX下怎么访问URL和获得IP呢?

curl,libevent
[解决办法]
这段代码是linux下向某个host(如:www.baidu.com)发送http请求的代码,c语言。

#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <fcntl.h>
#include <sys/select.h>

#define SIZE 1024
#define SERVER_PORT 80

int main(int argc, char *argv[])
{
int socket_fd, count;
char buf[SIZE];
struct sockaddr_in s_addr;
struct hostent *hent;

fd_set fdset;
struct timeval time_val;
if (argc != 2)
{
printf("usage: %s hostname\n", argv[0]);
return -1;
}

socket_fd = socket(AF_INET, SOCK_STREAM, 0);
if (socket_fd == -1)
{
printf("socket failed!\n");
return -1;
}
hent = gethostbyname(argv[1]);

bzero(&s_addr, sizeof(struct sockaddr_in));
s_addr.sin_family = AF_INET;
s_addr.sin_port = htons(SERVER_PORT);
s_addr.sin_addr.s_addr = inet_addr(inet_ntop(hent->h_addrtype, hent->h_addr_list[0], buf, sizeof(buf)));

connect(socket_fd, (struct sockaddr*)(&s_addr), sizeof(struct sockaddr));



count = snprintf(buf, SIZE, "GET / HTTP/1.1\r\nhost:%s\r\n\r\n", argv[1]);
write(socket_fd, buf, count);

FD_ZERO(&fdset);
FD_SET(socket_fd, &fdset);
time_val.tv_sec = 9;
time_val.tv_usec = 0;
count = select(socket_fd + 1, &fdset, NULL, NULL, &time_val);

if (count == 0)
{
printf("timeout\n");
return -1;
}
fcntl(socket_fd, F_SETFL, O_NONBLOCK);
while ((count = read(socket_fd, buf, SIZE - 1)) > 0)
{
buf[count] = 0;
printf("%s\n", buf);
}
close(socket_fd);

return 0;
}

读书人网 >C语言

热点推荐