0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

c++でズンドコゲッター

Last updated at Posted at 2016-05-29

概要

C++でズンドコゲッターやってみた。

実際

サンプルコード

# include <stdio.h>
# include <sys/socket.h>
# include <arpa/inet.h>
# include <stdlib.h>
# include <unistd.h>
# include <netdb.h>
# include <string.h>
# include <iostream>
using namespace std;

int create_tcp_socket();
char * get_ip(char * host);
char * build_get_query(char * host, char * page);
# define PORT        80
# define USERAGENT   "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)"
string zundoko()
{
    struct sockaddr_in * remote;
    int sock;
    int tmpres;
    char * ip;
    char * get;
    char buf[BUFSIZ + 1];
    char * host = "ohijs0.paas.jp-e1.cloudn-service.com";
    char * page = "/zundoko";
    sock = create_tcp_socket();
    ip = get_ip(host);
    remote = (struct sockaddr_in *) malloc(sizeof(struct sockaddr_in *));
    remote->sin_family = AF_INET;
    tmpres = inet_pton(AF_INET, ip, (void *) (&(remote->sin_addr.s_addr)));
    if (tmpres < 0)
    {
        perror("Can't set remote->sin_addr.s_addr");
        exit(1);
    }
    else if (tmpres == 0)
    {
        fprintf(stderr, "%s is not a valid IP address\n", ip);
        exit(1);
    }
    remote->sin_port = htons(PORT);
    if (connect(sock, (struct sockaddr *) remote, sizeof(struct sockaddr)) < 0)
    {
        perror("Could not connect");
        exit(1);
    }
    get = build_get_query(host, page);
    int sent = 0;
    while (sent < strlen(get))
    {
        tmpres = send(sock, get + sent, strlen(get) - sent, 0);
        if (tmpres == -1)
        {
            perror("Can't send query");
            exit(1);
        }
        sent += tmpres;
    }
    memset(buf, 0, sizeof(buf));
    int htmlstart = 0;
    char * htmlcontent;
    while ((tmpres = recv(sock, buf, BUFSIZ, 0)) > 0)
    {
        if (htmlstart == 0)
        {
            htmlcontent = strstr(buf, "\r\n\r\n");
            if (htmlcontent != NULL)
            {
                htmlstart = 1;
                htmlcontent += 4;
            }
        }
        else
        {
            htmlcontent = buf;
        }
        memset(buf, 0, tmpres);
    }
    if (tmpres < 0)
    {
        perror("Error receiving data");
    }
    free(get);
    free(remote);
    free(ip);
    close(sock);
    return htmlcontent;
}
int create_tcp_socket()
{
    int sock;
    if ((sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
    {
        perror("Can't create TCP socket");
        exit(1);
    }
    return sock;
}
char * get_ip(char * host)
{
    struct hostent * hent;
    int iplen = 15;
    char * ip = (char *) malloc(iplen + 1);
    memset(ip, 0, iplen + 1);
    if ((hent = gethostbyname(host)) == NULL)
    {
        herror("Can't get IP");
        exit(1);
    }
    if (inet_ntop(AF_INET, (void *) hent->h_addr_list[0], ip, iplen) == NULL)
    {
        perror("Can't resolve host");
        exit(1);
    }
    return ip;
}
char * build_get_query(char * host, char * page)
{
    char * query;
    char * getpage = page;
    char * tpl = "GET /%s HTTP/1.0\r\nHost: %s\r\nUser-Agent: %s\r\n\r\n";
    if (getpage[0] == '/')
    {
        getpage = getpage + 1;
    }
    query = (char *) malloc(strlen(host) + strlen(getpage) + strlen(USERAGENT) + strlen(tpl) - 5);
    sprintf(query, tpl, getpage, host, USERAGENT);
    return query;
}
int main()
{
    int z = 0;
    int c = 0;
    string s;
    do 
    {
        z = c ? (z + 1) : 0;
        s = zundoko();
        if (s == "ズン") c = 1;
        else c = 0;
        c = (rand() >> 3) & 1;
        printf("%s ", c ? "ズン" : "ドコ");
    } while (z < 4 || c);
    printf("キ・ヨ・シ!");
}

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?