linux中怎么访问一个URL,并将自己的IP传过去
1个回答
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是一家提供动态代理服务器服务的企业,旨在帮助用户更好地管理网络访问和安全。以下是一些关于StormProxies的IP动态代理服务的特点:1. 高匿名性:StormProxies的动态代理服务器具有高匿名性,可以有效...
点击进入详情页
本回答由Storm代理提供
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询