2019-05-28 11:48:06 +00:00
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <netinet/in.h>
|
2019-03-13 16:33:40 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/socket.h>
|
2019-05-28 11:48:06 +00:00
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <unistd.h>
|
2019-03-13 16:33:40 +00:00
|
|
|
|
|
|
|
int main(int argc, char** argv)
|
|
|
|
{
|
|
|
|
const char* addr_str = "127.0.0.1";
|
|
|
|
if (argc > 1)
|
|
|
|
addr_str = argv[1];
|
|
|
|
|
|
|
|
int fd = socket(AF_INET, SOCK_STREAM, 0);
|
|
|
|
if (fd < 0) {
|
|
|
|
perror("socket");
|
|
|
|
return 1;
|
2019-06-07 09:49:31 +00:00
|
|
|
}
|
2019-03-13 16:33:40 +00:00
|
|
|
|
2019-06-07 09:49:31 +00:00
|
|
|
struct timeval timeout {
|
|
|
|
3, 0
|
|
|
|
};
|
2019-03-13 16:33:40 +00:00
|
|
|
int rc = setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout));
|
|
|
|
if (rc < 0) {
|
|
|
|
perror("setsockopt");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
rc = setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, &timeout, sizeof(timeout));
|
|
|
|
if (rc < 0) {
|
|
|
|
perror("setsockopt");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct sockaddr_in dst_addr;
|
|
|
|
memset(&dst_addr, 0, sizeof(dst_addr));
|
|
|
|
|
|
|
|
dst_addr.sin_family = AF_INET;
|
2019-03-14 14:19:20 +00:00
|
|
|
dst_addr.sin_port = htons(80);
|
2019-03-13 16:33:40 +00:00
|
|
|
rc = inet_pton(AF_INET, addr_str, &dst_addr.sin_addr);
|
2019-06-06 03:25:18 +00:00
|
|
|
if (rc <= 0) {
|
2019-03-13 16:33:40 +00:00
|
|
|
perror("inet_pton");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2019-03-14 00:42:29 +00:00
|
|
|
printf("Connecting to %s...", addr_str);
|
|
|
|
fflush(stdout);
|
2019-03-13 16:33:40 +00:00
|
|
|
rc = connect(fd, (struct sockaddr*)&dst_addr, sizeof(dst_addr));
|
|
|
|
if (rc < 0) {
|
|
|
|
perror("connect");
|
|
|
|
return 1;
|
|
|
|
}
|
2019-03-14 00:42:29 +00:00
|
|
|
printf("ok!\n");
|
2019-03-13 16:33:40 +00:00
|
|
|
|
|
|
|
char buffer[BUFSIZ];
|
2019-03-14 14:19:20 +00:00
|
|
|
const char* msg = "GET / HTTP/1.0\r\n\r\n";
|
2019-03-13 16:33:40 +00:00
|
|
|
|
2019-03-14 00:42:29 +00:00
|
|
|
printf("Sending a greeting...");
|
2019-03-13 22:14:30 +00:00
|
|
|
rc = send(fd, (const char*)msg, strlen(msg), 0);
|
|
|
|
if (rc < 0) {
|
|
|
|
perror("send");
|
|
|
|
return 1;
|
|
|
|
}
|
2019-03-14 00:42:29 +00:00
|
|
|
printf("ok!\n");
|
2019-03-13 16:33:40 +00:00
|
|
|
|
2019-03-14 00:42:29 +00:00
|
|
|
printf("Waiting for response...");
|
2019-03-14 14:19:20 +00:00
|
|
|
size_t total_recv = 0;
|
|
|
|
for (;;) {
|
|
|
|
ssize_t nrecv = recv(fd, buffer, sizeof(buffer), 0);
|
|
|
|
if (nrecv < 0) {
|
|
|
|
perror("recvfrom");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (nrecv == 0)
|
|
|
|
break;
|
|
|
|
total_recv += nrecv;
|
|
|
|
buffer[nrecv] = '\0';
|
|
|
|
printf("\033[36;1m%s\033[0m", buffer);
|
2019-03-13 16:33:40 +00:00
|
|
|
}
|
|
|
|
|
2019-06-22 14:30:32 +00:00
|
|
|
printf("(%zu bytes received)\n", total_recv);
|
2019-03-13 16:33:40 +00:00
|
|
|
rc = close(fd);
|
|
|
|
if (rc < 0) {
|
|
|
|
perror("close");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|