linux中怎么访问一个URL,并将自己的IP传过去
2016-10-24
展开全部
#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;
}
#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;
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
Storm代理
2023-07-25 广告
2023-07-25 广告
StormProxies是一家国内优质海外HTTP代理商,拥有一个庞大的IP资源池,覆盖200多个地区,IP数量大且匿名度高。其优点还包括超高并发、稳定高效、技术服务等特点,同时提供HTTP、HTTPS以及SOCKS5协议支持。此外,Sto...
点击进入详情页
本回答由Storm代理提供
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询