#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <stdio.h>
int main(int argc, char *argv[])
{
if (argc < 3)
{
printf("Usage:%s <ip> <port>\n", argv[0]);
return 0;
}
int fd = socket(AF_INET, SOCK_STREAM, 0);
if (fd < 0)
return -1;
struct sockaddr_in saddr;
char *host = argv[1];
int port = atoi(argv[2]);
saddr.sin_family = AF_INET;
saddr.sin_port = htons(port);
//saddr.sin_addr = inet_addr(host);
inet_aton(host, &(saddr.sin_addr));
if (connect(fd, (struct sockaddr*)(&saddr), sizeof(saddr)) < 0 )
{
fprintf(stderr, "connect error.\n");
return -1;
}
else
{
printf("connect success!\n");
char *head = "GET / HTTP/1.0\r\nHost:127.0.0.1\r\n\r\n";
write(fd, head, strlen(head)+1);
char tmp[2048];
read(fd, tmp, sizeof(tmp));
printf("tmp=[%s]\n", tmp);
getchar();
}
return 0;
}
#include <sys/socket.h>
#include <arpa/inet.h>
#include <stdio.h>
int main(int argc, char *argv[])
{
if (argc < 3)
{
printf("Usage:%s <ip> <port>\n", argv[0]);
return 0;
}
int fd = socket(AF_INET, SOCK_STREAM, 0);
if (fd < 0)
return -1;
struct sockaddr_in saddr;
char *host = argv[1];
int port = atoi(argv[2]);
saddr.sin_family = AF_INET;
saddr.sin_port = htons(port);
//saddr.sin_addr = inet_addr(host);
inet_aton(host, &(saddr.sin_addr));
if (connect(fd, (struct sockaddr*)(&saddr), sizeof(saddr)) < 0 )
{
fprintf(stderr, "connect error.\n");
return -1;
}
else
{
printf("connect success!\n");
char *head = "GET / HTTP/1.0\r\nHost:127.0.0.1\r\n\r\n";
write(fd, head, strlen(head)+1);
char tmp[2048];
read(fd, tmp, sizeof(tmp));
printf("tmp=[%s]\n", tmp);
getchar();
}
return 0;
}