-
-
Save messa/acfbf88598c50c00325a to your computer and use it in GitHub Desktop.
pro diskuzi na abclinuxu.cz
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <sys/types.h> | |
#include <sys/socket.h> | |
#include <netdb.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <unistd.h> | |
#include <string.h> | |
using namespace std; | |
int get_connected_socket(const char *host, const char *service); | |
int main(int argc, char *argv[]) { | |
int s = get_connected_socket("www.seznam.cz", "80"); | |
const char *request = "GET / HTTP/1.0\r\nHost: www.seznam.cz\r\n\r\n"; | |
if (write(s, request, strlen(request)) != strlen(request)) { | |
fprintf(stderr, "partial/failed write\n"); | |
exit(EXIT_FAILURE); | |
} | |
int size, i; | |
char buf[1024]; | |
std::string response_header, response_body; | |
int BUFSIZE = 1; | |
// read HTTP response header | |
while ((size = recv(s, buf, BUFSIZE, MSG_WAITALL))) | |
{ | |
cout << "1"; | |
response_header += buf[BUFSIZE-1]; | |
i = response_header.find("\r\n\r\n"); | |
if (i != std::string::npos) | |
break; | |
} | |
cout << "Response header:\n" << response_header << endl; | |
// read HTTP response body | |
while ((size = recv(s, buf, BUFSIZE, MSG_WAITALL))) | |
{ | |
cout << "2"; | |
response_body += buf[BUFSIZE-1]; | |
i = response_body.find("\r\n\r\n"); | |
if (i != std::string::npos) | |
break; | |
} | |
cout << "Response body:\n" << response_body << endl; | |
exit(EXIT_SUCCESS); | |
} | |
int get_connected_socket(const char *host, const char *service) { | |
// prevzato z man getaddrinfo | |
struct addrinfo hints; | |
struct addrinfo *result, *rp; | |
int sfd, s, j; | |
size_t len; | |
ssize_t nread; | |
/* Obtain address(es) matching host/port */ | |
memset(&hints, 0, sizeof(struct addrinfo)); | |
hints.ai_family = AF_UNSPEC; /* Allow IPv4 or IPv6 */ | |
hints.ai_socktype = SOCK_STREAM; /* Datagram socket */ | |
hints.ai_flags = 0; | |
hints.ai_protocol = 0; /* Any protocol */ | |
s = getaddrinfo(host, service, &hints, &result); | |
if (s != 0) { | |
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(s)); | |
exit(EXIT_FAILURE); | |
} | |
/* getaddrinfo() returns a list of address structures. | |
Try each address until we successfully connect(2). | |
and) try the next address. */ | |
for (rp = result; rp != NULL; rp = rp->ai_next) { | |
sfd = socket(rp->ai_family, rp->ai_socktype, | |
rp->ai_protocol); | |
if (sfd == -1) | |
continue; | |
if (connect(sfd, rp->ai_addr, rp->ai_addrlen) != -1) | |
break; /* Success */ | |
close(sfd); | |
} | |
if (rp == NULL) { /* No address succeeded */ | |
fprintf(stderr, "Could not connect\n"); | |
exit(EXIT_FAILURE); | |
} | |
freeaddrinfo(result); /* No longer needed */ | |
return sfd; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment