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-06-01

概要

paizaのcでズンドコゲッターやってみた。

実際

サンプルコード

# include <stdio.h>
# include <stdlib.h>
# include <time.h>
# include <string.h>
# include <sys/types.h>
# include <sys/socket.h>
# include <netinet/in.h>
# include <arpa/inet.h>
# include <netdb.h>

char * zundoko(void)
{
    struct sockaddr_in server;
    int sock;
    char buf[256];
    char * deststr = "ohijs0.paas.jp-e1.cloudn-service.com";
    unsigned int ** addrptr;
    sock = socket(AF_INET, SOCK_STREAM, 0);
    if (sock < 0) 
    {
	    perror("socket");
	    return 1;
    }
    server.sin_family = AF_INET;
    server.sin_port = htons(80);
    server.sin_addr.s_addr = inet_addr(deststr);
    if (server.sin_addr.s_addr == 0xffffffff) 
    {
	    struct hostent * host;
	    host = gethostbyname(deststr);
	    if (host == NULL) 
	    {
		    if (h_errno == HOST_NOT_FOUND) 
		    {
			    printf("host not found : %s\n", deststr);
		    } 
		    else 
		    {
			    printf("%s : %s\n", hstrerror(h_errno), deststr);
		    }
		    return 1;
	    }
	    addrptr = (unsigned int **) host->h_addr_list;
	    while (* addrptr != NULL) 
	    {
		    server.sin_addr.s_addr = * (* addrptr);
    	    if (connect(sock, (struct sockaddr *) &server, sizeof(server)) == 0) 
    	    {
			    break;
		    }
		    addrptr++;
	    }
	    if (* addrptr == NULL) 
	    {
		    perror("connect");
		    return 1;
	    }
	    else
	    {
	        //printf("ok connect\n");
	    }
    } 
    else 
    {
	    if (connect(sock, (struct sockaddr *) &server, sizeof(server)) != 0) 
	    {
		    perror("connect");
		    return 1;
	    }
    }
    memset(buf, 0, sizeof(buf));
    snprintf(buf, sizeof(buf), "GET /zundoko HTTP/1.1\r\nHost: ohijs0.paas.jp-e1.cloudn-service.com\r\nUser-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)\r\n\r\n");
    int n = write(sock, buf, (int) strlen(buf));
    if (n < 0) 
    {
	    perror("write");
	    return 1;
    }
    else
    {
        //printf("ok write %d\n", n);
    }
    memset(buf, 0, sizeof(buf));
    n = read(sock, buf, sizeof(buf));
    if (n < 0) 
    {
	    perror("read");
	    return 1;
    }
    else
    {
        //printf("ok read %d\n", n);
    }
    char t[64];
    char * g;
    g = strstr(buf, "\r\n\r\n");
    g += 4;
    //puts(g);
    close(sock);
    return g;
}
int main() 
{
    int i = 0;
    int j = 0;
    printf("ここから ");
    while (j < 1) 
    {
        char * s = zundoko();
        printf("%s ", s);
        if (strcmp(s, "ドコ") == 0) 
        {
            if (i == 4) j = 1;
            i = 0;
        }
        else
        {
            i++;
        }
    }
    printf("キ・ヨ・シ!");
}
0
0
2

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?